There are quite a few known formulas for approximating factorials and the logarithms of factorials. At least two of these are named after James Stirling: the so-called Stirling approximation should probably be called the “first” Stirling approximation, since it can be seen as the first term in the Stirling series.
Let’s begin by deriving the basic Stirling formula.
We can always expand the logarithm of N! as:
In figure 1 and figure 2, we consider the example where N=8. In each figure, the blue area represents ln[(N−1)!]. The yellow area represents ln(N) so that the blue and yellow areas together represent ln(N!).
We can use figure 1 to obtain an upper bound on ln(N!):
Similarly, we can use figure 2 to obtain a lower bound on ln(N!):
This is useful because we can easily do the integral:
We can always subtract 1 from the lower bound to obtain a looser lower bound. Combining results, we have:
We now switch from obtaining bounds to obtaining estimates and approximations. Some interesting approximations include:
The low-low approximation can be obtained subtracting 1 from the lower bound to obtain a looser lower bound. It is sometimes called “the” Stirling approximation, but for small N it is pretty lame, and it is easy to do better. The middle approximation results from splitting the difference between the lower bound and the upper bound. The zeroth-order term in the first Stirling approximation is ½ln(2π) ≈ 0.918939. This may not seem much different from the 1 that appears in the middle approximation, but the first Stirling approximation performs noticeably better than the middle approximation. The other approximations in equation 6 are terrible by comparison ... although they are all the same to leading order, so the percentage error becomes small if N is sufficiently huge.
Other noteworthy approximations include the following. Gosper does better than first Stirling, with minimal complexity. Second Stirling does better still, with very little additional complexity.