0

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))
4
  • 1
    Where do you convert to a long? Commented Feb 15, 2017 at 17:45
  • 1
    Possible duplicate of OverflowError Python int too large to convert to C long Commented Feb 15, 2017 at 17:46
  • 1
    I don't, I'm using Windows PowerShell to run that script, and the error shown is in that itself Commented Feb 15, 2017 at 17:48
  • @WillemVanOnsem I think they don't, xrange apparently does. Commented Feb 15, 2017 at 17:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

1 Comment

How to do it without numpy dependency?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.