Stirling's Approximation

lean4-proofcombinatoricsvisualization
n!2πn(ne)nn! \sim \sqrt{2\pi n}\left(\frac{n}{e}\right)^n

Statement

Stirling's approximation: As nn \to \infty,

n!2πn(ne)nn! \sim \sqrt{2\pi n} \left(\frac{n}{e}\right)^n

More precisely, the ratio n!2πn(n/e)n1\frac{n!}{\sqrt{2\pi n}(n/e)^n} \to 1 as nn \to \infty.

The approximation is already tight for moderate nn and gives the correct exponential order and polynomial correction for quantities like the central binomial coefficient (2nn)4nπn\binom{2n}{n} \sim \frac{4^n}{\sqrt{\pi n}}.

Visualization

Numerical comparison: exact n!n! vs Stirling approximation S(n)=2πn(n/e)nS(n) = \sqrt{2\pi n}(n/e)^n and their ratio.

nnn!n!S(n)S(n) (approx)ratio n!/S(n)n!/S(n)
110.92211.0847
5120118.021.0168
1036288003598695.61.0083
202.432×10182.432 \times 10^{18}2.423×10182.423 \times 10^{18}1.0042

The ratio decreases monotonically toward 1. Robbins (1955) showed:

2πn(ne)ne1/(12n+1)<n!<2πn(ne)ne1/(12n)\sqrt{2\pi n}\left(\frac{n}{e}\right)^n e^{1/(12n+1)} < n! < \sqrt{2\pi n}\left(\frac{n}{e}\right)^n e^{1/(12n)}

Error magnitude. The multiplicative error is 1+112n+O(1/n2)1 + \frac{1}{12n} + O(1/n^2), so for n=10n = 10 the relative error is about 0.83%0.83\%.

Log version (useful for information theory and combinatorics):

ln(n!)=nlnnn+12ln(2πn)+O(1/n)\ln(n!) = n\ln n - n + \frac{1}{2}\ln(2\pi n) + O(1/n)

This is the starting point for entropy estimates and the analysis of random permutations.

Proof Sketch

  1. Wallis' product. Show k=1n(2k)(2k)(2k1)(2k+1)π2\prod_{k=1}^{n} \frac{(2k)(2k)}{(2k-1)(2k+1)} \to \frac{\pi}{2} as nn \to \infty (Wallis 1655).
  2. Define Stirling sequence. Let an=n!(n/e)nna_n = \frac{n!}{(n/e)^n \sqrt{n}}; show it is log-convex and decreasing.
  3. Identify the limit. The limit a=limana = \lim a_n satisfies π/2=a2/2\pi/2 = a^2/2 via Wallis, giving a=πa = \sqrt{\pi}.
  4. Reconstruct. n!2πn(n/e)n=an2ππ2π2=1\frac{n!}{\sqrt{2\pi n}(n/e)^n} = \frac{a_n}{\sqrt{2\pi}} \to \frac{\sqrt{\pi}}{\sqrt{2\pi}} \cdot \sqrt{2} = 1.

The Mathlib proof in Mathlib.Analysis.SpecialFunctions.Stirling follows exactly this route: Stirling.tendsto_stirlingSeq_sqrt_pi proves anπa_n \to \sqrt{\pi}, and Stirling.factorial_isEquivalent_stirling packages the asymptotic equivalence.

Connections

Stirling's approximation is the analytic foundation for estimates on Catalan NumbersCatalan NumbersCn=1n+1(2nn)C_n = \frac{1}{n+1}\binom{2n}{n}The ubiquitous sequence counting balanced parentheses, binary trees, and Dyck pathsRead more → (Cn4nn3/2πC_n \sim \frac{4^n}{n^{3/2}\sqrt{\pi}}) and Bell NumbersBell NumbersBn=k=0nS(n,k)B_n = \sum_{k=0}^{n} S(n, k)The total number of partitions of a set of n elementsRead more →. It connects to the Wallis product and Taylor's TheoremTaylor's Theoremf(x)=k=0nf(k)(x0)k!(xx0)k+Rn(x)f(x) = \sum_{k=0}^{n} \frac{f^{(k)}(x_0)}{k!}(x-x_0)^k + R_n(x)Approximate smooth functions by polynomials with explicit error boundsRead more → (the Stirling series is an asymptotic expansion). The log version is the combinatorial backbone of the Inclusion-Exclusion PrincipleInclusion-Exclusion PrincipleAB=A+BAB|A \cup B| = |A| + |B| - |A \cap B|The size of a union is the alternating sum of intersection sizesRead more → applied to derangements.

Lean4 Proof

import Mathlib.Analysis.SpecialFunctions.Stirling

/-- Stirling's formula: n! is asymptotically equivalent to sqrt(2*pi*n)*(n/e)^n.
    Mathlib's `Stirling.factorial_isEquivalent_stirling` states this. -/
theorem stirling_formula_alias :
    (fun n ↦ (n ! : )) ~[Filter.atTop]
      fun n ↦ Real.sqrt (2 * n * Real.pi) * (n / Real.exp 1) ^ n :=
  Stirling.factorial_isEquivalent_stirling

/-- Numeric sanity check: 10! = 3628800. -/
theorem factorial_10 : Nat.factorial 10 = 3628800 := by decide