首页 > AI前沿 > How big are factorials?

How big are factorials?

Hacker News 2026-09-15 21:25 2 阅读 查看原文
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 e is... 20-ish? And \log_{10}(20) is about 1.3 [1]; 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 n>0 is defined [2] 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 \Gamma(n+1) is: And now use integration by parts with: Then: So: But notice that the last integral is just \Gamma(n); therefore, we’ve shown that: Let’s also calculate \Gamma(1) - 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 x=ny, which means that dx=ndy: 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 [3] to the latest equation we have for n! (renaming the dummy integration variable back to x): In our case, f(x)=\ln x - x. 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 n! 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 [4] For comments, please send me an email.