Factorial of 32767

  • Thread starter Thread starter kids_pro
  • Start date Start date
K

kids_pro

Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks
 
You will need to use or write a class that can handle extremely large
numbers. You're using an int which has a range of roughly -2 billion to +2
billion. Well below 32767 factorial. If you search around with Google, I'm
sure you'll be able to find some C++ if not some C# classes that can handle
numbers of "infinite" length. I say that in quotes because those classes
will still be limited by memory. Also, as the numbers become larger, the
operations will eventually get very slow. My guess is that 32767! will take
quite some time to calculate since the final result is going to probably be
in the vicinity of 100,000 digits long.

Pete
 
I would suggest that you use either a pre-calculated values or a library
that supports that.
It's not a good idea to calculate that at runtime.
Try to search on the web - there are plenty of articles on this.

Cheers,
Branimir
 
You won't go very far with int or even long. You will very quickly overflow.
The decimal type won't bring you much further (28 digits max for the
result). And the double type will only give you a very approximate result,
and will stop when the result has a bit more than 300 digits.

You can write your own "infinite precision integer" library, but you can
also use the J# library for this.
Add a reference to vjslib.dll to your project (you need to install the J#
redistributable if you don't have it)
Then, you will be able to use the java.math.BigInteger class, which does
exactly what you want.

Documentation is on:
http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html

Be careful: take a pencil first and try to evaluate the size of your result.
BigInteger can theoretically handle numbers of any size but you will be
limited by the amount of memory (the binary representation of your number
has to fit in memory).

n! ~= n**n * e**-n * sqrt(2*pi*n)

So, 32768! will fit withouth any problem but it will take a long time to
compute. On my fast machine, 1000! already takes more than 1 minute (and it
already has 2568 digits!). So be ready for a few hours of computation and
several pages of digits (more than 100,000) in the end.

Bruno.
 
The Windows calculator churns for about 10 seconds and finally comes up
with....... wait for it, wait for it :-)

2.7749286793134184196846444043574e+133729

which (as you probably know) is approximately
27749286793134184196846444043574 followed by 133697 zeros.

Best Regards
Julian Nicholls
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top