Preprint
Short Note

This version is not peer-reviewed.

An Algorithm for Calculating Terms of the Stirling’s Formula Remainder

A peer-reviewed article of this preprint also exists.

Submitted:

16 September 2025

Posted:

18 September 2025

You are already at the latest version

Abstract
In this paper, we deal with Stirling’s approximation to N!. We employ elementary mathematical techniques to provide an algorithm to quickly calculate many terms of Stirling’s formula remainder.
Keywords: 
;  ;  

1. Introduction

In this note, we study Stirling’s approximation to N ! , a fundamental result in asymptotic analysis and number theory. Originally derived in the 18th century, the approximation provides an accurate estimate of the factorial function for large values of N. Over the centuries, various forms and refinements of this approximation have been developed. A particularly elegant and instructive derivation, based solely on elementary transformations of integrals and term-by-term integration of a series, was introduced by Marsaglia [1].
Our first objective is to briefly derive Stirling’s formula. We begin with the representation of ln N ! as a sum of logarithms:
ln N ! = ln 1 + ln 2 + . . . + ln N = = 1 N ln x d x + k = 1 N 1 ln ( k + 1 ) ln k 2 k = 1 N 1 D k ,
where each term D k measures the discrepancy between the trapezoidal rule and the exact integral of ln x over the interval [ k , k + 1 ] . That is,
D k = k k + 1 ln x d x ln k + ln ( k + 1 ) 2 = = x ln x x | k k + 1 ln k + ln ( k + 1 ) 2 = = ( k + 1 ) ln ( k + 1 ) k ln k 1 ln k + ln ( k + 1 ) 2 = = k + 1 2 ln 1 + 1 k 1 .
The integral in (1) can be evaluated explicitly,
1 N ln x d x = N ln N N + 1 , and k = 1 N 1 ln ( k + 1 ) ln k 2 = ln N 2
is a telescoping sum, as the terms cancel pairwise. In this way,
ln N ! = N ln N N + 1 + ln N 2 k = 1 N 1 D k = = ln N N N + 1 + ln N 1 / 2 k = 1 N 1 D k ,
which leads to
N ! = N N + 1 / 2 e 1 N exp k = 1 N 1 D k ,
where D k is given in (2).
It is well known, from the Mercator series, that
ln ( 1 + x ) = x x 2 2 + x 3 3 x 4 4 + . . . = i = 1 + ( 1 ) i + 1 x i i , 1 < x 1 .
Substituting x = 1 / k , where k = 1 , 2 , 3 , . . . , yields
ln 1 + 1 k = 1 k 1 2 k 2 + 1 3 k 3 1 4 k 4 + . . . = i = 1 + ( 1 ) i + 1 1 i k i .
Thus, D k in (2) can be rewritten as
D k = k + 1 2 ln 1 + 1 k 1 = = 1 1 2 k + 1 3 k 2 1 4 k 3 + . . . + 1 2 1 k 1 2 k 2 + 1 3 k 3 . . . 1 = = 1 3 1 2.2 1 k 2 1 4 1 2.3 1 k 3 + 1 5 1 2.4 1 k 4 . . . = = i = 2 + ( 1 ) i 1 i + 1 1 2 i 1 k i = i = 2 + ( 1 ) i a i k i ,
where a i = 1 / ( i + 1 ) 1 / ( 2 i ) , for i = 2 , 3 , 4 , . . . , is a positive decreasing sequence of real numbers that converges to zero.
Thus,
k = 1 N 1 D k = k = 1 N 1 i = 2 + ( 1 ) i a i k i = i = 2 + ( 1 ) i a i k = 1 N 1 1 k i .
From the Riemann Zeta function (see [2] for details),
ζ ( x ) = j = 1 + 1 j x ,
which is convergent for x R with x > 1 , it follows that
k = 1 N 1 1 k i = ζ ( i ) k = N + 1 k i ,
and, therefore,
k = 1 N 1 D k = i = 2 + ( 1 ) i a i ζ ( i ) i = 2 + ( 1 ) i a i k = N + 1 k i .
From [2,3],
i = 2 + ( 1 ) i ζ ( i ) 2 i = 1 2 ζ ( 2 ) 2 ζ ( 3 ) 3 + ζ ( 4 ) 4 ζ ( 5 ) 5 + . . . = γ 2 ,
where γ is the Euler-Mascheroni constant.
Additionally, from [3,4], based on the Riemann Zeta function and Rational Zeta series, it follows that
i = 2 + ( 1 ) i ζ ( i ) 1 i + 1 = 1 2 γ + 3 ln 2 π ln 2 .
Moreover, using the Mercator series evaluated at x = 1 ,
i = 2 + ( 1 ) i 1 i + 1 = ln 2 1 2 ,
we arrive at the Suryanarayana formula (see [3] for example),
i = 2 + ( 1 ) i ζ ( i ) i + 1 = 1 2 γ + 3 ln 2 π ln 2 + ln 2 1 2 = γ 2 + 1 ln 2 π ,
and, consequently,
i = 2 + ( 1 ) i a i ζ ( i ) = γ 2 + 1 ln 2 π γ 2 = 1 ln 2 π .
In this way,
k = 1 N 1 D k = 1 ln 2 π i = 2 + k = N + ( 1 ) i a i k i .
Therefore,
exp k = 1 N 1 D k = e 1 2 π e r N ,
where r N is given by
r N = i = 2 + k = N + ( 1 ) i a i k i ,
and, as before, a i = 1 / ( i + 1 ) 1 / ( 2 i ) , for i = 2 , 3 , 4 , . . . , is a positive decreasing sequence of real numbers that converges to zero.
Finally, Eq. (3), known in the literature as Stirling’s formula, can be expressed as
N ! = 2 π N N + 1 / 2 e N e r N ,
where the remainder r N is given in (4).
Eq (5) was first presented in [5], where it was also established that 11 / 2 < r N + ln 2 π < 1 . Since then, the term r N has been the subject of detailed study (see [6,7,8,9] for historical notes), particularly through works such as [10], who first provided sharp two-sided bounds for it,
1 12 N + 1 < r N < 1 12 N .
Several years later, [11] improved the estimation of r N ,
1 12 N 1 360 N 3 < r N < 1 12 N .
More recently, [12] obtained the following result,
1 12 N 1 360 N 3 < r N < 1 12 N 1 360 N ( N + 1 ) ( N + 2 ) ,
[8] proved that
1 12 N + c 1 r N < 1 12 N + c 2 ,
with the best possible constants c 1 = 12 + 1 / ( 1 ln 2 π ) 0.336317 and c 2 = 0 , and [9] derived the following inequality:
1 12 N 7 360 N ( 7 N 2 + 2 ) 13 35280 N 7 < r N < 1 12 N 7 360 N ( 7 N 2 + 2 ) .
It is also important to highlight the works [1,7,13,14,15] who focused on obtaining explicit formulas for the coefficients of the asymptotic expansion of r N .
The main goal of this work is to present an algorithm, relying only on elementary mathematical methods techniques, for efficiently calculating many terms of r N exactly. To the best of our knowledge, we understand that our algorithm represents an alternative to the procedures presented herein.

