# Stirling's approximation

In mathematics, Stirling's approximation (or Stirling's formula) is an approximation for factorials. It is a good approximation, leading to accurate results even for small values of n. It is named after James Stirling, though it was first stated by Abraham de Moivre.

The version of the formula typically used in applications is

$\ln n!=n\ln n-n+O(\ln n)$ (in big O notation, as $n\to \infty$ ), or, by changing the base of the logarithm (for instance in the worst-case lower bound for comparison sorting),

$\log _{2}n!=n\log _{2}n-n\log _{2}e+O(\log _{2}n).$ Specifying the constant in the O(ln n) error term gives 1/2ln(2πn), yielding the more precise formula:

$n!\sim {\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n},$ where the sign ~ means that the two quantities are asymptotic: their ratio tends to 1 as n tends to infinity.

One may also give simple bounds valid for all positive integers n, rather than only for large n:

${\sqrt {2\pi }}\ n^{n+{\frac {1}{2}}}e^{-n}\leq n!\leq e\ n^{n+{\frac {1}{2}}}e^{-n}$ for $n=1,2,3,\ldots$ . These follow from the more precise error bounds discussed below.

## Derivation

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

$\ln n!=\sum _{j=1}^{n}\ln j$ with an integral:

$\sum _{j=1}^{n}\ln j\approx \int _{1}^{n}\ln x\,{\rm {d}}x=n\ln n-n+1.$ The full formula, together with precise estimates of its error, can be derived as follows. Instead of approximating n!, one considers its natural logarithm, as this is a slowly varying function:

$\ln n!=\ln 1+\ln 2+\cdots +\ln n.$ The right-hand side of this equation minus

${\tfrac {1}{2}}(\ln 1+\ln n)={\tfrac {1}{2}}\ln n$ is the approximation by the trapezoid rule of the integral

$\ln n!-{\tfrac {1}{2}}\ln n\approx \int _{1}^{n}\ln x\,{\rm {d}}x=n\ln n-n+1,$ and the error in this approximation is given by the Euler–Maclaurin formula:

{\begin{aligned}\ln n!-{\tfrac {1}{2}}\ln n&={\tfrac {1}{2}}\ln 1+\ln 2+\ln 3+\cdots +\ln(n-1)+{\tfrac {1}{2}}\ln n\\&=n\ln n-n+1+\sum _{k=2}^{m}{\frac {(-1)^{k}B_{k}}{k(k-1)}}\left({\frac {1}{n^{k-1}}}-1\right)+R_{m,n},\end{aligned}} where Bk is a Bernoulli number, and Rm,n is the remainder term in the Euler–Maclaurin formula. Take limits to find that

$\lim _{n\to \infty }\left(\ln n!-n\ln n+n-{\tfrac {1}{2}}\ln n\right)=1-\sum _{k=2}^{m}{\frac {(-1)^{k}B_{k}}{k(k-1)}}+\lim _{n\to \infty }R_{m,n}.$ Denote this limit as y. Because the remainder Rm,n in the Euler–Maclaurin formula satisfies

$R_{m,n}=\lim _{n\to \infty }R_{m,n}+O\left({\frac {1}{n^{m}}}\right),$ where big-O notation is used, combining the equations above yields the approximation formula in its logarithmic form:

$\ln n!=n\ln \left({\frac {n}{e}}\right)+{\tfrac {1}{2}}\ln n+y+\sum _{k=2}^{m}{\frac {(-1)^{k}B_{k}}{k(k-1)n^{k-1}}}+O\left({\frac {1}{n^{m}}}\right).$ Taking the exponential of both sides and choosing any positive integer m, one obtains a formula involving an unknown quantity ey. For m = 1, the formula is

$n!=e^{y}{\sqrt {n}}\left({\frac {n}{e}}\right)^{n}\left(1+O\left({\frac {1}{n}}\right)\right).$ The quantity ey can be found by taking the limit on both sides as n tends to infinity and using Wallis' product, which shows that ey = 2π. Therefore, one obtains Stirling's formula:

$n!={\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n}\left(1+O\left({\frac {1}{n}}\right)\right).$ ## An alternative derivation

An alternative formula for n! using the gamma function is

$n!=\int _{0}^{\infty }x^{n}e^{-x}\,{\rm {d}}x.$ (as can be seen by repeated integration by parts). Rewriting and changing variables x = ny, one obtains

$n!=\int _{0}^{\infty }e^{n\ln x-x}\,{\rm {d}}x=e^{n\ln n}n\int _{0}^{\infty }e^{n(\ln y-y)}\,{\rm {d}}y.$ Applying Laplace's method one has

