Some websites and books give stirling approximation as
$$n! = \sqrt{2 \pi n} \left( \frac{n}{e} \right)^n \left( 1 + O \left(\frac{1}{n} \right)\right)$$
However when I check their derivations most do it analytically up to
$$n! \approx \sqrt{2 \pi n} \left(\frac{n}{e}\right)^n$$
So I tried proving the $$O\left(\frac {1}{n}\right) $$ term but to no avail.
Here are my steps, hopefully someone can point me in the right direction
First I began by defining
$$n!=\Gamma(n+1) = \int{e^{-x}x^{n} }dx $$
$$=\int{dxe^{nlnx-x}}$$
The maximum of the integrand is at $$x =n$$
Next, I will expand the powers by a taylor approximation about x=n
$$f(x) = -x+nlnx$$
$$f'(x) = -1 +nx^{-1}$$
$$f''(x) = -nx^{-2}$$
$$f^{3}(x)=2nx^{-3}$$
By letting $$ x = n+\epsilon$$
I get
$$f(n+\epsilon) = -n + nlnn + 0\epsilon + -\frac{1}{n^2}\epsilon^2 + \frac{2}{n^{2}\epsilon^3} $$
Plugging back into the integration
$$\Gamma(n+1) = n! = n^{n}e^{-n}\int{e^{-\frac{\epsilon^2}{2n}+\frac{\epsilon^3}{3n^2}+…}}d\epsilon +…$$
Normally if it were up to the second derivative term of the taylor's expansion, it would be a gaussian integral. But if there are more terms, how would I do it?
Or is there another approach?
Best Answer
The Euler-MacLaurin summation formula can be used to approximate $n!$ conveniently.
The following is taken from Concrete Mathematics by D.E. Knuth ad R.L. Graham. We find in section 9.5 among other representations this version of the Euler-MacLaurin summation formula for integer values $a\leq b$
with $B_k$ the Bernoulli numbers starting with \begin{align*} B_0=1,B_1=-\frac{1}{2},B_2=\frac{1}{6},B_4=-\frac{1}{30},B_6=\frac{1}{42},\ldots \end{align*} and $B_k=0$ for odd $k$ greater $1$.
It is also shown, that if the function $f$ has the nice property $$f^{2m}(x)\geq 0$$ for $a\leq x \leq b$ then it turns out that the remainder in (1) can be replaced with \begin{align*} \Theta_m\frac{B_{2m+2}}{(2m+2)!}\left.f^{(2m+1)}(x)\right|_a^b\qquad\qquad \text{for some }0<\Theta<1 \end{align*}
Now we have everything we need to estimate $n!$ as precise as we like:
Using the generating function of $\exp(x)=\sum_{n\geq 0}\frac{x^n}{n!}$ we obtain from (3) \begin{align*} n!&=\exp\left(n\ln n-n+\sqrt{2\pi}+\frac{1}{2}\ln n+\frac{1}{12n}-\frac{1}{360n^3} +\mathcal{O}\left(\frac{1}{n^5}\right)\right)\tag{4}\\ &=\sqrt{2\pi n}\left(\frac{n}{e}\right)^n \exp\left(\frac{1}{12n}-\frac{1}{360n^3}+\mathcal{O}\left(\frac{1}{n^5}\right)\right)\\ &=\sqrt{2\pi n}\left(\frac{n}{e}\right)^n \left(1+\left(\frac{1}{12n}-\frac{1}{360n^3}\right)+\frac{1}{2}\left(\frac{1}{12n}-\frac{1}{360n^3}\right)^2\right.\\ &\qquad\qquad\qquad\qquad\left.+\frac{1}{6}\left(\frac{1}{12n}-\frac{1}{360n^3}\right)^3+\mathcal{O}\left(\frac{1}{n^4}\right)\right)\\ &=\sqrt{2\pi n}\left(\frac{n}{e}\right)^n \left(1+\left(\frac{1}{12n}-\frac{1}{360n^3}\right)+\frac{1}{2}\left(\frac{1}{144n^2}\right)\right.\\ &\qquad\qquad\qquad\qquad\left.+\frac{1}{6}\left(\frac{1}{1728n^3}\right)+\mathcal{O}\left(\frac{1}{n^4}\right)\right) \end{align*}
Observe the change from $-\frac{1}{2}\ln n$ to $+\frac{1}{2}\ln n$ from (3) to (4) due to the change from $(n-1)!$ to $n!$.