[Tutor] OverflowError: cannot fit 'long' into an index-sized integer

Alan Gauld alan.gauld at btinternet.com
Tue Jan 7 11:50:51 CET 2014


On 07/01/14 09:49, Jorge L. wrote:
> I'm working through Project Euler problems, now I'm at problem 3. I did
> an implementation of the shieve of Erastothenes to find the prime
> numbers less than a given number. Then I run a divisibility test over
> those prime numbers to find the largest prime factor of that given
> number.

Please, always send the full error text not a summary.
The error message is full of useful information that helps us see 
exactly what was going on at the time.

It also helps if you tell us which version of Python and OS you are 
using. In this case the OS probably doesn't matter, but I assume you are 
on Python 2.X?

Here's the code:
>
> import time
>
> def main():
>      n = input("Please enter the number to find its highest prime factor: ")
>      start_time = time.clock()
>      l = p(n)
>      div(n,l)
>      print "Execution time = ",time.clock() - start_time, "seconds"
>
>
> # Sieve of Eratosthenes implementation
>
> def p(n):
>      is_p=[False]*2 + [True]*(n-1)

If n is large this will create a large list.
Do you have enough memory for a list with
600851475144 members? It may also have
something to do with your indexing problem.
I certainly can't create anything so big on
my PC.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list