I am stuck at the following exercise:
Show that for $n \rightarrow \infty$ holds
$$\log(n!) = n \log n − n + \mathcal{O}(\log n).$$
I do not see how I should prove this. I know that
$$\log(n!) = \log(n)+\log(n-1)+\cdots+\log(1)$$
, and I see that this is similar to $$n\log(n) = \sum_{i=1}^n \log(n)$$but I do not see how this should help here. Could you give me a hint?
Let us treat this as an exercise in summation by parts:
$$ \log(N!)=\sum_{n=1}^{N}\log(n)=N\log N-\sum_{n=1}^{N-1}n\log\left(1+\frac{1}{n}\right) $$ and by Maclaurin series $\log(1+x)=x-\frac{x^2}{2}+O(x^3)$, so $$ \log(N!)=N\log N-(N-1)+\sum_{n=1}^{N-1}n\left(\frac{1}{n}-\log\left(1+\frac{1}{n}\right)\right)$$ equals $$ N\log N-N+1+\sum_{n=1}^{N-1}\left(\frac{1}{2n}+O\left(\frac{1}{n^2}\right)\right) $$ i.e. $$ N\log N - N + \frac{1}{2}\log N + O(1). $$ Probably the hardest part of the proof of Stirling's inequality is to show that the last $O(1)$ term hides $\frac{1}{2}\log(2\pi)+O\left(\frac{1}{n}\right)$, but this weaker form is usually more than enough for concrete applications.