[Tutor] how to handle very large numbers

Surya K suryak at live.com
Mon Jan 23 13:32:50 CET 2012




> Date: Sun, 22 Jan 2012 21:28:14 -0500
> From: d at davea.name
> To: suryak at live.com
> CC: tutor at python.org
> Subject: Re: [Tutor] how to handle very large numbers
> 
> 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.
> 
> 
> -- 
> 
> DaveA
> 

I am using Python 2.7. I don't think range() can take large values.
example: 
I took a = 1000000000000000000 (10^18); This is the max limit in a puzzle.
python showed the following error :OverflowError: range() has too many items.
I even tried using xrange(), I didn't solve the issue..
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120123/ab713555/attachment.html>


More information about the Tutor mailing list