$\int _{0}^{\infty }e^{n(\ln y-y)}\,{\rm {d}}y\sim {\sqrt {\frac {2\pi }{n}}}e^{-n},$ which recovers Stirling's formula:

$n!\sim e^{n\ln n}n{\sqrt {\frac {2\pi }{n}}}e^{-n}={\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n}.$ In fact, further corrections can also be obtained using Laplace's method. For example, computing two-order expansion using Laplace's method yields

$\int _{0}^{\infty }e^{n(\ln y-y)}\,{\rm {d}}y\sim {\sqrt {\frac {2\pi }{n}}}e^{-n}\left(1+{\frac {1}{12n}}\right)$ and gives Stirling's formula to two orders:

$n!\sim e^{n\ln n}n{\sqrt {\frac {2\pi }{n}}}e^{-n}\left(1+{\frac {1}{12n}}\right)={\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n}\left(1+{\frac {1}{12n}}\right).$ A complex-analysis version of this method is to consider ${\frac {1}{n!}}$ as a Taylor coefficient of the exponential function $e^{z}=\sum _{n=0}^{\infty }{\frac {z^{n}}{n!}}$ , computed by Cauchy's integral formula as

${\frac {1}{n!}}={\frac {1}{2\pi i}}\oint \limits _{|z|=r}{\frac {e^{z}}{z^{n+1}}}\,\mathrm {d} z.$ This line integral can then be approximated using the saddle-point method with an appropriate choice of countour radius $r=r_{n}$ . The dominant portion of the integral near the saddle point is then approximated by a real integral and Laplace's method, while the remaining portion of the integral can be bounded above to give an error term.

## Speed of convergence and error estimates The relative error in a truncated Stirling series vs. n, for 0 to 5 terms. The kinks in the curves represent points where the truncated series coincides with Γ(n + 1).

Stirling's formula is in fact the first approximation to the following series (now called the Stirling series):

$n!\sim {\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n}\left(1+{\frac {1}{12n}}+{\frac {1}{288n^{2}}}-{\frac {139}{51840n^{3}}}-{\frac {571}{2488320n^{4}}}+\cdots \right).$ An explicit formula for the coefficients in this series was given by G. Nemes.[a] The first graph in this section shows the relative error vs. n, for 1 through all 5 terms listed above.

As n → ∞, the error in the truncated series is asymptotically equal to the first omitted term. This is an example of an asymptotic expansion. It is not a convergent series; for any particular value of n there are only so many terms of the series that improve accuracy, after which accuracy worsens. This is shown in the next graph, which shows the relative error versus the number of terms in the series, for larger numbers of terms. More precisely, let S(n, t) be the Stirling series to t terms evaluated at n. The graphs show

$\left|\ln \left({\frac {S(n,t)}{n!}}\right)\right|,$ which, when small, is essentially the relative error.

Writing Stirling's series in the form

$\ln n!\sim n\ln n-n+{\tfrac {1}{2}}\ln(2\pi n)+{\frac {1}{12n}}-{\frac {1}{360n^{3}}}+{\frac {1}{1260n^{5}}}-{\frac {1}{1680n^{7}}}+\cdots ,$ 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.

More precise bounds, due to Robbins, valid for all positive integers n are

${\sqrt {2\pi }}\ n^{n+{\frac {1}{2}}}e^{-n}e^{\frac {1}{12n+1}} ## Stirling's formula for the gamma function

For all positive integers,

$n!=\Gamma (n+1),$ where Γ denotes the gamma function.

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. If Re(z) > 0, then

$\ln \Gamma (z)=z\ln z-z+{\tfrac {1}{2}}\ln {\frac {2\pi }{z}}+\int _{0}^{\infty }{\frac {2\arctan \left({\frac {t}{z}}\right)}{e^{2\pi t}-1}}\,{\rm {d}}t.$ Repeated integration by parts gives

$\ln \Gamma (z)\sim z\ln z-z+{\tfrac {1}{2}}\ln {\frac {2\pi }{z}}+\sum _{n=1}^{N-1}{\frac {B_{2n}}{2n(2n-1)z^{2n-1}}},$ where Bn is the n-th Bernoulli number (note that the limit of the sum as $N\to \infty$ is not convergent, so this formula is just an asymptotic expansion). The formula is valid for z large enough in absolute value, when |arg(z)| < π − ε, where ε is positive, with an error term of O(z−2N+ 1). The corresponding approximation may now be written:

$\Gamma (z)={\sqrt {\frac {2\pi }{z}}}\,{\left({\frac {z}{e}}\right)}^{z}\left(1+O\left({\frac {1}{z}}\right)\right).$ where the expansion is identical to that of Stirling' series above for n!, except that n is replaced with z-1.

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.

## Error bounds

For any positive integer N, the following notation is introduced:

