Preface #
The derivation of Stirling’s formula , which is widely used in the field of statistical mechanics, is presented in this blog for future reference. Before jumping into the Stirling’s formula, two important concepts, the Gamma function and the Gaussian integral , are introduced first.
Gamma Function #
The Gamma function is a continuous extension of the factorial function from (only) positive integers to real and complex numbers. The definition is given by
$$\Gamma(x) = \int_0^\infty t^{x-1}e^{-t}\,\mathrm{d}t,\quad x>0. \tag{1} \label{eq1}$$
Its connection with the factorial can be derived using integration by parts. Specifically, let
$$u = t^{x-1},\quad v = -e^{-t},$$
then we have
$$\mathrm{d}u = (x-1)t^{x-2}\mathrm{d}t,\quad \mathrm{d}v = e^{-t}\mathrm{d}t.$$
Equation $\eqref{eq1}$ becomes
\begin{align} \Gamma(x) &= \int_0^\infty u\mathrm{d}v\\ &= \left(uv\right)_0^\infty-\int_0^\infty v\mathrm{d}u\\ &= (x-1)\int_0^\infty t^{x-2}e^{-t}\mathrm{d}t\\ &= (x-1)\Gamma(x-1)\\ &= \cdots =(x-1)!. \end{align}
In this way, the computation of factorial $n!$ is generalized beyond just positve integers, while the Gamma function also allows efficient numerical evaluation without explicitly performing $n$ successive multiplications.
Gaussian Integral #
The Gaussian integral is the integral of the Gaussian function over the entire real line:
$$I = \int_{-\infty}^\infty e^{-x^2}\,\mathrm{d}x.$$
To compute its value, we square the above integral:
$$I^2 = \int_{-\infty}^\infty\int_{-\infty}^\infty e^{-(x^2+y^2)}\,\mathrm{d}x\mathrm{d}y.$$
Let $x = r\cos\theta,\ y = r\sin\theta$, we have
$$I^2 = \int_0^{2\pi}\int_0^\infty e^{-r^2}r\,\mathrm{d}r\mathrm{d}\theta = \pi.$$
Therefore,
$$I = \int_{-\infty}^\infty e^{-x^2}\,\mathrm{d}x = \sqrt{\pi}. \tag{2} \label{eq2}$$
Its connection with the Gamma function can be seen in the following equation:
\begin{align} \Gamma\left(\frac{1}{2}\right) &= \int_0^\infty t^{-\frac{1}{2}}e^{-t}\,\mathrm{d}t\\ &= 2\int_0^\infty e^{-u^2}\,\mathrm{d}u,\quad u = \sqrt{t}\\ &= \int_0^\infty e^{-u^2}\,\mathrm{d}u + \int_0^{-\infty} e^{-u^2}\,\mathrm{d}(-u)=I. \end{align}
Stirling’s Formula #
By applying the Gamma function, we obtain
$$\Gamma(n+1) = \int_0^\infty t^ne^{-t}\,\mathrm{d}t = \int_0^\infty e^{n\ln x-x}\,\mathrm{d}x = n!. \tag{3} \label{eq3}$$
Let
$$f(x) = n\ln x-x,$$
which peaks at $x=n$, thus, $f^\prime(n) = 0$. Consider its (second-order) Taylor expansion around $x=n$:
$$f(x)\approx f(n)+\frac{1}{2}f^{\prime\prime}(n)(x-n)^2 = n\ln n-n-\frac{(x-n)^2}{2n}. \tag{4} \label{eq4}$$
Substituting Equation $\eqref{eq4}$ into Equation $\eqref{eq3}$ yields
$$n! \approx e^{n\ln n-n}\int_0^\infty e^{-\frac{(x-n)^2}{2n}}\,\mathrm{d}x. \tag{5} \label{eq5}$$
Let $z = (x-n)/\sqrt{2n}$, then
$$\int_0^\infty e^{-\frac{(x-n)^2}{2n}}\,\mathrm{d}x = \sqrt{2n}\int_{-\sqrt{n/2}}^\infty e^{-z^2}\,\mathrm{d}z\approx \sqrt{2\pi n},$$
where the last approximation holds true for $\mathbf{n\gg 1}$. In this way, we obtain the Stirling’s approximation from Equation $\eqref{eq5}$:
$$\boxed{n! \approx \left(\frac{n}{e}\right)^n\sqrt{2\pi n}\approx\left(\frac{n}{e}\right)^n.}$$
Stirling’s formula suggests that the “average” factor in the factorial $n!$ is roughly $\frac{n}{e}$. A more widely used logarithmic form is
$$\ln n!\approx n\ln n-n.$$
Last modified on 2026-08-16