1

Put shortly, I am trying to create and loop through a list from 0 to an arbitrary large number. This number is likely to be huge (e.g. 10000000000). I've tried using xrange, but it runs into overflow errors. I also tried using itertools.islice with count() (as suggested here), but the range I need is larger than sys.maxint, which is the limit on islice()'s stop argument.

What I would like to know is whether there's any way to deal with these large numbers in an elegant way? Is there something I'm missing here? I know I have the option of using a while-loop, but I would prefer not to.

I'm currently using 32-bit Python 2.7.

Thank you!

Community
  • 1
  • 1
mondayRain
  • 65
  • 6
  • 3
    What are you going to do in your iteration? Surely it will take a huge amount of time to do anything with 1e10 sequential numbers, even if each step is simple. Will your program be useful to you if it takes hours or days to run? Can you skip the loop by finding an analytical solution (e.g. rather than adding the numbers together, use `n*(n+1)/2`)? – Blckknght Jan 19 '15 at 07:28
  • I'm actually trying to write out the algorithm for the Sieve of Eratosthenese: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_description I'm sure there is a way to optimize this, but I'm not entirely sure how... – mondayRain Jan 20 '15 at 01:42
  • 1
    It sounds like you may want to look at [this question](http://stackoverflow.com/questions/2211990/how-to-implement-an-efficient-infinite-generator-of-prime-numbers-in-python/), which asks for efficient approaches to generating a large number of primes. I'm especially fond of the recursive generator solution presented by Will Ness in the third answer (based on [this older recipe](http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/), but with important modifications that greatly reduce its space complexity). – Blckknght Jan 20 '15 at 21:38

0 Answers0