fac :: Integer->Integer
fac n
| (n == 0) = 1
| otherwise = (fac (n-1)) * n
But when I run is with the parameter like this
"435435443543532343543254324325435". It show an error :"ERROR - C
stack overflow"
What happened?
Your function is not tail-recursive. Try
fac :: Integer -> Integer
fac n = product [1..n]
and make sure it is strict (e.g. with GHC, compile with -O, no idea
how to do that in WinHugs).
BTW, don't expect an answer for "435435443543532343543254324325435".
With Stirling approximation
logfac b n = n * logBase b (n / exp(1)) + 0.5 * logBase b (2 * pi * n)
So
Prelude> logfac 10 435435443543532343543254324325435
1.402303704118515e34
means the result has 10^34 decimal digits, or in base 2
Prelude> logfac 2 435435443543532343543254324325435
4.658352072275911e34
that's 4*10^34 bits, or
Prelude> 4*10^34 / 8 / 1024^3
4.656612873077393e24
about 4000000000000000000000000 GB of memory, if I didn't make a
stupid mistake somwhere. I doubt you can buy this much memory
somewhere, much less find a CPU to address all of it :-)
Above some point, the results of the factorial function will get large, so
you'll run out of memory. Nothing you can do about that.
- Dirk