The other day, I found myself wondering how big 52! (52 factorial) is, and that led me to ponder how these could be estimated without a calculator or a computer.
It turns out there’s some fairly interesting math behind being able to estimate the size (number of digits) of a factorial reasonably accurately. This post will start by stating how to do the estimate, and if you’re curious you can read on for the math background.
Without further ado, the approximation is:
As an example, let’s use my original question, by estimating this for 52!
Well, 52 divided by is... 20-ish? And is about 1.3 ; therefore our estimate comes out to:
The real answer is 68, so this is very close! In estimates like this - when you’re dealing with enormous numbers - being off by a couple of digits usually isn't a big deal.
The Gamma function
The Gamma function for real is defined as:
This integral does not have an analytic expression in the general case, but it does have a very useful property that we can take advantage of. Let’s see what is:
And now use integration by parts with:
Then:
So:
But notice that the last integral is just ; therefore, we’ve shown that:
Let’s also calculate - it’s a special case that has an analytical solution:
This helps establish an induction argument:
In other words - the Gamma function is an interpolation of the factorial over all positive reals. Here’s a plot of the Gamma function over a small range; note that the y axis is log-scale because of the function’s fast growth:
Stirling’s approximation
You may have encountered Stirling’s approximation before:
It’s a great approximation that works reasonably well even for small
. This section is a brief overview of how Stirling’s formula is
derived from the Gamma function.
Taking:
We’ll start by massaging the integrand a bit:
And making a change of variables , which means that :
These steps make the integral amenable to applying Laplace’s method, which allows us to approximate definite integrals of the form:
Where
is a twice-differentiable function and
some
large number. By Laplace’s method, such integrals can be approximated
by:
Where
is the global maximum of
.
Let’s see how to apply this method to the latest equation we have for (renaming the dummy integration variable back to ):
In our case, . It’s easy to show that this
function is twice differentiable and has a global maximum at
. Moreover:
Substituting these into the proper places in Laplace’s approximation, we get:
Number of digits from Stirling’s approximation
We can calculate the number of digits in by taking the base-10 logarithm of Stirling’s formula:
Note that the first term is not multiplied by
itself;
therefore, as
grows, it will become less and less noticeable.
That said, it still adds a couple of digits - so you should take it into
account if you want a more accurate approximation