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/...igInteger.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.
"kids_pro" <ki******@yahoo.com> a écrit dans le message de
news:eQ**************@TK2MSFTNGP10.phx.gbl...
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