Reputation: 45
I'm a beginner and I'm doing the problems in and while doing the third problem, which is about finding the largest prime factor of 600851475143, I get this error:
Python int too large to convert to C long
plist = [2]
def primes(min, max):
if 2 >= min:
yield 2
for i in xrange(3, max, 2):
for p in plist:
if i % p == 0 or p * p > i:
break
if i % p:
plist.append(i)
if i >= min:
yield i
def factors(number):
for prime in primes(2, number):
if number % prime == 0:
number /= prime
yield prime
if number == 1:
break
a = 600851475143
print max(factors(a))
Upvotes: 0
Views: 5801
Reputation: 96
This occurs when the number you are dealing with is greater than sys.maxsize
You could possibly use the numpy module and use a larger data type. Not sure how large you need without checking though.
Upvotes: -1
Reputation: 280708
Annoyingly, in Python 2, xrange
requires its arguments to fit into a C long
. 600851475143
is too big for that on your system. You'll have to rewrite your algorithm to not need such a big range, or use a substitute, such as your own xrange
implementation, or a while
loop with manual counter management.
Upvotes: 2