Mathematics · Applied Mathematics
Stirling's Approximation Calculator
Computes Stirling's approximation of n! and compares it against the exact factorial value to show relative error.
Calculator
Formula
Here n is the non-negative integer whose factorial is being approximated, \pi \approx 3.14159 is pi, and e \approx 2.71828 is Euler's number. The natural-log form is \ln(n!) \approx n\ln(n) - n + \tfrac{1}{2}\ln(2\pi n), which is numerically stable for very large n.
Source: James Stirling, Methodus Differentialis (1730); also Abramowitz & Stegun, §6.1.
How it works
Computing exact factorials is straightforward for small integers, but n! grows super-exponentially: 20! already exceeds 2 × 10¹⁸, and beyond n = 170 double-precision floating-point arithmetic overflows entirely. Stirling's approximation sidesteps this by expressing n! in terms of elementary functions that remain computable for arbitrarily large n.
The standard form is n! ≈ √(2πn) · (n/e)^n. Taking logarithms yields the numerically robust version ln(n!) ≈ n·ln(n) − n + ½·ln(2πn). This log-form is preferred in practice because it avoids overflow: working in log-space, probabilities and combinatorial coefficients can be computed as sums and differences of logarithms rather than products of astronomically large numbers. A refined version adds correction terms — the Stirling series — giving ln(n!) ≈ n·ln(n) − n + ½·ln(2πn) + 1/(12n) − 1/(360n³) + …, which converges asymptotically but not absolutely.
Applications span an enormous range: the Boltzmann entropy formula S = k·ln(W) requires ln(N!) where N is on the order of Avogadro's number; the central limit theorem proof uses Stirling to approximate binomial coefficients; machine learning practitioners use it to evaluate log-likelihoods of multinomial models; and number theorists invoke it when studying the prime counting function and the Riemann hypothesis. Chemists use it to count microstates in statistical thermodynamics, while information theorists use it to compute channel capacities.
Worked example
Problem: Estimate 10! using Stirling's approximation and compare to the exact value.
Step 1 — Exact factorial: 10! = 10 × 9 × 8 × … × 1 = 3,628,800.
Step 2 — Apply the formula: n = 10, so we compute √(2π × 10) × (10/e)^10.
First, √(20π) = √(62.8318…) ≈ 7.9267.
Next, 10/e = 10/2.71828… ≈ 3.6788. Raising to the 10th power: 3.6788^10 ≈ 457,323.5.
Step 3 — Multiply: 7.9267 × 457,323.5 ≈ 3,622,467.
Step 4 — Relative error: |3,628,800 − 3,622,467| / 3,628,800 × 100 ≈ 0.174%. Even at n = 10 the approximation is already within 0.2% of the true value.
Log-space check: ln(10!) = ln(3,628,800) ≈ 15.10441. Stirling log form: 10·ln(10) − 10 + ½·ln(20π) = 23.02585 − 10 + 2.07944 = 15.10429. The error in log-space is only about 0.00012, confirming excellent accuracy.
Limitations & notes
Stirling's approximation is an asymptotic formula, not a convergent series. For small n (particularly n ≤ 5), the relative error can exceed 1–2%, so exact factorials should be used when precision matters at small values. The basic two-term formula n! ≈ √(2πn)·(n/e)^n always underestimates n!, because the omitted correction terms are positive. Adding the first correction term, multiplying by e^(1/(12n)), dramatically improves accuracy. For n = 1 the error is about 8.3%; by n = 10 it falls to 0.83% for the basic form and near 0.001% with the first correction term. In floating-point arithmetic the direct formula overflows for n > 170 even before any approximation is made, so the log-form ln(n!) ≈ n·ln(n) − n + ½·ln(2πn) should always be preferred for large n. The Stirling series itself is divergent — adding too many correction terms eventually increases error — so truncate at the first neglected term that starts growing.
Frequently asked questions
How accurate is Stirling's approximation?
The relative error of the basic formula is approximately 1/(12n), so at n = 10 it is about 0.83%, at n = 100 about 0.083%, and at n = 1000 about 0.0083%. Adding the correction factor e^(1/(12n)) reduces this to roughly 1/(360n³), giving extraordinary accuracy even for moderate n.
Does Stirling's approximation overestimate or underestimate n!?
The basic form √(2πn)·(n/e)^n always underestimates n! because the first omitted correction term 1/(12n) is positive. The corrected form √(2πn)·(n/e)^n·e^(1/(12n)) overestimates slightly, and successive correction terms alternately over- and under-estimate.
What is the log form of Stirling's approximation and when should I use it?
The log form is ln(n!) ≈ n·ln(n) − n + ½·ln(2πn). Use it whenever n is large enough that n! would overflow a standard floating-point number (n > 170 in double precision) or when you need to compute ratios of factorials, binomial coefficients, or log-likelihoods numerically without overflow.
Why does statistical mechanics use Stirling's approximation so heavily?
In statistical thermodynamics, the Boltzmann entropy S = k·ln(W) requires computing ln(N!) where N ≈ 6 × 10²³. No computer can store this integer exactly, but Stirling's log form gives ln(N!) ≈ N·ln(N) − N, which is trivially computable. This simplification is foundational to deriving the Maxwell–Boltzmann, Fermi–Dirac, and Bose–Einstein distributions.
Can Stirling's approximation be used for non-integer arguments?
The standard integer-only formula extends naturally through the Gamma function: n! = Γ(n+1), and ln Γ(z) ≈ (z−½)·ln(z) − z + ½·ln(2π) for large |z|. This generalization is used in Bayesian statistics, special function computation, and complex analysis, but requires careful treatment of the branch cut for complex arguments.
Last updated: 2025-01-15 · Formula verified against primary sources.