Factorial Approximations

n! (and its logarithm) keep showing up in the analysis of algorithm. Unfortunately, it’s very often unwieldy, and we use approximations of n! (or \log n!) to simplify things. Let’s examine a few!

First, we have the most known of these approximations, the famous “Stirling formula”:

\displaystyle n!=\sqrt{2\pi{}n}\left(\frac{n}{e}\right)^n\left(1+\frac{1}{12n}+\frac{1}{288n^2}-\frac{139}{51840n^3}-\cdots\right),

Where the terms at the right are known as Stirling Series (the numerators are given by A046968 and the denominators by A046969). If you evaluate the complete series, it’s truly equal to n!.

However, you may not quite want to evaluate an infinite series, and we find truncated versions:

\displaystyle n!\approx\sqrt{2\pi{}n}\left(\frac{n}{e}\right)^n

that we will call “Stirling” from now on; a “Stirling more” version could be


or even a “Stirling most”:

\displaystyle n!\approx\sqrt{2\pi{}n}\left(\frac{n}{e}\right)^n\left(1+\frac{1}{12n}+\frac{1}{288n^2}\right).

The literature is fraught with approximations. For example, we find Gosper’s:

\displaystyle n!\approx\sqrt{2\pi\left(n+\frac{1}{6}\right)}\left(\frac{n}{e}\right)^n.

Everybody refers [a] as the source of this approximation. However, while it can be a consequence of that paper, it’s not in it (also, its typography is a train wreck).

We have Burnside’s [2]:

\displaystyle n!\approx\sqrt{2\pi}\left(\frac{n+\frac{1}2{2}}{e}\right)^{n+\frac{1}{2}}.

Then Mortici’s [3]:

\displaystyle n!\approx\sqrt{\frac{2\pi}{e}}\left(\frac{n+1}{e}\right)^{n+\frac{1}{2}}.

There are plenty more, but let’s consider one more. Mohanty and Rummens’ [4]:

\displaystyle n!\approx\sqrt{2\pi}(n+1)^{n+\frac{1}{2}}e^{\frac{1}{12(n+1)}-(n+1)}.

* *

How do those compare? Asymptotically, when n is very large, the ratio of any of these approximation to n! goes to 1. On smaller n, they also all kind-of-work OK:

From the figure above, we see that Gosper’s does very well, just a bit worst than “Sterling More”. Mohanty and Rumme1ns’ does best, but it’s also quite a bit more complex than Gosper’s. What if we have a look at the digits that are output? The following shows the result (in Mathematica, which computes in “infinite precision”) rounded:

But what’s more telling, is the ratio to the real value:

But we can also have a look at the number of correct leading digits:

* *

So Gosper’s approximation is much better than just the truncated Stirling and compares to “Stirling More”, which is not that surprising because it’s very close to what you get by distributing the 1/12n into the square root; so it’s a good numerical trade off. However, “Stirling More” and Mohanty & Rummens’ compare with a slight advantage to the later.

[1] R. William Gosper, Jr. — Decision Procedure for Indefinite Hypergeometric Summation — Procs. Nat. Acad. Science USA, vol 75 no 1 (1978) p. 42–46.

[2] W. Burnside — A Rapidly Convergent Series for Log N! — Messenger Math., vol 46 no ? (1917) p. 157–159

[3] Cristinel Mortici — An Ultimate Extremely Accurate Formula for Approximation of the Factorial Function — Archiv der Mathematik, vol 93 (2009) p. 37–45

[4] S. Mohanty, F. H. A. Rummens — Comment on “An Improved Analytical Approximation to n!” — J. Chem. Physics, vol 80 (1984) p. 591.

3 Responses to Factorial Approximations

  1. paul watson says:

    Fabulous summary of important topic to probability. Thank you!

  2. Arin Chaudhuri says:

    Should sqrt(2n * (n+1/6)) (n/e)^n be sqrt(2pi * (n+1/6)) (n/e)^n in Gosper’s formula.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: