I’m trying to find the largest prime factor of the number x, Python gives me the error that the range is too large. I’ve tried using x range but I get an OverflowError: Python int too large to convert to C long
x = 600851475143
maxPrime = 0
for i in range(x):
isItPrime = True
if (x%i == 0):
for prime in range(2,i-1):
if (i%prime == 0):
isItPrime = False
if (isItPrime == True):
if (i > maxPrime):
maxPrime = i;
print maxPrime
In old (2.x) versions of Python,
xrangecan only handle Python 2.xints, which are bound by the native long integer size of your platform. Additionally,rangeallocates a list with all numbers beforehand on Python 2.x, and is therefore unsuitable for large arguments.You can either switch to 3.x (recommended), or a platform where
long int(in C) is 64 bit long, or use the following drop-in:Equivalently, in a plain form: