NFactorial[n_], fast for large (n).
- To: mathgroup@smc.vnet.net
- Subject: [mg12506] NFactorial[n_], fast for large (n).
- From: Ersek_Ted%PAX1A@mr.nawcad.navy.mil
- Date: Sat, 23 May 1998 18:10:39 -0400
If (num) is a large integer N[ num! ] computes the exact value of
(num!),
and then converts it to a less precise value. This can take a
relatively long time (see below).
In[1]:=
N[12000!];//Timing
Out[1]=
{10.16 Second,Null}
But (n!) == Gamma[n+1]
So I define a function NFactorial[n_Integer]
In[2]:=
NFactorial[n_Integer]:=Gamma[n+1.0]
NFactorial[12000] is computed in an instant!
In[3]:=
NFactorial[12000];//Timing
Out[3]=
{0. Second,Null}
I wanted to assign the rule to N[Factorial[n_Integer]] but I understand
I would have to give N a Hold attribute to do that. I worry there will
be a down side to making N hold it's argument.
You can also make fast machine precision implementations of other
functions such as:
Binomial, Fibonacci, BernoulliB, EulerE, StirlingS1, ....
The Handbook of Mathematica Functions, by Abramowitz M., Stegun I. A.
is a good source for the needed identities. However, Abramowitz,
Stegun neglect to discuss Fibonacci numbers!
For Fibonacci numbers use the following elegant expression:
Fibonacci[n_]:=(GoldenRatio^n - (-GoldenRatio)^(-n))/Sqrt[5]
Ted Ersek