I have tried to implement the first is_prime-function from this answer: https://stackoverflow.com/a/17298131/6208280
# for large numbers, xrange will throw an error.
# OverflowError: Python int too large to convert to C long
# to get over this:
def mrange(start, stop, step):
while start < stop:
yield start
start += step
# benchmarked on an old single-core system with 2GB RAM.
from math import sqrt
def is_prime(num):
if num == 2:
return True
if (num < 2) or (num % 2 == 0):
return False
return all(num % i for i in mrange(3, int(sqrt(num)) + 1, 2))
Yet I have a little probleme while testing high numbers.
For really high numbers, I get an overflow error: long int too large to convert to float
I checked the max for float:
sys.float_info.max
1.7976931348623157e+308
for is_prime(10**308)
everything works fine... but for example with is_prime(10**309)
there will be this overflow error (because of the float max?).
Does that mean 1.7976931348623157e+308 is the limit for this kind of is_prime()-function or is there any solution to check higher numbers with the is_prime()-function?
In my mind such solutions as "use decimal" will not really solve the problem, because of the lack of precision a prime number checking function will need?