2. The Algorithm

Observe that r N in (4) can be rewritten as
r N = k = N + i = 2 + ( 1 ) i a i k i = k = N + a 2 k 2 a 3 k 3 + a 4 k 4 a 5 k 5 + ,
a numerical series whose general term forms a convergent alternating series for each N. Define
S j ( k ) = i = 2 j ( 1 ) i a i k i ,
the sequence of partial sums of this series.
Step 1: Consider the polynomial p 1 defined by the following equation:
S 4 ( k ) α 1 1 k 1 k + 1 = p 1 ( k ) k 4 ( k + 1 ) ,
where α 1 is a real parameter to be determined later. This leads to:
p 1 ( k ) = k 4 ( k + 1 ) S 4 ( k ) α 1 1 k 1 k + 1 = = k 4 ( k + 1 ) a 2 k 2 a 3 k 3 + a 4 k 4 α 1 1 k 1 k + 1 = = ( a 2 α 1 ) k 3 + ( a 2 a 3 ) k 2 + ( a 4 a 3 ) k + a 4 .
Now, α 1 is chosen in order to reduce the degree of p 1 to the lowest possible value. In this case, α 1 = a 2 = 1 / 12 and, after some calculations, we obtain
p 1 ( k ) = ( a 4 a 3 ) k + a 4 = 1 120 k + 3 40
which is a polynomial of degree 1, that is, deg ( p 1 ) = 1 . We denote by η ( p 1 ) = 1 / 120 the coefficient associated with the highest degree term of the polynomial p 1 .
In this way, the partial sum S 4 can be expressed as
S 4 ( k ) = a 2 k 2 a 3 k 3 + a 4 k 4 = 1 12 1 k 1 k + 1 + p 1 ( k ) k 4 ( k + 1 ) .
As an important remark, r N in (4) can then be approximated by
r N k = N + S 4 ( k ) = 1 12 k = N + 1 k 1 k + 1 + k = N + p 1 ( k ) k 4 ( k + 1 ) = = 1 12 N + k = N + p 1 ( k ) k 4 ( k + 1 ) .
The sum above can be estimated using the integral test, comparing it to an improper integral.
Step 2: Similarly, we seek a polynomial p 3 that satisfies
p 1 ( k ) k 4 ( k + 1 ) a 5 k 5 + a 6 k 6 α 3 1 k 3 1 ( k + 1 ) 3 = p 3 ( k ) k 6 ( k + 1 ) 3 ,
where α 3 is a real parameter to be determined.
Thus,
p 3 ( k ) = k 2 ( k + 1 ) 2 p 1 ( k ) a 5 k ( k + 1 ) 3 + a 6 ( k + 1 ) 3 α 3 k 3 ( k + 1 ) 3 k 3 .
Again, α 3 is chosen with the aim of reducing the degree of p 3 to the lowest possible value. In this case, α 3 = η ( p 1 ) / 3 = 1 / 360 .
Therefore, we can present S 6 ( k ) as
S 6 ( k ) = 1 12 1 k 1 k + 1 1 360 1 k 3 1 ( k + 1 ) 3 + p 3 ( k ) k 6 ( k + 1 ) 3 ,
where
p 3 ( k ) = 1 252 k 3 + 3 56 k 2 + 47 420 k + 5 84 .
Note that deg ( p 3 ) = 3 and η ( p 3 ) = 1 / 252 .
Again, as a remark, r N in (4) is better approximated by
r N k = N + S 6 ( k ) = 1 12 k = N + 1 k 1 k + 1 1 360 k = N + 1 k 3 1 ( k + 1 ) 3 + + k = N + p 3 ( k ) k 6 ( k + 1 ) 3 = 1 12 N 1 360 N 3 + k = N + p 3 ( k ) k 6 ( k + 1 ) 3 .
Step 3: Similarly, aiming for a better approximation of r N , we write
p 3 ( k ) k 6 ( k + 1 ) 3 a 7 k 7 + a 8 k 8 α 5 1 k 5 1 ( k + 1 ) 5 = p 5 ( k ) k 8 ( k + 1 ) 5 ,
where α 5 is, as before, a real parameter to be determined.
The polynomial p 5 can be rewritten as
p 5 ( k ) = k 2 ( k + 1 ) 2 p 3 ( k ) a 7 k ( k + 1 ) 5 + a 8 ( k + 1 ) 5 α 5 k 3 ( k + 1 ) 5 k 5 .
The parameter α 5 is chosen with the aim of reducing the degree of p 5 to the lowest possible value. Thus, α 5 = η ( p 3 ) / 5 = 1 / 1260 .
Then, S 8 ( k ) is given by
S 8 ( k ) = 1 12 1 k 1 k + 1 1 360 1 k 3 1 ( k + 1 ) 3 + + 1 1260 1 k 5 1 ( k + 1 ) 5 + p 5 ( k ) k 8 ( k + 1 ) 5 ,
where
p 5 ( k ) = 1 240 k 5 + 29 720 k 4 + 13 72 k 3 + 5 18 k 2 + 191 1008 k + 7 144 .
Note that deg ( p 5 ) = 5 and η ( p 5 ) = 1 / 240 .
Therefore, a better approximation for r N in (4) is obtained:
r N k = N + S 8 ( k ) = 1 12 k = N + 1 k 1 k + 1 1 360 k = N + 1 k 3 1 ( k + 1 ) 3 + + 1 1260 k = N + 1 k 5 1 ( k + 1 ) 5 + k = N + p 5 ( k ) k 8 ( k + 1 ) 5 = = 1 12 N 1 360 N 3 + 1 1260 N 5 + k = N + p 5 ( k ) k 8 ( k + 1 ) 5 .
Step 4: Likewise, the polynomial p 7 is defined as
p 7 ( k ) = k 2 ( k + 1 ) 2 p 5 ( k ) a 9 k ( k + 1 ) 7 + a 10 ( k + 1 ) 7 α 7 k 3 ( k + 1 ) 7 k 7 .
Taking α 7 = η ( p 5 ) / 7 = 1 / 1680 , we obtain
p 7 ( k ) = 1 132 k 7 + 7 132 k 6 + 3349 13860 k 5 + 8119 13860 k 4 + 10891 13860 k 3 + + 105 176 k 2 + 479 1980 k + 9 220 ,
where deg ( p 7 ) = 7 and η ( p 7 ) = 1 / 132 .
Thus,
S 10 ( k ) = 1 12 1 k 1 k + 1 1 360 1 k 3 1 ( k + 1 ) 3 + + 1 1260 1 k 5 1 ( k + 1 ) 5 1 1680 1 k 7 1 ( k + 1 ) 7 + p 7 ( k ) k 10 ( k + 1 ) 7 ,
and then
r N k = N + S 10 ( k ) = 1 12 N 1 360 N 3 + 1 1260 N 5 1 1680 N 7 + k = N + p 7 ( k ) k 10 ( k + 1 ) 7 ,
where deg ( p 7 ) = 7 .
In summary, the following algorithm is presented.
Step 5: Algorithm
α 1 = 1 / 12 p 1 ( k ) = ( 1 / 120 ) k + 3 / 40 = η ( p 1 ) k + 3 / 40 For l = 1 , 2 , 3 , p 2 l + 1 ( k ) = k 2 ( k + 1 ) 2 p 2 l 1 ( k ) a 2 l + 3 k ( k + 1 ) 2 l + 1 + a 2 l + 4 ( k + 1 ) 2 l + 1 α 2 l + 1 k 3 ( k + 1 ) 2 l + 1 k 2 l + 1 α 2 l + 1 = η ( p 2 l 1 ) / ( 2 l + 1 )
Remark 1.
The sequence ( α 2 l + 1 ) , for l = 0 , 1 , 2 , 3 , . . . , defined above, is crucial because it leads to
r N = l = 0 + ( 1 ) l α 2 l + 1 N 2 l + 1 .
Furthermore, based on computational tests, it was observed that, for l = 0 , 1 , 2 , . . . ,
deg ( p 2 l + 1 ) = 2 l + 1 .
The exact calculations of p 2 l + 1 ( k ) and α 2 l + 1 , for l = 1 , 2 , 3 , . . . , were carried out using symbolic computations in the Mathematica software [16], using the script that follows:
Preprints 177017 i001
To illustrate our algorithm, we calculate α 1 , α 3 , α 5 , . . . , α 39 using the Mathematica script provided above (CPU time approximately 0.1 seconds, executed in the trial version of Mathematica Online) and then, r N in (4) can be approximated by
r N = l = 0 + ( 1 ) l α 2 l + 1 N 2 l + 1 1 12 N 1 360 N 3 + 1 1260 N 5 1 1680 N 7 + 1 1188 N 9 691 360360 N 11 + 1 156 N 13 3617 122400 N 15 + 43867 244188 N 17 174611 125400 N 19 + + 77683 5796 N 21 236364091 1506960 N 23 + 657931 300 N 25 3392780147 93960 N 27 + 1723168255201 2492028 N 29 7709321041217 505920 N 31 + 151628697551 396 N 33 26315271553053477373 2418179400 N 35 + + 154210205991661 444 N 37 261082718496449122051 21106800 N 39 .
As a test, the CPU time to calculate the first 300 terms of r N was approximately 200 seconds. Remark 1 have been verified in this case.