$\ln \Gamma (z)=z\ln z-z+{\tfrac {1}{2}}\ln {\frac {2\pi }{z}}+\sum \limits _{n=1}^{N-1}{\frac {B_{2n}}{2n\left({2n-1}\right)z^{2n-1}}}+R_{N}(z)$ and

$\Gamma (z)={\sqrt {\frac {2\pi }{z}}}\left({\frac {z}{e}}\right)^{z}\left({\sum \limits _{n=0}^{N-1}{\frac {a_{n}}{z^{n}}}+{\widetilde {R}}_{N}(z)}\right).$ Then 

{\begin{aligned}|R_{N}(z)|&\leq {\frac {|B_{2N}|}{2N(2N-1)|z|^{2N-1}}}{\begin{cases}1&{\text{ if }}|\arg z|\leq {\frac {\pi }{4}},\\|\csc(\arg z)|&{\text{ if }}{\frac {\pi }{4}}<|\arg z|<{\frac {\pi }{2}},\\\sec ^{2N}\left({\tfrac {\arg z}{2}}\right)&{\text{ if }}|\arg z|<\pi ,\end{cases}}\\[6pt]\left|{\widetilde {R}}_{N}(z)\right|&\leq \left({\frac {\left|a_{N}\right|}{|z|^{N}}}+{\frac {\left|a_{N+1}\right|}{|z|^{N+1}}}\right){\begin{cases}1&{\text{ if }}|\arg z|\leq {\frac {\pi }{4}},\\|\csc(\arg z)|&{\text{ if }}{\frac {\pi }{4}}<|\arg z|<{\frac {\pi }{2}}.\end{cases}}\end{aligned}} For further information and other error bounds, see the cited papers.

## A convergent version of Stirling's formula

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. Obtaining a convergent version of Stirling's formula entails evaluating Raabe's formula:

$\int _{0}^{\infty }{\frac {2\arctan \left({\frac {t}{x}}\right)}{e^{2\pi t}-1}}\,{\rm {d}}t=\ln \Gamma (x)-x\ln x+x-{\tfrac {1}{2}}\ln {\frac {2\pi }{x}}.$ One way to do this is by means of a convergent series of inverted rising exponentials. If

$z^{\bar {n}}=z(z+1)\cdots (z+n-1),$ then

$\int _{0}^{\infty }{\frac {2\arctan \left({\frac {t}{x}}\right)}{e^{2\pi t}-1}}\,{\rm {d}}t=\sum _{n=1}^{\infty }{\frac {c_{n}}{(x+1)^{\bar {n}}}},$ where

$c_{n}={\frac {1}{n}}\int _{0}^{1}x^{\bar {n}}\left(x-{\tfrac {1}{2}}\right)\,{\rm {d}}x={\frac {1}{2n}}\sum _{k=1}^{n}{\frac {k|s(n,k)|}{(k+1)(k+2)}},$ where s(nk) denotes the Stirling numbers of the first kind. From this one obtains a version of Stirling's series

{\begin{aligned}\ln \Gamma (x)&=x\ln x-x+{\tfrac {1}{2}}\ln {\frac {2\pi }{x}}+{\frac {1}{12(x+1)}}+{\frac {1}{12(x+1)(x+2)}}+\\&\quad +{\frac {59}{360(x+1)(x+2)(x+3)}}+{\frac {29}{60(x+1)(x+2)(x+3)(x+4)}}+\cdots ,\end{aligned}} which converges when Re(x) > 0.

## Versions suitable for calculators

The approximation

$\Gamma (z)\approx {\sqrt {\frac {2\pi }{z}}}\left({\frac {z}{e}}{\sqrt {z\sinh {\frac {1}{z}}+{\frac {1}{810z^{6}}}}}\right)^{z}$ and its equivalent form

$2\ln \Gamma (z)\approx \ln(2\pi )-\ln z+z\left(2\ln z+\ln \left(z\sinh {\frac {1}{z}}+{\frac {1}{810z^{6}}}\right)-2\right)$ 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.

Gergő Nemes proposed in 2007 an approximation which gives the same number of exact digits as the Windschitl approximation but is much simpler:

$\Gamma (z)\approx {\sqrt {\frac {2\pi }{z}}}\left({\frac {1}{e}}\left(z+{\frac {1}{12z-{\frac {1}{10z}}}}\right)\right)^{z},$ or equivalently,

$\ln \Gamma (z)\approx {\tfrac {1}{2}}\left(\ln(2\pi )-\ln z\right)+z\left(\ln \left(z+{\frac {1}{12z-{\frac {1}{10z}}}}\right)-1\right).$ An alternative approximation for the gamma function stated by Srinivasa Ramanujan (Ramanujan 1988[clarification needed]) is

