Approximating log of factorial

Solution 1:

Approximation for $\log n!$ given by Srinivasa Ramanujan :

$$\log n! \approx n\log n-n+\frac{\log(n(1+4n(1+2n)))}{6}+\frac{\log(\pi)}{2}$$

Solution 2:

For smaller values of $n$

$$ \log n! \approx (n+1) \log n - n$$

Assuming $\log = \log_e$ (In other words, $\log$ is to the base $e$)

Solution 3:

Stirling's approximation gets better as $n$ gets higher, so storing a table of small values and switching over for large $n$ is quite viable. It's a trade between size of the table and accuracy. You could also use more terms in the approximation. The next is $\ln(1+\frac 1{12n})$. For $n$ in the 20's or 30's that decreases the error from a few thousandths to a few millionths. That will replace many entries in your table. The fact that the error in Stirling's approximation is bounded by the first neglected term gives you an easy way to find the best number of terms as a function of $n$ if you want.

Solution 4:

According to an answer in this post (i.e. a possible duplicate), there exists a better approximation for $\log(n!)$ as opposed to what the accepted answer asserts.

Let $m=n\big(1+4n(1+2n)\big)$, then

$$\log(n!)\approx n\big(\log_e(n)-1\big)+\frac12\Bigg(\frac 13\log_e\left(\dfrac{1}{30}+m\right)+\log_e(\pi)\Bigg)$$

such that $e$ is, by definition, Euler's Constant.

The accepted answer has the exact same approximation, except it excludes $1/30$.