View Single Post
  #8  
Old January 29th, 2007, 06:08 AM
chogo chogo is offline
Member
 
Join Date: Nov 2006
Location: oxford
Posts: 118
Country:
Thanks: 57
Thanked 15 Times in 15 Posts
chogo is on a distinguished road
Default

i had this problems some weeks back. Stirlings function is probably the best for large factorials. But otherwise use this bit of code i wrote in java which is computationally efficient.


public double gammaln(float xx){
double x,y,tmp,ser;
double[] cof={76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5};
int j;
y=x=xx;
tmp=x+5.5;
tmp -= (x+0.5)*Math.log(tmp);
ser=1.000000000190015;
for (j=0;j <= 5;j++) {ser += cof[j]/++y;}
return -tmp+ Math.log(2.5066282746310005*ser/x);}


public double factorial(float n){
double value = gammaln(n + 1);
double answer = Math.exp(value);
return answer;}

This is based around a method of gamma approximation by C.lanczos. (Paper is called - (A precision approximation of the gamma function).

This method can also be used to find NON integer factorials
Reply With Quote