Testing Stirling's approximation
This program tests the Stirling's approximation formula given in Stirling's approximation at Wikipedia that Stirling's approximation is
and in the book Information Theory, Inference, and Learning Algorithms by David MacKay that Stirling's approximation is
for numbers up to fifty.
#!/home/ben/software/install/bin/perl use warnings; use strict; use Math::Trig; my $factorial = 1; my $e = exp (1.0); # This is the format for printing out the numbers used in "sprintf" # below. my $format = "<tr><td>%3d</td>" . ("<td>%10g</td>" x 5) . "</tr>\n"; for my $n (1..50) { $factorial *= $n; # Wikipedia's formula for Stirling's Approximation. my $stirling = sqrt (2*pi*$n) * ($n/$e)**$n; my $ratio = $factorial / $stirling; # David MacKay's "Stirling's Approximation". my $mackay = $n**$n * exp (-$n); my $mratio = $factorial / $mackay; # This is the output string. my $out = sprintf $format, $n, $factorial, $stirling, $ratio, $mackay, $mratio; # This converts the e+12-format output into HTML format. $out =~ s!e\+0*(\d+)!×10<sup>$1</sup>!g; print "$out"; }
The output looks like this:
$N$ | $N!$ | $n! \sim \sqrt{2 \pi n} \left(\frac{n}{e}\right)^n=W$ | $N!/W$ | $x! \simeq x^{x} \, e^{-x}=M$ | $N!/M$ |
---|---|---|---|---|---|
1 | 1 | 0.922137 | 1.08444 | 0.367879 | 2.71828 |
2 | 2 | 1.919 | 1.04221 | 0.541341 | 3.69453 |
3 | 6 | 5.83621 | 1.02806 | 1.34425 | 4.46345 |
4 | 24 | 23.5062 | 1.02101 | 4.6888 | 5.11858 |
5 | 120 | 118.019 | 1.01678 | 21.0561 | 5.69907 |
6 | 720 | 710.078 | 1.01397 | 115.649 | 6.22575 |
7 | 5040 | 4980.4 | 1.01197 | 750.974 | 6.71128 |
8 | 40320 | 39902.4 | 1.01047 | 5628.13 | 7.16401 |
9 | 362880 | 359537 | 1.0093 | 47811.5 | 7.58981 |
10 | 3.6288×106 | 3.5987×106 | 1.00837 | 453999 | 7.99296 |
11 | 3.99168×107 | 3.96156×107 | 1.0076 | 4.76519×106 | 8.37675 |
12 | 4.79002×108 | 4.75687×108 | 1.00697 | 5.47824×107 | 8.74371 |
13 | 6.22702×109 | 6.18724×109 | 1.00643 | 6.84598×108 | 9.09589 |
14 | 8.71783×1010 | 8.6661×1010 | 1.00597 | 9.23995×109 | 9.43493 |
15 | 1.30767×1012 | 1.30043×1012 | 1.00557 | 1.33953×1011 | 9.76221 |
16 | 2.09228×1013 | 2.08141×1013 | 1.00522 | 2.07591×1012 | 10.0789 |
17 | 3.55687×1014 | 3.53948×1014 | 1.00491 | 3.42472×1013 | 10.3859 |
18 | 6.40237×1015 | 6.3728×1015 | 1.00464 | 5.99245×1014 | 10.6841 |
19 | 1.21645×1017 | 1.21113×1017 | 1.0044 | 1.10847×1016 | 10.9742 |
20 | 2.4329×1018 | 2.42279×1018 | 1.00418 | 2.16128×1017 | 11.2568 |
21 | 5.10909×1019 | 5.08886×1019 | 1.00398 | 4.43018×1018 | 11.5325 |
22 | 1.124×1021 | 1.11975×1021 | 1.00379 | 9.52402×1019 | 11.8017 |
23 | 2.5852×1022 | 2.57585×1022 | 1.00363 | 2.14273×1021 | 12.065 |
24 | 6.20448×1023 | 6.18298×1023 | 1.00348 | 5.03503×1022 | 12.3226 |
25 | 1.55112×1025 | 1.54596×1025 | 1.00334 | 1.2335×1024 | 12.575 |
26 | 4.03291×1026 | 4.02001×1026 | 1.00321 | 3.14522×1025 | 12.8224 |
27 | 1.08889×1028 | 1.08553×1028 | 1.00309 | 8.33433×1026 | 13.0651 |
28 | 3.04888×1029 | 3.03982×1029 | 1.00298 | 2.29181×1028 | 13.3034 |
29 | 8.84176×1030 | 8.81639×1030 | 1.00288 | 6.53134×1029 | 13.5374 |
30 | 2.65253×1032 | 2.64517×1032 | 1.00278 | 1.92665×1031 | 13.7676 |
31 | 8.22284×1033 | 8.20076×1033 | 1.00269 | 5.87602×1032 | 13.9939 |
32 | 2.63131×1035 | 2.62447×1035 | 1.00261 | 1.85087×1034 | 14.2166 |
33 | 8.68332×1036 | 8.66142×1036 | 1.00253 | 6.01509×1035 | 14.4359 |
34 | 2.95233×1038 | 2.9451×1038 | 1.00245 | 2.01498×1037 | 14.6519 |
35 | 1.03331×1040 | 1.03086×1040 | 1.00238 | 6.95144×1038 | 14.8648 |
36 | 3.71993×1041 | 3.71133×1041 | 1.00232 | 2.46768×1040 | 15.0746 |
37 | 1.37638×1043 | 1.37328×1043 | 1.00225 | 9.00675×1041 | 15.2816 |
38 | 5.23023×1044 | 5.21877×1044 | 1.0022 | 3.37743×1043 | 15.4858 |
39 | 2.03979×1046 | 2.03543×1046 | 1.00214 | 1.30027×1045 | 15.6874 |
40 | 8.15915×1047 | 8.14217×1047 | 1.00209 | 5.13595×1046 | 15.8864 |
41 | 3.34525×1049 | 3.33846×1049 | 1.00203 | 2.08×1048 | 16.0829 |
42 | 1.40501×1051 | 1.40222×1051 | 1.00199 | 8.63181×1049 | 16.2771 |
43 | 6.04153×1052 | 6.02983×1052 | 1.00194 | 3.66844×1051 | 16.4689 |
44 | 2.65827×1054 | 2.65324×1054 | 1.0019 | 1.59573×1053 | 16.6586 |
45 | 1.19622×1056 | 1.19401×1056 | 1.00185 | 7.10087×1054 | 16.8461 |
46 | 5.50262×1057 | 5.49266×1057 | 1.00181 | 3.23083×1056 | 17.0316 |
47 | 2.58623×1059 | 2.58165×1059 | 1.00177 | 1.50231×1058 | 17.2151 |
48 | 1.24139×1061 | 1.23924×1061 | 1.00174 | 7.13583×1059 | 17.3966 |
49 | 6.08282×1062 | 6.07248×1062 | 1.0017 | 3.46081×1061 | 17.5763 |
50 | 3.04141×1064 | 3.03634×1064 | 1.00167 | 1.71307×1063 | 17.7541 |
MacKay's formula seems not to work very well.
Copyright © Ben Bullock 2009-2024. All
rights reserved.
For comments, questions, and corrections, please email
Ben Bullock
(benkasminbullock@gmail.com).
/
Privacy /
Disclaimer