Is the Ramanujan factorial approximation optimal, or can it be tweaked?

4.4k Views Asked by At

Ramanujan's famous factorial approximation, $$n!\approx\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+\frac{1}{30}}$$ is far more accurate that the Stirling approximation when applied to numbers less than 1, but it's not quite there. What could be done to tweak it without adding to its complexity?

Update: After experimenting with Ramanujan's approximation, I've discovered this disturbing fact: apparently, the error decreases slower than the factorial increases, leading to massive integer-level errors the higher the factorial gets. Allow me to demonstrate: $$\begin{array}{r|rr} n&\text{actual }n!&\text{Ramanujan}\\\hline 0&1&1.005513858315898906334685\\ 1&1&1.000283346113497298280502\\ 2&2&2.000066137639113675155990\\ 3&6&6.000048293969899370824935\\ 4&24&24.000067662060676644042510\\ 5&120&120.000147065856635128019467\\ 6&720&720.000442402580258517644894\\ 7&5040&5040.001717876125295382828871\\ 8&40320&40320.008220460028928349902077\\ 9&362880&362880.046912269278701001557543\\ 10&3628800&3628800.311612606631103904381528\\ 11&39916800&39916802.364768173672637433190699\end{array}$$ At first the error reduction is apparent, but then the zeroes start to disappear, until at $11!$ the error overtakes the decimal point. Now I'm not saying the error is increasing—only that it it not decreasing fast enough. What can be done to correct this problem without radically altering the formula? I know that Ramanujan himself anticipated this problem when he wrote this inequality: $$\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+\frac{1}{100}}<n!<\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+\frac{1}{30}}$$ Could this hold the key? (See Formula 2.1 here)

4

There are 4 best solutions below

4
On BEST ANSWER

The fraction $\tfrac{1}{30}$ may not be, practically speaking, the best upper bound for the final term of the radicand. Indeed, according to Ramanujan's own notes, the number ranges from $\tfrac{1}{30}$ down to $\tfrac{1}{100}$. Observe that when evaluated at $0$, $$\begin{align}0!&\approx\sqrt{\pi}\left(\frac{0}{e}\right)^0\root{\LARGE{6}}\of{8\cdot0^3+4\cdot0^2+0+\frac{1}{30}}\\ &=\sqrt{\pi}\cdot1\cdot\root{\LARGE{6}}\of{\frac{1}{30}}\\ &=\frac{\sqrt{\pi}}{\root{\LARGE{6}}\of{30}}\\ &=\root{\LARGE{6}}\of{\frac{\pi^3}{30}}\\ &\approx1.00551\end{align}$$ Notice the error in the result. However, if we replace the number $30$ in the above formula with $\pi^3$, the result is dead accurate: $$\begin{align}0!&\approx\sqrt{\pi}\left(\frac{0}{e}\right)^0\root{\LARGE{6}}\of{8\cdot0^3+4\cdot0^2+0+\frac{1}{\pi^3}}\\ &=\sqrt{\pi}\cdot1\cdot\root{\LARGE{6}}\of{\frac{1}{\pi^3}}\\ &=\frac{\sqrt{\pi}}{\root{\LARGE{6}}\of{\pi^3}}\\ &=\root{\LARGE{6}}\of{\frac{\pi^3}{\pi^3}}\\ &=1\end{align}$$ Now let's try it on $n=1$: $$\begin{align}1!&\approx\sqrt{\pi}\left(\frac{1}{e}\right)^1\root{\LARGE{6}}\of{8\cdot1^3+4\cdot1^2+1+\frac{1}{\pi^3}}\\ &=\frac{\sqrt{\pi}}{e}\root{\LARGE{6}}\of{13+\frac{1}{\pi^3}}\\ &\approx1.0002695\end{align}$$ For this reason, I recommend this modification to Ramanujan's approximation: $$n!\approx\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+\frac{1}{\pi^3}}$$ Alternatively, since $\pi^3\approx31.00627668$, a slightly less robust adjustment to the formula can be made by substituting $31$ for $30$, instead of $\pi^3$.

Let me know what you think about this!

