#!/bin/perl -w # calculates e using the series: # # e = SUM (n=0, n->infinity) 1/n! # # use warnings; use strict; my $iterations = $ARGV[0] || 17; #17 iterations maxes out my build's precision my $cx = 1; my $total = 1; #e's sequence starts with a constant 1. sub factorial { # brought to you by the kind and knowledgable folks at my $n = 1; # http://timjoh.com/writing-a-factorial-subroutine-in-perl/ $n *= $_ for 2..shift; #Thanks, Tim! return $n; } while ($cx <= $iterations) { $total += 1/factorial($cx++); print "$total\n"; }