|
500! fits on something like 137 kb. So you need to use more than two ints. I never handled such large factorials, but I did play around with multiplications of very large numbers (I got it up to 15000 decimal digits, probably could have gone MUCH further). What you need is a structure to represent large chunks of bits (preferably a memory mapped file) and a multiplication algorithm (try Karatsuba, FFT multiplication)...
The simplest way I can think of doing it is to do a recursive function to handle the factorials.
The recursive definition of the factorial is:
factorial(0) = 1
factorial(n+1) = factorial(n) * n+1
One thing to remember: multiplying two numbers of length a and b would get a result of size <= (a+b), so you don't have to use up all the memory from the beginning.
|