Jun-05-2019, 10:51 AM
(Jun-05-2019, 10:06 AM)DeaD_EyE Wrote: You can improve the algorithm, if you define some primes. Here an example:I got the point, but, don't you think it is too slow way to solve the problem? My programm is too slow for that big integer, and I guess I should think up about completely different algorithm to solve it, should i? Or i can somehow improve this one?def is_prime(n): primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 ] if n == 2: return True elif n % 2 == 0 or n <= 1: return False # speed up with known primes if n > 50 and any(n % p == 0 for p in primes): return False sqr = int(math.sqrt(n)) + 1 for divisor in range(3, sqr, 2): if n % divisor == 0: return False return True