range error - or "How to take the factorial of a huge
Jim Witte
jswitte at bloomington.in.us
Fri Apr 9 14:36:18 EDT 2004
> 2.
> Work with logs of expressions.
The MathWorld page on Stirling's approximation gives a formula for
(log n!) as well. The first formula gives an *exect* (I think)
derivation for ln n! as
ln n! = sum(k=1..n, ln k)
which is approximately (by changing that summation to an integral from
1..n and simplifying it, to give
ln n! ~ n ln n - n
It also gives an (exact) integral definition of factorial:
n! = integral(0, infinity, ( exp(-x)*(x^n) ), dx)
So you could write an definite-integration function, taking a small
slice parameter for dx, and sum the integrand term: This would be
something around the following
function integrand (x, n)
reuturn (exp(-x) * x^n)
end integrand
function fact (n)
put 0 into min
put 100 into max
put 0.001 into dx
put min into x
put 0 into sum
add integrand(x, n) to sum
add dx to x
if (x>max) then
return sum
end if
end fact
since exp(-x) gets very small as x gets large, 100 should provide a
reasonable maximum. I don't know how small dx should really be. I
don't know how much better (or worse) this is compared to the foluma I
quoted earlier.
Jim
More information about the use-livecode
mailing list