Sterlings Approximation for Factorial
Sterling’s approximation for the factorial function, even for small values of x. Out of my own interest, I wanted to know how accurate it is.
\[x! \simeq x^{x} e^{-x} \sqrt{2 \pi x}\]We can evaluate the accuracy for a few values of x. With the following script:
// The factorial way
var factorial = 1;
var curValue = input;
while (curValue > 0) {
factorial *= curValue;
curValue -= 1;
}
// The Sterling way
var approx = Math.pow(input, input) * Math.pow(Math.E, -input) * Math.sqrt(2 * Math.PI * input);
Factorial:
Approximation: