[Tutor] how to handle very large numbers

Dave Angel d at davea.name
Mon Jan 23 03:28:14 CET 2012

On 01/22/2012 09:08 PM, Surya K wrote:
> Well,
> I have been doing a puzzle where I have to deal with number 10^18. A variable can store that value but can't do operations.Ex: If I use range() on it, it shows overflow error.
> So, How do I handle this. I have to use range() for that number.. in this instance.
> Also mention how to handle in other cases too (A small tutorial/ book would be appreciated )
> Thanks 		 	   		
Check out the ongoing thread of someone who's probably doing the same 
assignment.  Subject is "OverflowError in lucky numbers script"

Short answers for range():  You need to specify your Python version.

In Python 2.7, range() has no problem handling longs as its arguments.  
It does have a problem when the number of items gets too large for 
memory.  You could avoid the memory problem by using xrange(), which is 
restricted to ints.  You can, however, write a generator to operate over 
a series of such longs.

I don't believe Python 3.x range has any constraints, and it's already a 
generator, so you'll have to be more specific about what your 
environment is.



More information about the Tutor mailing list