We consider a handful of useful approximations to the factorial or gamma function.
We begin with a derivation of Stirling's approximation. It is easy to show, using integration by parts, that the following integral representation of the gamma function gives the factorial of integer values.
The Γ function is a continuous generalization of the integer-defined factorial function, with the definition such that . This integral is also known as a Euler integral of the second kind. In order to derive Stirling's approximation, we first rewrite the integrand.
For values of , the exponential approaches zero because of the logarithm. For large values of , the exponential also approaches zero. The integral is dominated by values of near the local maximum of . For and its derivatives we find
, and .
The derivative tells us that the maximum is at . Near the maximum we approximate the function in terms of as
,
which gives us an approximation to the integral as
The usual application of this approximation is for large values of , in which case we can treat the integral's lower limit as minus infinity without additional error. This gives the standard version of Stirling's approximation.
We begin with the same integral.
This time, we rewrite the integral with a change of variables in terms of . The result is
We use the same treatment of this integral as the previous one. For and its derivatives we find
, and .
The derivative tells us that the maximum is at . Near the maximum we approximate the function in terms of as
,
with the corresponding approximation to the integral
We arrive at our first modified version of Stirling's approximation:
The following plot shows a comparison of these approximations with the gamma (factorial) function from 2 to 3. First, it is somewhat remarkable how well they both do at such small values of n, since derivations generally assume large values of n. The relevant factorials are obviously 2!=2 and 3!=6. Stirling's approximation gives 1.9190 and 5.8362, with errors of -4.0% and -2.7%. The second formula, expressed in terms of n+1, gives 1.9454 and 5.8765, for errors of -2.7% and -2.1%. There is a much more remarkable difference at n=0. The true value is 0!=1. Stirling's approximation gives 0. The modified formula gives .

Comparison of gamma (factorial) function, Stirling's approximation, and the preceding equation from 2 to 3. The δ=0.5 label is a function also known as Burnside's formula.
If we compare Stirling's approximation with the modified formula, they can both be represented by the following equation
Stirling's approximation is recovered with . The modified formula follows for . Interestingly, there is improved accuracy for intermediate values. The plot above includes the example of . Over this range, this is an improvement, with a value of 2.0333 at 2 and 6.0715 at 3, larger than the correct factorial values by 1.7% and 1.2%. Optimal values (in some regard) for δ can be found in Mortici. Below, we will show that these special values of δ cancel the 1/m2 term in an asymptotic expansion.
Now we consider the accuracy of Stirling's approximation in an asymptotic sense and derive a higher order term that makes it more accurate for large values of n. Consider the ratio of the true factorial to Stirling's approximation.
If the function were precisely equal to the factorial, both this ratio and the ratio of sequential terms in this series would be unity. The ratio of sequential terms is
This simplifies to
,
which we finally write as
If this ratio is one, then its logarithm will be zero.
This expression is convenient because we can use the Taylor expansion of the logarithms to get the asymptotic dependence in powers of 1/n.
,
with the ellipsis denoting neglect of higher powers of 1/n. Similarly, the second logarithm can be written
.
If we combine the terms, then to second order in 1/n, we find
Simplifying the combined terms, we find
(terms proportional to )
It is not difficult to show, by following the same procedure, that the following modification to Stirling's approximation would cancel this term in :
The same asymptotic expansion can be applied to the modified formula. When we do this, we find
This form makes the gamma function notation too convenient, so finally we write
The gamma function is often written with an argument of z to denote that it is defined throughout the complex plane. Here it is written as a function of x, because the approximation is valid for the factorial of real numbers greater than zero (corresponding to gamma function arguments x ≥ 1).
Let's return to the generalized formula
We can perform another asymptotic study on this functional form. Along the way, we define m=n+δ. Then for the same power series expansion performed on the standard Stirling's approximation, we find the first two non-zero terms to be
(terms proportional to )
The first term is zero for values , which happen to be the values given in Mortici. So these optimal values, as given, cancel the second order term. However, these values result in a non-zero 1/m3 term. Interestingly there are three values of δ which result in these two terms being equal: 0, 1/2 and 1. For these values, the 1/m2 term as given above also cancels the 1/m3 term.
We can also apply the Euler-Maclaurin formula directly to the logarithm of n!. We set the integration constant to half the logarithm of 2π based on previous considerations. Including terms through the ninth derivative of the logarithm, this yields
This is identical to 6.1.41 in Handbook of Mathematical Functions, with one additional term included.
The advantage in the modified formula is due to the form the integral takes before application of the approximation. The limits extend to both plus and minus infinity after subsitution, plus the functional form of the exponential is better suited to the approximation. It actually even works for values of x < 1 in the argument of the gamma function, but it fails as the gamma function approaches x=0.
Behavior of the factorial function and approximations from 0 to 2. The correct value of 0! is 1. Stirling's approximation gives 0. The higher order (H.O.) version of Stirling's approximation diverges near zero, but agrees much better than the original version as x approaches 0.5. The modified formula is a decent approximation with an offset less than 8% throughout this range. The H.O. version of the modified formula is nearly indistinguishable from the correct value throughout this range.
We now show a precise comparison between some of the approximations given here and some very good ones. The one from Luschny looks like the Burnside formula, but with four correction terms in continued fraction form. The one labelled δ=0.7887 is one of the versions that cancel the 1/m2 expansion term. The relatively simple modified HO form derived here is practically identical to 6.1.37 in Handbook of Mathematical Functions including terms through 1/(288z2) in square brackets. It is the best at the smallest values, then the Luschny form is best until about x=4. The Euler-Maclaurin formula result ('Euler') is the best above x≈4 and rapidly approaches floating point precision above x=10. It is amusing that the simple form approximates the factorial function well to x values as low as -0.5 (gamma function argument of 0.5).
Relative errors in five different approximations to the factorial function.
[1]
"An ultimate extremely accurate formula for approximation of the factorial function," by Cristinel Mortici, in Archiv der Mathematik 93 (2009).
[2]
"Approximation Formulas for the Factorial Function n!," by Peter Luschny
[3]
"Handbook of Mathematical Functions," Abramowitz and Stegun, Dover Publications