Testing Stirling's approximation

This program tests the Stirling's approximation formula given in Stirling's approximation at Wikipedia that Stirling's approximation is

$n! \sim \sqrt{2 \pi n} \left(\frac{n}{e}\right)^n$

and in the book Information Theory, Inference, and Learning Algorithms by David MacKay that Stirling's approximation is

$x! \simeq x^{x} \, e^{-x}$

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+)!&times;10<sup>$1</sup>!g;
    print "$out";
}

(download)

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
103.6288×1063.5987×106 1.00837 453999 7.99296
113.99168×1073.96156×107 1.00764.76519×106 8.37675
124.79002×1084.75687×108 1.006975.47824×107 8.74371
136.22702×1096.18724×109 1.006436.84598×108 9.09589
148.71783×10108.6661×1010 1.005979.23995×109 9.43493
151.30767×10121.30043×1012 1.005571.33953×1011 9.76221
162.09228×10132.08141×1013 1.005222.07591×1012 10.0789
173.55687×10143.53948×1014 1.004913.42472×1013 10.3859
186.40237×10156.3728×1015 1.004645.99245×1014 10.6841
191.21645×10171.21113×1017 1.00441.10847×1016 10.9742
202.4329×10182.42279×1018 1.004182.16128×1017 11.2568
215.10909×10195.08886×1019 1.003984.43018×1018 11.5325
22 1.124×10211.11975×1021 1.003799.52402×1019 11.8017
232.5852×10222.57585×1022 1.003632.14273×1021 12.065
246.20448×10236.18298×1023 1.003485.03503×1022 12.3226
251.55112×10251.54596×1025 1.003341.2335×1024 12.575
264.03291×10264.02001×1026 1.003213.14522×1025 12.8224
271.08889×10281.08553×1028 1.003098.33433×1026 13.0651
283.04888×10293.03982×1029 1.002982.29181×1028 13.3034
298.84176×10308.81639×1030 1.002886.53134×1029 13.5374
302.65253×10322.64517×1032 1.002781.92665×1031 13.7676
318.22284×10338.20076×1033 1.002695.87602×1032 13.9939
322.63131×10352.62447×1035 1.002611.85087×1034 14.2166
338.68332×10368.66142×1036 1.002536.01509×1035 14.4359
342.95233×10382.9451×1038 1.002452.01498×1037 14.6519
351.03331×10401.03086×1040 1.002386.95144×1038 14.8648
363.71993×10413.71133×1041 1.002322.46768×1040 15.0746
371.37638×10431.37328×1043 1.002259.00675×1041 15.2816
385.23023×10445.21877×1044 1.00223.37743×1043 15.4858
392.03979×10462.03543×1046 1.002141.30027×1045 15.6874
408.15915×10478.14217×1047 1.002095.13595×1046 15.8864
413.34525×10493.33846×1049 1.00203 2.08×1048 16.0829
421.40501×10511.40222×1051 1.001998.63181×1049 16.2771
436.04153×10526.02983×1052 1.001943.66844×1051 16.4689
442.65827×10542.65324×1054 1.00191.59573×1053 16.6586
451.19622×10561.19401×1056 1.001857.10087×1054 16.8461
465.50262×10575.49266×1057 1.001813.23083×1056 17.0316
472.58623×10592.58165×1059 1.001771.50231×1058 17.2151
481.24139×10611.23924×1061 1.001747.13583×1059 17.3966
496.08282×10626.07248×1062 1.00173.46081×1061 17.5763
503.04141×10643.03634×1064 1.001671.71307×1063 17.7541

MacKay's formula seems not to work very well.


Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer