[Tutor] how to handle big numbers

Dave Angel d at davea.name
Fri Dec 9 21:18:09 CET 2011


On 12/09/2011 03:04 PM, surya k wrote:
>
>
>
> ----------------------------------------
>> Date: Fri, 9 Dec 2011 14:53:07 -0500
>> From: d at davea.name
>> To: suryak at live.com
>> CC: tutor at python.org
>> Subject: Re: [Tutor] how to handle big numbers
>>
>> On 12/09/2011 02:25 PM, surya k wrote:
>>> Finding factorial of 8 or 9 isn't big. If I would like to find factorial of 32327, how can I ??
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>> How close do you want your answer? The gamma function can be used to
>> calculate it, but it won't be precise. I don't know how many digits
>> are in 32327!, but it must be at least hundreds of thousands, and a
>> float cannot represent that exactly. In fact it cannot represent a
>> number that big, even approximately. It might be millions of digits,
>> but I don't have the time right now to figure it out.
>>
>> If you use a long int (which int will promote to, automatically), you
>> could do the calculation with a very simple program, providing you don't
>> run out of either time or memory.
>>
>> if it were my problem, I'd do it in three steps. First write a program
>> to calculate N! exactly. See how long it takes for 100, and how many
>> digits are in the answer. Then try it again for 1000!
>>
>> Next, I'd look up the gamma function, and figure out how large the
>> desired value will be.
>>
>> Finally, depending on what I got from those first two, I'd either run
>> the first program with 32327, and wait a long time, or write a
>> specialized math package to calculate the gamma function to whatever
>> precision I thought Ineeded.
>>
>>
>> --
>>
>> DaveA
>>
>
> Well, its in a puzzle.. everything is done except this part. I need to calculate N! ( max value of N is 10^6).
>   		 	   		

I decided to take the time, and the desired number is 1317440 digits 
long, and takes about 3 seconds to compute.  So try it, and forget my 
stuff till you want a really large factorial.

I tried factorial of 10 times your number, and decided to kill it after 
5 minutes.  So if you have to go up to a million, my 3-part answer is 
still appropriate.

If it's part of a larger puzzle, perhaps you are not really looking for 
the actual value, but are going to do something with it.  For example, 
you may just want the last 1000 digits (I can tell you that answer 
without a program).

What does your code look like so far?  If it's more than one line, 
perhaps you haven't loooked hard enough at the standard library.  it has 
both gamma and factorial functions.  Unfortunately, the gamma function 
runs out of steam at less than 180, when the float just isn't big enough.

There are other approaches that may bear looking at, like the SciPy 
library.  But first we'd need to know just what you need with this 
factorial.
--

DaveA


More information about the Tutor mailing list