EXPLOOT project
A note on the Stirling formula

I. Cnop
Vrije Universiteit Brussel
icnop@vub.ac.be

How fast does the  Stirling formula approach the  factorial?

The Stirling formula

Here is the Stirling approximation for  n!

s(
        n_) := (Sqrt[2 π n]) ((((n/E)))^n)

Check a few values:

s(4. )

4!

First[Timing[s[1000. ]]]

First[Timing[1000!]]

((s(1000. ))/(1000!))

which is close to   1  , while both numbers are huge:

N[s(1000)]

1000!

ListPlot[
      Table[100 N[((n! - s(n))/(n!))], {n, 2, 2000, 100}]]

Relative error

Extend the previous graphic

ListPlot[
      Table[100 N[(-s[n] + n!)/n!], {n, 2, 20000, 
          100}]]

This graph looks as if it decays in a   (1/(x^power)) manner.  It takes some time to generate:

First[
      Timing[Table[
          100 N[(-s[n] + n!)/n!], {n, 20, 8000, 
            10}]]]

Make plot with larger step size to cover more of the    x -axis  :

relative = 
        Table[{n, 100 N[(-s[n] + n!)/n!]}, {n, 2000, 50000, 
            1000}];

and try to approximate with rational expressions

Fit[relative, {1, 1/x, 1/x^2, 1/x^3}, x]

Fit[relative, {1, 1/x, 1/x^2}, x]

Only  the contribution of  1/x  is important here:

Fit[relative, {1, 1/x}, x]

ListPlot[relative]

\(Plot[8.333183027656423`\/x, {x, 0, 50000}]\)

Show[%%, %]

Show[%, PlotRange -> {0, 0.005}]

The coefficient is close to

1/12 // N

Epilogue: it can be proved that this is a precise estimate

The proof uses much more advanced mathematics.