Ad Space — Top Banner

Stirling Approximation Calculator

Approximate n! for large n using Stirling formula and its refinement.
Compare exact vs approximate factorials and see relative error for any input.

Stirling Approximation

Stirling Approximation

For large n, computing n! by direct multiplication is slow and overflows quickly. Stirling’s formula gives a remarkably accurate continuous approximation that grows polynomially in cost rather than linearly.

Standard Form

n! ≈ √(2πn) × (n / e)^n

Refined Form (with correction)

n! ≈ √(2πn) × (n / e)^n × (1 + 1/(12n) + 1/(288n²) − 139/(51840n³))

The corrections are the first terms of an asymptotic series — adding more terms improves accuracy until the series eventually diverges (asymptotic series rarely converge in the classical sense).

Logarithmic Form (most useful in practice)

ln(n!) ≈ n × ln(n) − n + ½ × ln(2πn)

This avoids the overflow problem entirely — works for n in the millions on a calculator and for n in the billions in numerical software.

Worked Example — n = 10

  • Exact: 10! = 3,628,800
  • Standard Stirling: √(20π) × (10/e)¹⁰ ≈ 3,598,696 (error 0.83%)
  • Refined Stirling (1/12n term): ≈ 3,628,810 (error 0.0003%)

Even at modest n the refined form is essentially exact.

How Accurate Is Stirling?

n Standard Error Refined Error
1 7.9% 0.13%
5 1.6% 0.01%
10 0.83% 0.003%
100 0.083% 3×10⁻⁵ %
1000 0.0083% tiny

Relative error of standard Stirling falls like 1/(12n) — halving the error roughly each time you double n.

Why It Matters

Field Use
Statistical mechanics Entropy of N-particle systems
Information theory Asymptotics of binomial coefficients
Probability Central limit theorem proofs
Combinatorics Counting permutations and partitions
Numerical computing Avoiding factorial overflow

Connection to the Gamma Function

For non-integer arguments:

Γ(z+1) ≈ √(2π/z) × (z/e)^z × refinement(z)

This extends Stirling smoothly to all positive real and complex z (not on the negative real axis). The result is a key tool in physics and analytic number theory.

Caveat

Stirling is an asymptotic approximation: it improves as n grows, but for tiny n (say n = 0 or 1) the error is significant. Use exact factorial up to about n = 20 in most languages, then switch to Stirling or to a logarithmic Lanczos approximation for larger arguments. For very large n, work in log space — even Stirling’s value overflows IEEE doubles past about n = 170.


Ad Space — Bottom Banner

Embed This Calculator

Copy the code below and paste it into your website or blog.
The calculator will work directly on your page.