[Tutor] OverflowError in lucky numbers script
Peter Otten
__peter__ at web.de
Sun Jan 22 13:39:47 CET 2012
Shreesh bhat wrote:
> I m using Python 2.7
> Steven wrote:
> " Scale your numbers from time to time, to avoid them getting too big "
> What does this mean?
>
> inp refers to the sample input test case I have given at first.Its a
> string containing two numbers,
> The program has to handle large numbers till 10**18 and also has to
> execute considerably fast (within 16 CPU time).
>
> Using xrange() causes the OveflowError,Whereas using range() causes too
> many items
> Is writing my own generator only solution? Or is there another way in
> which i can generate big numbers and in considerably fast manner?
If you look at the numbers
$ python -m timeit -s'from lucky_number import islucky; start=10**10;
stop=10**10+1000' 'i = start' 'while i < stop:' ' islucky(i); i += 1'
100 loops, best of 3: 10.8 msec per loop
$ python -m timeit -s'from lucky_number import islucky; start=10**10;
stop=10**10+1000' 'i = start' 'while i < stop:' ' i += 1'
10000 loops, best of 3: 145 usec per loop
you'll see that counting even with a primitive while loop takes less than
two percent of the total time spent. This means you don't have to worry
about that part of your script until you have come up with a luckiness test
that is much faster than your current one.
More information about the Tutor
mailing list