Additional thoughts: Based on the material I added to my question above, perhaps $\tfrac{1}{\pi^3}\approx31$ should be gradually reduced toward $\tfrac{1}{\pi^4}\approx97$ as $n$ increases. A draft formula, pending further investigation: $${\large n!\approx\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+\frac{1}{\pi^{3+c}}}}$$ where $$\left(\frac{n}{n+1}\right)^3>c>\left(\frac{n}{n+1}\right)^2$$ The formula above is constantly evolving as I test and make adjustments. This formula is tricky to adjust. I have to express it in terms of an unknown value $c$ until I can dial it in, so to speak. My current estimate is $$c\approx\left(\frac{n}{n+1}\right)^{{\pi^3}\lower{2pt}/\lower{4pt}{12}}$$ Conclusion: The answer is no and no. According to my own analysis, Ramanujan's formula is only useful for estimating what the value of $n!$ might be for some $n$; and the error reduction rate appears painfully slow, slow enough to begin to accumulate error in the integer range as early as $11!$; as for tweaking it, the need appears to be for an adjustment of a major sort, which perhaps I will be unable to accomplish at this time. Indeed, any adjustment that optimizes the formula over one range appears to diminish its accuracy over another, as with all asymptotic approximations of Factorial and Gamma functions in general. I'm not saying a formula for the factorial function cannot be found that is optimal for all $x\geqslant0$; I simply have neither the time nor the mathematical expertise to do it myself.

10
On

Interestingly, if we do a series expansion in Mathematica

Series[(Gamma[n + 1]/Sqrt[Pi] (n/Exp[1])^-n)^6, {n, Infinity, 6}]

We get $$8 n^3+4 n^2+n+\frac{1}{30}-\frac{11}{240 n}+\frac{79}{3360n^2}+\frac{3539}{201600 n^3}-\frac{9511}{403200 n^4}-\frac{10051}{716800n^5}+\frac{233934691}{6386688000n^6}+O\left(\frac{1}{n}\right)^{13/2}.$$ This suggests that your ad hoc adjustment of the constant term is probably not strictly justified.

1
On

Long time ago, needing an explicit formula for statistical thermodynamics, I was facing the same problem of the calculation of $n!$ for $0<n<1$. Starting from Ramanujan's approximation, what I did was to write $$n!\approx\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+x(n)}$$ What can be easily established is that $$x(0)=\frac{1}{\pi ^3}$$ just as proposed by Brian J. Fink. We can also show that $$x(1/2)=\frac{1}{8} \left(e^3-20\right)$$ and $$x(1)=13-\frac{e^6}{\pi ^3}$$ and I used (this is totally empirical and cannot be justified) a quadratic expansion of $x(n)=a+b n+c n^2$ which leads to $$a=\frac{1}{\pi ^3}$$ $$b=\frac{1}{2} \left(6+e^3-\frac{2 \left(3+e^6\right)}{\pi ^3}\right)$$ $$c=\frac{1}{2} \left(-32-e^3+\frac{4 \left(1+e^6\right)}{\pi ^3}\right)$$ Again, this has never been supposed to be used outside the range $[0,1]$

0
On

Back to the problem three and half years later.

Considering heropup's interesting answer, considering $$n!\approx\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+x(n)}$$ we can approximate $x(n)$ by $$x(n)=\frac{1}{30}+\frac{1455925-70798882\, n}{1544702880\, n^2+760646880\, n+981836548}$$ which leads to $$\left( \begin{array}{ccc} n & n! & \text{approximation}\\ 1 & 1 & 1.0000133314197806798 \\ 2 & 2 & 2.0000005398355538814 \\ 3 & 6 & 6.0000001098448978182 \\ 4 & 24 & 24.000000057274931372 \\ 5 & 120 & 120.00000005587036156 \\ 6 & 720 & 720.00000008585188238 \\ 7 & 5040 & 5040.0000001871217948 \\ 8 & 40320 & 40320.000000539851469 \\ 9 & 362880 & 362880.00000196433740 \\ 10 & 3628800 & 3628800.0000087019948\\ 11 & 39916800 & 39916800.000045696065 \end{array} \right)$$

This would make heropup's expansion $$8 n^3+4 n^2+n-\frac{644167196629}{44487442944000 \, n^5}+O\left(\frac{1}{n^6}\right)$$