#!/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 = "
%3d | " . ("%10g | " x 5) . "
\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$1!g;
print "$out";
}