use strict;
use warnings;
use Memoize;
$| = 1;
memoize('f_fenLisesi');
my $LARGE_N = shift || 300_000;
for (\&f_fenLisesi, \&f_mjd, \&f_swampyankee) {
print "7! = ", $_->(7), "\n";
}
time_this( \&f_fenLisesi, 'fenLisesi' );
time_this( \&f_mjd, 'mjd' );
time_this( \&f_swampyankee, 'swampyankee');
exit( 0 );
#--------------------------------------------------+
sub time_this {
my ($cref, $test_name) = @_;
my $start = time();
print "testing $test_name: ";
for (1 .. $LARGE_N) {
$cref->( $_ );
}
printf "%d second(s)\n", time() - $start;
}
#--------------------------------------------------+
sub f_swampyankee {
my ($n) = @_;
my $sum = 0;
for my $i (1 .. $n) {
$sum += $i;
}
$sum;
}
##--------------------------------------------------+
sub f_fenLisesi {
my ($n) = @_;
return 1 if $n == 1;
$n + f_fenLisesi( $n - 1 );
}
##--------------------------------------------------+
my @fact;
BEGIN {@fact = (0)}
sub f_mjd {
my ($n) = @_;
while ($#fact < $n) {
push @fact, @fact + $fact[-1];
}
return $fact[$n];
}
|