Javascript (116)
JavaScript, 116 bytes
Black magicsmagic here ! Gives a result in fewa few milliseconds.
Only elementary math functions are used : ln
, pow
, exponential
x=9;n=prompt(M=Math);for(i=1e4;i--;)x+=(n/M.exp(-x)/M.pow(x,x-.5)/2.5066/(1+1/12/x+1/288/x/x)-1)/M.log(x);alert(x-1)
Too bad LaTeX is not supported on codegolf but basicallyBasically, I coded a newton solver for f(y)=gamma(y)-n=0
\$ f(y)=\Gamma(y)-n=0 \$ and x=y-1
\$ x=y-1 \$ (because x!
\$ x! \$ is gamma(x+1)
\$ \Gamma(x+1) \$) and approximations for gamma and digamma functions.
Gamma approximation is Stirling approximation
Digamma.
Digamma approximation useuses Euler Maclaurin formula
The. The digamma function is the derivative of gamma function divided by gamma function : f'(y)=gamma(y)*digamma(y)
\$ f'(y)=\Gamma(y) \cdot \psi(y) \$
Ungolfed :
n = parseInt(prompt());
x = 9; //first guess, whatever but not too high (<500 seems good)
//10000 iterations
for(i=0;i<10000;i++) {
//approximation for digamma
d=Math.log(x);
//approximation for gamma
g=Math.exp(-x)*Math.pow(x,x-0.5)*Math.sqrt(Math.PI*2)*(1+1/12/x+1/288/x/x);
//uncomment if more precision is needed
//d=Math.log(x)-1/2/x-1/12/x/x+120/x/x/x/x;
//g=Math.exp(-x)*Math.pow(x,x-0.5)*Math.sqrt(Math.PI*2)*(1+1/12/x+1/288/x/x-139/51840/x/x/x);
//classic newton, gamma derivative is gamma*digamma
x-=(g-n)/(g*d);
}
alert(x-1);
Test cases :
10 => 3.390062988090518
120 => 4.99999939151027
720 => 6.00000187248195
40320 => 8.000003557030217
3628800 => 10.000003941731514