I offer another way to compare two approximations of n!.
We compare the Mortici formula (from the paper) to the two-term Stirling series (which the paper fails to do):
mortici = Sqrt[2 \[Pi] n] (n/E + 1/(12 E n))^n;
stirling2 = Sqrt[2 \[Pi] n] (n/E)^n (1 + 1/(12 n));
factorial = n!;
Series[( (* takes a few seconds *)
mortici - factorial)/(stirling2 - factorial) // Simplify,
{n, Infinity, 10}] // Normal // FullSimplify[#, n > 0] &;
Series[%, {n, Infinity, 2}]
(* 1/(5 n) + 77/(450 n^2) + O[1/n]^3 *)
{1/(5 n), (mortici - factorial)/(stirling2 - factorial)} /.
{{n -> 100.}, {n -> 2500.`30}} // N
(* 1/(5n) ratio
{{0.002, 0.00201741},
{0.00008, 0.0000800274}}
*)
Thus the Mortici formula has an absolute error approximately 1/(5n) times the error of the two-term Stirling formula.
Numerics note: It may be better to use logarithms and LogGamma, since the numbers can get very large. However, the arbitrary-precision numbers seem to work well enough up to n = 2500. In any case, here's the set-up for an logarithmic approach:
logmortici = mortici // Log // FullSimplify[#, n > 0] &;
logstirling2 = stirling2 // Log // FullSimplify[#, n > 0] &;
logfactorial = LogGamma[n + 1];
(* rel/abs errors *)
relmortici = Internal`Expm1[logmortici - logfactorial];
absmortici = relmortici factorial;
relstirling2 = Internal`Expm1[logstirling2 - logfactorial];
absstirling2 = relstirling2 factorial;
The function Internal`Expm1[x] computes Exp[x]-1 accurately when Exp[x] is near 1 (x near zero).
Note also that Exp[2500.] overflows machine-precision and is automatically promoted to arbitrary precision with $MachinePrecision digits (approx. 15.95). However, for the absolute error mortici - factorial for n = 2500., we need at least 19 digits to get a one-digit result.
Exp@Normal@Series[LogGamma[n + 1], {n, Infinity, 1}]does a pretty good job, only 2-4 times worse than the paper's formula for $100\le n \le 2500$. – Michael E2 Jun 14 '23 at 19:37Normal@Series[Gamma[n + 1], {n, Infinity, 1}](Stirling series), which seems fairer than comparing it to the zero-order approximation, especially since theirs is still much better. – Michael E2 Jun 14 '23 at 19:472.or1., your result will always be given as a machine-precision number. This can be an obstacle when looking at very large numbers, asymptotes, etc. as you're trying to do. – Roman Jun 14 '23 at 20:28Exp[-x] Sqrt[2π/x] (x Sqrt[1/(810 x^6) + x Sinh[1/x]])^x– Greg Hurst Jun 15 '23 at 11:36