Stirling's approximation

It is a good approximation, leading to accurate results even for small values of

It is named after James Stirling, though a related but less precise result was first stated by Abraham de Moivre.

In computer science applications such as the worst-case lower bound for comparison sorting, it is convenient to instead use the binary logarithm, giving the equivalent form

Roughly speaking, the simplest version of Stirling's formula can be quickly obtained by approximating the sum

The full formula, together with precise estimates of its error, can be derived as follows.

, one considers its natural logarithm, as this is a slowly varying function:

is a Bernoulli number, and Rm,n is the remainder term in the Euler–Maclaurin formula.

where big-O notation is used, combining the equations above yields the approximation formula in its logarithmic form:

Taking the exponential of both sides and choosing any positive integer

, so we "peel off" this dominant term, then perform two changes of variables, to obtain:

This line integral can then be approximated using the saddle-point method with an appropriate choice of contour radius

gives the usual, more precise form of Stirling's approximation.

[7] Further terms are listed in the On-Line Encyclopedia of Integer Sequences as A001163 and A001164.

(Bender and Orszag[8] p. 218) gives the asymptotic formula for the coefficients:

which shows that it grows superexponentially, and that by the ratio test the radius of convergence is zero.

As n → ∞, the error in the truncated series is asymptotically equal to the first omitted term.

it is known that the error in truncating the series is always of the opposite sign and at most the same magnitude as the first omitted term.

[citation needed] Other bounds, due to Robbins,[9] valid for all positive integers

The lower bound is weaker than that obtained by stopping the series after the

However, the gamma function, unlike the factorial, is more broadly defined for all complex numbers other than non-positive integers; nevertheless, Stirling's formula may still be applied.

[10] A further application of this asymptotic expansion is for complex argument z with constant Re(z).

See for example the Stirling formula applied in Im(z) = t of the Riemann–Siegel theta function on the straight line ⁠1/4⁠ + it.

Thomas Bayes showed, in a letter to John Canton published by the Royal Society in 1763, that Stirling's formula did not give a convergent series.

can be obtained by rearranging Stirling's extended formula and observing a coincidence between the resultant power series and the Taylor series expansion of the hyperbolic sine function.

This approximation is good to more than 8 decimal digits for z with a real part greater than 8.

Robert H. Windschitl suggested it in 2002 for computing the gamma function with fair accuracy on calculators with limited program or register memory.

The approximation may be made precise by giving paired upper and lower bounds; one such inequality is[16][17][18][19]

The formula was first discovered by Abraham de Moivre[2] in the form

De Moivre gave an approximate rational-number expression for the natural logarithm of the constant.

Stirling's contribution consisted of showing that the constant is precisely

Comparison of Stirling's approximation with the factorial
The relative error in a truncated Stirling series vs. , for 0 to 5 terms. The kinks in the curves represent points where the truncated series coincides with Γ( n + 1) .
The relative error in a truncated Stirling series vs. the number of terms used