home3T MRINHFML FEAGarrettStirling

Author: David Max MA DPhil (2017-11-08)

A Fortran-77 program to test Stirling's formula for computing factorials

Factorials n! can be calculated directly for small n. For large n, however, the computation becomes cumbersome.

Stirling's formula is a commonly-used approximation for computing (the log of) factorial n for large n.

Program STIRLING is designed to test the accuracy of Stirling's approximation for a few values of n.

There are two forms of the approximation, the first of which is simply nln(n)-n. Program STIRLING computes the second, more sophisticated form.

The program is written in Fortran-77 and compiles okay using the gcc compiler (i.e., gfortran).

The computations are done using 8-byte precision (REAL*8).

STIRLING Fortran program listing

Fortran program for download

The Fortran-77 source file, together with a batch file to compile it and a file containing output results, are available to download in either tar or zip archive form.

On clicking on one of the links, your browser should ask you what you want to do with the selected archive file.

First, save the file into a working directory (or folder, in Windows-speak) of your choice.

Under Linux/Unix, the tar archive file can be unpacked using the shell command...

tar -xf STIRLING.tar.

(tar is also available for Windows).

On Windows the zip archive file can be unpacked by right clicking on the file and choosing "Extract All...".

If gcc has been installed, the program file STIRLING.exe can be built on Windows by running the batch file MAKE.STIRLING2.bat from the command line.

The batch file provided may be a useful starting place for compiling the source with an alternative compiler (Intel, Salford, etc).

Results for n up to 50

The table below gives some results from program STIRLING.

Fortran77 Stirling approximation results
n STIRLINGln(n!)
recursive
frac. error A. & S.
1-0.0810610.0000000.0000000.000000
20.6518060.693147-0.0596420.693147
31.7640821.791759-0.0154471.791759
43.1572633.178054-0.0065423.178054
54.7708474.787492-0.0034774.787492
66.5653756.579251-0.0021096.579251
78.5132658.525161-0.0013958.525161
810.59419210.604603-0.00098210.604603
912.79257212.801827-0.00072312.799525
1015.09608215.104413-0.00055215.104412
1117.49473417.502308-0.00043317.502308
1219.98027219.987214-0.00034719.987215
1322.54575522.552164-0.00028422.552164
1425.18527025.191221-0.00023625.191220
1527.89371727.899271-0.00019927.899272
1630.66665230.671860-0.00017030.671861
1733.50017233.505073-0.00014633.505075
1836.39081636.395445-0.00012736.395445
1939.33549939.339884-0.00011139.339885
2042.33145042.335616-0.00009842.335617
2145.37617145.380139-0.00008745.380139
2248.46739448.471181-0.00007848.471182
2351.60305351.606676-0.00007051.606675
2454.78125754.784729-0.00006354.784730
2558.00027258.003605-0.00005758.003606
2661.25849761.261702-0.00005261.261702
2764.55445264.557539-0.00004864.557539
2867.88676767.889743-0.00004467.889744
2971.25416671.257039-0.00004071.257039
3074.65545974.658236-0.00003774.658236
3178.08953578.092224-0.00003478.092224
3281.55535581.557959-0.00003281.557960
3385.05194285.054467-0.00003085.054468
3488.57837788.580828-0.00002888.580828
3592.13379592.136176-0.00002692.136176
3695.71738095.719695-0.00002495.719694
3799.32836099.330612-0.00002399.330613
38102.966006102.968199-0.000021102.968198
39106.629624106.631760-0.000020106.631760
40110.318556110.320640-0.000019110.320640
41114.032179114.034212-0.000018114.034212
42117.769897117.771881-0.000017117.771881
43121.531144121.533082-0.000016121.533082
44125.315377125.317271-0.000015125.317270
45129.122082129.123934-0.000014129.123934
46132.950763132.952575-0.000014132.952576
47136.800950136.802723-0.000013136.802724
48140.672188140.673924-0.000012140.673924
49144.564043144.565744-0.000012144.565744
50148.476100148.477767-0.000011148.477767

The results for the 'recursive' column agree with the Abramovich and Stegun table.

The small differences might be the result of using lower (8-byte) precision here than in the computations for the A. & S. tables.

The form of the Stirling formula computed here agrees with the accurate value to 5.5 parts in 104 with n = 10.

At n = 50, agreement has reached 1.1 parts in 105.

For comparison, the very simple form of the approximation, nln(n)-n, returns 13.036 (for n = 10) and 145.601 (n = 50).

References

[1] Abramowitz, M. & Stegun, I.A. 1965. Handbook of Mathematical Functions. Dover, New York.

[1] Kupferschmid, M. 2009. Classical FORTRAN: Programming for Engineering and Scientific Applications. CRC Press, Boca Raton.