Gives n! asymptotically as sqrt(2 pi n) (n/e)^n, which yields the log n! = Theta(n log n) bound.
Stirling's approximation says that the factorialn!∼2πn(n/e)n: the ratio of the two sides tends to 1 as n→∞. It turns the product n! into a closed form that you can compare with other functions by asymptotic notation. Its weaker consequence, lg(n!)=nlgn−nlge+O(logn), gives the bound Ω(nlogn) for comparison sorting.
Statement
For each integer n≥1 there is a real number rn such that
n!=2πn(en)nern,12n+11<rn<12n1.
The upper bound and the positivity of rn are proved below. The lower bound 12n+11<rn is Robbins' (1955). So n!∼2πn(n/e)n in the sense of tilde notation, and lnn!=nlnn−n+21lnn+21ln(2π)+rn. The same asymptotic holds for Γ(x+1) as x→∞ through the reals.
The weaker form has a short proof. For each integer n≥1,
(en)n≤n!≤en(en)n,
and so nlnn−n≤lnn!≤nlnn−n+21lnn+1. In base 2, with lge=1.4427…, we get nlgn−nlge≤lgn!≤nlgn−nlge+21lgn+lge.
In plain words
The number n! is the product of the first n whole numbers. It grows very fast, and the product has no simple form. Stirling's formula gives a simple expression that is close to it. The expression is about the number of items raised to its own power, divided by a power of a fixed constant, times a factor that grows only like the square root of the number.
The formula has two uses. A rough use only needs the size of the logarithm of the product: it is about the number of items times the logarithm of the number of items. This is what the lower bound for sorting needs. A sharp use needs the correction factor too. It says, for example, how many ways there are to choose half of the items, and it does so with a relative error that shrinks as the number of items grows.
Proof
Weak form. We prove (n/e)n≤n!≤en(n/e)n for each integer n≥1.
Lower bound. The series en=∑k≥0nk/k! has positive terms, so en≥nn/n!. Hence n!≥nne−n=(n/e)n.
Upper bound. The function ln is concave on (0,∞), so on [k,k+1] it lies above its chord: lnx≥lnk+(x−k)(ln(k+1)−lnk). Integrating over [k,k+1] gives ∫kk+1lnxdx≥21(lnk+ln(k+1)). Add this for k=1,…,n−1:
where we used ln1=0. The integral is nlnn−n+1. So lnn!≤nlnn−n+1+21lnn, which is the upper bound. □
This is the technique of integral bounds for sums. It shows that lnn!=nlnn−n+O(logn), with the error between 0 and 1+21lnn.
Sharp form. The proof that rn exists, that 0<rn<12n1, and that the constant is 2π is in the proof of the sharp form. It shows that an=lnn!−(n+21)lnn+n decreases, and that an−12n1 increases, so both converge to the same limit. It finds the limit with the Wallis integrals ∫0π/2sinmxdx. The lower bound rn>12n+11 is Robbins' result (1955), and the page does not prove it.
Consequences
The sorting lower bound. A comparison sort on n distinct keys needs a decision tree with at least n! leaves, so its worst case is at least ⌈lgn!⌉ comparisons, see the comparison sorting lower bound. By the weak form, ⌈lgn!⌉≥nlgn−nlge≥nlgn−1.4427n. So the bound is Ω(nlogn). The sharp form gives lgn!=nlgn−nlge+21lgn+21lg(2π)+O(1/n), with 21lg(2π)=1.3257….
The table shows the exact value of lgn!, the bound nlgn−nlge, and the worst-case comparison count n⌈lgn⌉−2⌈lgn⌉+1 of merge sort.
n
lgn!
⌈lgn!⌉
nlgn−nlge
merge sort, worst case
10
21.79
22
18.79
25
100
524.76
525
520.12
573
1000
8529.40
8530
8523.09
8977
106
18488884.82
18488885
18488873.53
18951425
At n=106 merge sort is about 2.5 percent above the lower bound. The gap between lgn! and nlgn−nlge is 21lgn+1.3257+o(1): it is 6.3 at n=1000 and 11.3 at n=106.
lgn!=Θ(nlogn). The upper bound is lgn!≤nlgn. For the lower bound, nlgn−nlge≥21nlgn exactly when lgn≥2lge, that is n≥e2=7.39…. So 21nlgn≤lgn!≤nlgn for each n≥8. The simpler argument for this fact, which does not need Stirling's formula, is on the page of asymptotic notation.
The central binomial coefficient. Step 6 and Step 7 of the sharp proof give (n2n)∼4n/πn. For n=10 this is 187079 against the true value 184756. See also binomial coefficient bounds.
Orders of growth. Since n!≥(n/e)n and (n/e)n/cn=(n/(ec))n→∞ for each constant c, the factorial grows faster than each exponential cn. Since nn/n!∼en/2πn→∞, it also grows slower than nn. So n!=o(nn), and n! is not Θ(nn). For n=10, nn/n!=2755.7 and en/2πn=2778.8.
Tightness
The constant 2π and the exponent 21 of n cannot change: they are the exact limit. The weak upper bound is off by a bounded factor, but the weak lower bound is not. Its upper bound en(n/e)n exceeds the truth by the factor e/2π=1.0844… in the limit, and its lower bound (n/e)n is short by the unbounded factor 2πn. So the error term O(logn) in lnn!=nlnn−n+O(logn) cannot be improved to O(1): the true error is 21lnn+0.9189…+O(1/n).
The error exponent rn is ∼12n1, by the two-sided bound in the statement. Finer terms are known from the Stirling series:
n!∼2πn(en)n(1+12n1+288n21−51840n3139+⋯).
For n=10 the factor 2πn(n/e)n is 3598695.6, and 10!=3628800. With one, two and three correction terms the value is 3628684.7, 3628809.7 and 3628800.05. The series is asymptotic and does not converge for any n.
Two warnings. First, ∼ does not mean that the difference is small: n!−2πn(n/e)n is about 2πn(n/e)n/(12n), which grows without bound. Second, lnn!∼nlnn does not give n!∼nn. Taking a logarithm hides a factor of size e−n, as the paragraph on orders of growth shows.
Proof: The sharp form of Stirling's formula, with the constant and the error boundsfull
Statement. There is a real sequence (rn)n≥1 with 0<rn<12n1 for each n≥1, such that
n!=2πn(en)nern.
In particular n!∼2πn(n/e)n. The sharper lower bound rn>12n+11 is Robbins' theorem and is not proved here.
Step 1: a convergent sequence. For n≥1 let
an=lnn!−(n+21)lnn+n,so thatn!=eann(n/e)n.
Then
an−an+1=(n+21)lnnn+1−1.
To see this, expand an+1 and use ln(n+1)!=lnn!+ln(n+1): the terms lnn! cancel, and what remains is −ln(n+1)+(n+23)ln(n+1)−(n+21)lnn+n−(n+1), which is the right side.
Step 2: a series for the difference. Put t=2n+11, so 0<t<1. Then 1−t1+t=2n2n+2=nn+1 and n+21=2t1. For 0≤t<1 we have ln1−t1+t=2∑j≥02j+1t2j+1. So
an−an+1=2t1⋅2j≥0∑2j+1t2j+1−1=j≥1∑2j+1t2j.
Step 3: two-sided bound on the difference. Every term of the series is positive, so an−an+1>0. Also 2j+11≤31 for j≥1, with strict inequality for j≥2, so
an−an+1<31j≥1∑t2j=3(1−t2)t2.
Since t−2−1=(2n+1)2−1=4n(n+1), we get 1−t2t2=4n(n+1)1. Hence
0<an−an+1<12n(n+1)1=12n1−12(n+1)1.
Step 4: the limit and the bounds on rn. Step 3 says two things. First, (an) is strictly decreasing. Second, an−12n1<an+1−12(n+1)1, so bn=an−12n1 is strictly increasing. Also bn<an≤a1 for each n, so (bn) is increasing and bounded above. It converges to a limit L. Since an=bn+12n1 and 12n1→0, the sequence (an) also converges to L. A strictly increasing sequence is smaller than its limit, and a strictly decreasing sequence is larger than its limit. So for each n≥1,
L<anandan−12n1<L.
Let rn=an−L. Then 0<rn<12n1, and n!=eLernn(n/e)n. Put K=eL. It remains to show K=2π.
Step 5: Wallis integrals. For integers m≥0 let Im=∫0π/2sinmxdx. Then I0=π/2 and I1=1. For m≥2 write sinmx=sinm−1x⋅sinx and integrate by parts: Im=(m−1)∫0π/2sinm−2xcos2xdx=(m−1)(Im−2−Im), because the boundary term −sinm−1xcosx is 0 at both ends. So Im=mm−1Im−2. Since 0≤sinx≤1 on [0,π/2], the sequence (Im) is non-increasing. By induction on m, ImIm+1=2(m+1)π: for m=0 the product is π/2, and if it holds for m, then Im+1Im+2=m+2m+1Im+1Im=2(m+2)π.
For n≥1 the monotonicity gives I2n+1≤I2n≤I2n−1. Multiply by I2n>0:
2(2n+1)π≤I2n2≤4nπ.
Multiply by 4n/π: the left side becomes 2n+12n and the right side becomes 1. Both tend to 1, so I2n2∼4nπ, that is I2n∼21π/n.
Step 6: the central binomial coefficient. The recurrence gives I2n=2π∏j=1n2j2j−1. Now ∏j=1n(2j−1)=2nn!(2n)! and ∏j=1n2j=2nn!, so I2n=2π⋅4n(n!)2(2n)!=2π(n2n)4−n. With Step 5,
(n2n)4−n=π2I2n∼π2⋅2nπ=πn1.
Step 7: the constant. By Step 4, n!∼Kn(n/e)n. So (2n)!∼K2n(2n/e)2n and (n!)2∼K2n(n/e)2n. Dividing,
(n2n)∼K2nn2nK2n(2n)2n=Kn24n.
Compare with Step 6: Kn2∼πn1, so K=2π. Then n!=2πn(n/e)nern with 0<rn<12n1, and ern→1. ■
Remark. The lower bound rn>12n+11 needs a finer estimate of the series of Step 2. Robbins gives it in the paper cited on the page. The proof above gives the upper bound 12n1 and the positivity of rn.
The formula is named for James Stirling, who gave it with the constant 2π in his book Methodus Differentialis (1730). Abraham de Moivre found the form n!∼Cn(n/e)n with the constant C only as a numerical series (Miscellanea Analytica, 1730). Stirling showed that C=2π. Robbins proved the two-sided bound 12n+11<rn<12n1 in 1955. Knuth, Volume 1, Section 1.2.11.2, and Graham, Knuth and Patashnik, Chapter 9, derive the formula with the Euler summation formula, and they give the series.