References

  1. Marsaglia, G., Marsaglia, J.C.W.: A new derivation of stirling’s approximation to n! Am. Math. Mon. 97:9, 826–829 (1990).
  2. Lagarias, J.C.: Euler’s constant: Euler’s work and modern developments. Bulletin of the American Mathematical Society 50(4), 527–628 (2013).
  3. Coppo, M.-A.: A note on some alternating series involving zeta and multiple zeta values. Journal of Mathematical Analysis and Applications 475(2), 1831–1841 (2019).
  4. Borwein, J.M., Bradley, D.M., Crandall, R.E.: Computational strategies for the riemann zeta function. Journal of Computational and Applied Mathematics 121(1), 247–296 (2000).
  5. Hummel, P.M.: A note on stirling’s formula. Am. Math. Mon. 47, 97–99 (1940).
  6. Beesack, P.R.: Improvement of stirling’s formula by elementary methods. Univ. Beograd. Publ. Elektrotehn. Fak. Set. Mat. Fiz. 274-301, 17–21 (1969).
  7. Nemes, G.: On the coefficients of the asymptotic expansion of n! (2010). https://arxiv.org/abs/1003.2907.
  8. Chen, C.P., Batir, N.: Some inequalities and monotonicity properties associated with the gamma and psi functions. Appl. Math. Comput. 218, 8217–8225 (2012).
  9. Lin, L.: On stirling’s formula remainder. Applied Mathematics and Computation 247, 494–500 (2014).
  10. Nanjundiah, T.S.: Note on stirling’s formula. Am. Math. Mon. 66, 701–703 (1959).
  11. Robbins, H.: A remark on stirling’s formula. Am. Math. Mon. 62, 26–29 (1955).
  12. Shi, X., Liu, F., M., H.: A new asymptotic series for the gamma function. J. Comput. Appl. Math. 195, 134–154 (2006).
  13. Comtet, L.: Advanced Combinatorics: The Art of Finite and Infinite Expansions - Stirling Numbers, pp. 204–229. Springer, Dordrecht (1974). [CrossRef]
  14. S., B., M´endez, A.: The asymptotic expansion for n! and Lagrange inversion formula (2010). https://arxiv.org/abs/1002.3894.
  15. Impens, C.: Stirling’s series made easy. Am. Math. Mon. 110:8, 730–735 (2003).
  16. Inc., W.R.: Mathematica Online, Version 14.2. Champaign, IL, 2024. https://www.wolfram.com/mathematica.
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.
Copyright: This open access article is published under a Creative Commons CC BY 4.0 license, which permit the free download, distribution, and reuse, provided that the author and preprint are cited in any reuse.
Prerpints.org logo

Preprints.org is a free preprint server supported by MDPI in Basel, Switzerland.

Subscribe

Disclaimer

Terms of Use

Privacy Policy

Privacy Settings

© 2025 MDPI (Basel, Switzerland) unless otherwise stated