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?
Intuitive reason for an approximation to $x!$
372 Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 2 best solutions below
On
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)$$
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.