$\Gamma (1+x)\approx {\sqrt {\pi }}\left({\frac {x}{e}}\right)^{x}\left(8x^{3}+4x^{2}+x+{\frac {1}{30}}\right)^{\frac {1}{6}}$ for x ≥ 0. The equivalent approximation for ln n! has an asymptotic error of 1/1400n3 and is given by

$\ln n!\approx n\ln n-n+{\tfrac {1}{6}}\ln(8n^{3}+4n^{2}+n+{\tfrac {1}{30}})+{\tfrac {1}{2}}\ln \pi .$ The approximation may be made precise by giving paired upper and lower bounds; one such inequality is

${\sqrt {\pi }}\left({\frac {x}{e}}\right)^{x}\left(8x^{3}+4x^{2}+x+{\frac {1}{100}}\right)^{1/6}<\Gamma (1+x)<{\sqrt {\pi }}\left({\frac {x}{e}}\right)^{x}\left(8x^{3}+4x^{2}+x+{\frac {1}{30}}\right)^{1/6}.$ ## Estimating central effect in the binomial distribution

In computer science, especially in the context of randomized algorithms, it is common to generate random bit vectors that are powers of two in length. Many algorithms producing and consuming these bit vectors are sensitive to the population count of the bit vectors generated, or of the Manhattan distance between two such vectors. Often of particular interest is the density of "fair" vectors, where the population count of an n-bit vector is exactly $n/2$ . This amounts to the probability that an iterated coin toss over many trials leads to a tie game.

Stirling's approximation to ${n \choose n/2}$ , the central and maximal binomial coefficient of the binomial distribution, simplifies especially nicely where $n$ takes the form of $4^{k}$ , for an integer $k$ . Here we are interested in how the density of the central population count is diminished compared to $2^{n}$ , deriving the last form in decibel attenuation:

{\begin{aligned}\log _{2}{n \choose n/2}-n&=-k-{\frac {\log _{2}(\pi )-1}{2}}+O(\log _{2}n)\\&\approx -k-0.3257481\\&\approx -k-{\frac {1}{3}}\\&\approx \mathbf {3k+1} ~~dB~(attenuation)\end{aligned}} This simple approximation exhibits surprising accuracy:

{\begin{aligned}10\log _{10}(2^{-1024}{1024 \choose 512})&\approx -16.033159~~~{\begin{cases}k&=5\\n=4^{k}&=1024\\3k+1&=\mathbf {16} \\\end{cases}}\\10\log _{10}(2^{-1048576}{1048576 \choose 524288})&\approx -31.083600~~~{\begin{cases}k&=10\\n=4^{k}&=1048576\\3k+1&=\mathbf {31} \\\end{cases}}\end{aligned}} Binary diminishment obtains from dB on dividing by $10\log(2)/\log(10)\approx 3.0103\approx 3$ .

As a direct fractional estimate:

{\begin{aligned}{n \choose n/2}/2^{n}&=2^{{\frac {1-\log _{2}(\pi )}{2}}-k}+O(n)\\&\approx {\sqrt {\frac {2}{\pi }}}~2^{-k}\\&\approx 0.7978846~2^{-k}\\&\approx \mathbf {{\frac {4}{5}}2^{-k}} \end{aligned}} Once again, both examples exhibit accuracy easily besting 1%:

{\begin{aligned}{256 \choose 128}2^{-256}&\approx 20.072619^{-1}~~~{\begin{cases}k&=4\\n=4^{k}&=256\\{\frac {4}{5}}\times {\frac {1}{2^{4}}}&=\mathbf {20} ^{-1}\\\end{cases}}\\{1048576 \choose 524288}2^{-1048576}&\approx 1283.3940^{-1}~~~{\begin{cases}k&=10\\n=4^{k}&=1048576\\{\frac {4}{5}}\times {\frac {1}{2^{10}}}&=\mathbf {1280} ^{-1}\end{cases}}\end{aligned}} Interpreted at an iterated coin toss, a session involving slightly over a million coin flips (a binary million) has one chance in roughly 1300 of ending in a draw.

Both of these approximations (one in log space, the other in linear space) are simple enough for many software developers to obtain the estimate mentally, with exceptional accuracy by the standards of mental estimates.

The binomial distribution closely approximates the normal distribution for large $n$ , so these estimates based on Stirling's approximation also relate to the peak value of the probability mass function for large $n$ and $p=0.5$ , as specified for the following distribution: ${\mathcal {N}}(np,\,np(1-p))$ .

## History

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

$n!\sim [{\rm {constant}}]\cdot n^{n+{\frac {1}{2}}}e^{-n}.$ 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 ${\sqrt {2\pi }}$ .