Come for the quick hacks, stay for the epiphanies. PerlMonks

by Dominus (Parson)
 on Jun 01, 2004 at 22:19 UTC Need Help??

```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];
}

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found