Intuitive reason for an approximation to $x!$

I was working today on approximating $$x!$$ using the integral of $$\ln x$$, and using computer software to calculate limits, I found that $$\dfrac{(12x)(\sqrt{2\pi x})(x^x)}{(12x-1)e^x}$$ gives a very close approximation of $$x!$$. Multiplying the figure by $$\dfrac{288x^2}{288x^2+1}$$ seems to render the approximation even closer, and there are obviously further terms one could add as well. What I'm curious about is the role of the $$\sqrt{2\pi x}$$ term; I knew there would be a (hopefully asymptotic) error term in the product, but $$\sqrt{2\pi}$$ had never crossed my mind as a possibility. Is there an intuitive reason that this term appears here?

• You seem to be rediscovering the Stirling-Laplace asymptotics! Good job! Yes, it is not obvious that various "goofy" artifactual constants would appear, but, yes, they do. :) Good stuff! More later... :) Feb 17, 2022 at 2:13
• Could you elaborate more regarding what steps you took to discover this?
– Snaw
Feb 17, 2022 at 2:18
• @Snaw I don’t have the paper I used anymore, but I remember something along the lines of $(x+1)\ln(x+1) - (x+1) - x\ln x + x$ = $\ln(x+1) + x\ln(1+\frac{1}{x}) - 1$, where $\ln(x+1)$ is the “desired” increase. Using a rough approximation of $x\ln(1+\frac{1}{x}) - 1 = x(\ln(x+1)-\ln x)-1$ as $x(\frac{\frac{1}{x}+\frac{1}{x+1}}{2})-1 = \frac{1}{2(x+1)}$, I got half of the harmonic series, which is asymptotic to half of $\ln(x+1)$, yielding $\sqrt x$ in the final result. Since this is an approximation, I knew there would be some error, and this is where the computer gave $\sqrt{2\pi}$. Feb 17, 2022 at 12:49
• Thanks! Nice stuff.
– Snaw
Feb 17, 2022 at 16:34

This is the well-studied Stirling's formula, which is commonly written $$n!\sim\sqrt{2\pi n}\left(\frac{n}{e}\right)^n\left(1+\frac{1}{12n}+\frac{1}{288n^2}+\dots\right)\tag{1}$$ The general term here has no nice formula (in particular, it does not match $$e^{-\frac{1}{12n}}$$). On the other hand, Carmeister in comments below points out that there is a nice recursive algorithm — see equation 5.11.6 in the NIST Handbook of Mathematical Functions.

Importantly, (1) is a scheme to produce a sequence of asymptotic approximations, not an infinite series for $$n!$$; if you were to include all the terms in "$$\dots$$", your sum would diverge. Instead, cut off (1) at the $$k$$th term; for large enough $$n$$, the resulting formula has error proportional to the next term. But the $$n$$ at which this "kicks in" increases as you increase $$k$$.

Wikipedia gives a short derivation; essentially the reason $$\sqrt{2\pi n}$$ appears is that the formula approximates $$n!=\int_0^{\infty}{e^{n(\ln{\!(t)}-t)}\,dt}$$ as $$n!\sim\int_{-\infty}^{\infty}{e^{-(a_nx+b_n)^2}\,dt}$$ for some well-chosen $$a_n$$ and $$b_n$$. The integral of $$e^{-x^2}$$ involves $$\sqrt{2\pi}$$, in part because that integral can be computed using 2D areas.

• If you were to include all the terms in "...", your sum would diverge - I'm confused; isn't that sum the power series for $e^{1 \over 12n}$, which converges for all nonzero $n$? Or have I misunderstood what you're saying? Feb 17, 2022 at 11:52
• @psmears It's not the power series for $\exp(1/12n)$, it just happens that the first three terms agree with it. The next term is $-\frac{139}{51840n^3}$, more information is given on this page Feb 17, 2022 at 12:19
• @psmears: This is why presenting such asymptotic expansions without clearly specifying the limiting conditions is the worst. You can take the first $k$ terms in that expansion, and so you have two parameters $k$ and $n$. For fixed $k$ the approximation is asymptotically correct as $n→∞$. If you fix $n$ instead then you get nothing meaningful at all because taking $k→∞$ yields rubbish. Feb 17, 2022 at 12:25
• Ah OK - thanks folks! It would be worth adding a little more detail to the answer, partly because of the ambiguity of the expansion, but mostly because the Wikipedia article linked is rather unclear (eg there are several different expressions it calls "Stirling's Formula"!) Feb 17, 2022 at 13:44

Considering the ratio $$R=\frac{12x\,\sqrt{2\pi x}\,x^x}{(12x-1)\,e^x\, x!}$$ and using series for large values of $$x$$, we have$$R=1+\frac{1}{288 x^\color{red}{2}}+\frac{77}{25920 x^3}++O\left(\frac{1}{x^4}\right)$$ while

$$\frac{288x^2}{288x^2+1}=1-\frac{1}{288 x^2}+\frac{1}{82944 x^4}+O\left(\frac{1}{x^6}\right)$$ As you observed $$\frac{288 x^2}{288 x^2+1}\,R=1+\frac{77}{25920 x^\color{red}{3}}+O\left(\frac{1}{x^4}\right)$$ is much better.

Trying to improve it, I considered $$\frac {a_0+a_1x+a_2x^2} {b_0+b_1x+b_2x^2}\,R$$ which was expanded for large values of $$x$$.

This leads to $$\frac{129600 x^2-110880 x+94189}{129600 x^2-110880 x+94639}\,R=1+\frac{4000387}{2939328000 x^\color{red}{5}}+O\left(\frac{1}{x^6}\right)$$

It is amazing to see how a "minor" change can improve. Notice that $$\frac{129600 x^2-110880 x+94189}{129600 x^2-110880 x+94639}-\frac{288x^2}{288x^2+1}=-\frac{77}{25920 x^3}+O\left(\frac{1}{x^4}\right)$$

• I think these are examples of Pade approximations. Feb 18, 2022 at 7:16
• @martycohen. Yes, for sure. Thsi is a way to "improve" the approximations of factorials (Sirling, Ramanujan, ..) with fewer terms. Cheers :-) Feb 18, 2022 at 7:20