You must be joking! I just calculated that factorial, it is expressed by the integer value with its decimal presentation which is 77388 decimal places long!
No CPU integer type can carry so much information.
If you can use Framework 4.0 you can use
System.Numerics.BigInteger
for such calculation:
static BigInteger Factorial(int arg) {
BigInteger value = 1;
for (int index = 2; index <= arg; index++)
value *= index;
return value;
}
Let's test it and compare with maximum
long
:
string factorial = string.Format("{0} decimal places", Factorial(20000));
Console.WriteLine(factorial.Length);
Console.WriteLine(long.MaxValue);
Output:
77338 decimal places
9223372036854775807
Sorry I don't want to print the integer value 77338 digits long.
No wonder you could not do it using
long
!
—SA