Sunday, February 07, 2010

10000 Factorial in perl

Using perl, here is the shortest way to calculate the factorial of a big number like 10000

use strict;
use Math::BigInt;

sub fact {
my ($num) = @_;
return 1 if (($num == 1) or ($num == 0));
return $num * fact($num - 1);
}

my $init = time();
print Math::BigInt->new($ARGV[0])->bfac();
my $last = time();

print "Number of minutes: " . (($last - $init)/60) . "\n";

No comments: