If you're writing programs to mangle large numbers recursively, use lisp, don't
use perl w/ Math::BigInt. Check out the following factorial
implementations and runtime.
use Math::BigInt ':constant';
$tot = 1;
for($i=1;$i<=1024;$i++){
$tot *= $i;
}
print "i is $tot\n";
perl 1028$ time !!
time perl bigint.pl
i is +541852879605885728307692194468385473800155396353801344448287027068321061207337660373314098413621458671907918845708980753931994165770187368260454133333721939108367528012764993769768292516937891165755680659663747947314518404886677672556125188694335251213677274521963430770133713205796248433128870088436171654690237518390452944732277808402932158722061853806162806063925435310822186848239287130261690914211362251144684713888587881629252104046295315949943900357882410243934315037444113890806181406210863953275235375885018598451582229599654558541242789130902486944298610923153307579131675745146436304024890820442907734561827369030502252796926553072967370990758747793127635104702469889667961462133026237158973227857814631807156427767644064591085076564783456324457736853810336981776080498707767046394272605341416779125697733374568037475186676265961665615884681450263337042522664141862157046825684773360944326737493676674915098953768112945831626643856479027816385730291542667725665642276826058264393884514911976419675509290208592713156362983290989441052732125187249527501314071676405516936190781821236701912295767363117054126589929916482008515781751955466910902838729232224509906388638147771255227782631322385756948819393658889908993670874516860653098411020299853816281564334981847105777839534742531499622103488807584513705769839763993103929665046046121166651345131149513657400869056334867859885025601787284982567787314407216524272262997319791568603629406624740101482697559533155736658800562921274680657285201570401940692285557800611429055755324549794008939849146812639860750085263298820224719585505344773711590656682821041417265040658600683844945104354998812886801316551551714673388323340851763819713591312372548673734783537316341517369387565212899726597964903241208727348690699802996369265070088758384854547542272771024255049902319275830918157448205196421072837204937293516175341957775422453152442280391372407717891661203061040255830055033886790052116025408740454620938384367637886658769912790922323717371343176067483352513629123362885893627132294183565884010418727869354439077085278288558308427090461075019007184933139915558212752392329879780649639075333845719173822840501869570463626600235265587502335595489311637509380219119860471335771652403999403296360245577257963673286654348957325740999710567131623272345766761937651408103999193633908286420510098577454524068106897392493138287362226257920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
real 0m25.374s
user 0m24.580s
sys 0m0.020s
Now perl with recursive function:
use Math::BigInt ':constant';
print factorial(1024) . "\n";
sub factorial{
my ($n) = @_;
return ($n * factorial($n - 1) );
}
perl 1031$ time perl bigint.pl
<CTRL-C> #i gave up at the 2minute point
real 1m58.452s
user 1m53.370s
sys 0m0.810s
Now for the lisp implementation and runtime:
(defun factorial (x) ( if (= 1 x ) 1 (* x (factorial (- x 1) ) )) )
(print(factorial 1024))
(print "~%")
(quit)
lisp 1019$ time !!
time lisp-start factorial.lisp
; Loading #p"/home/hallerp/devel/lisp/factorial.lisp".
Warning: Declaring A special.
541852879605885728307692194468385473800155396353801344448287027068321061207337660373314098413621458671907918845708980753931994165770187368260454133333721939108367528012764993769768292516937891165755680659663747947314518404886677672556125188694335251213677274521963430770133713205796248433128870088436171654690237518390452944732277808402932158722061853806162806063925435310822186848239287130261690914211362251144684713888587881629252104046295315949943900357882410243934315037444113890806181406210863953275235375885018598451582229599654558541242789130902486944298610923153307579131675745146436304024890820442907734561827369030502252796926553072967370990758747793127635104702469889667961462133026237158973227857814631807156427767644064591085076564783456324457736853810336981776080498707767046394272605341416779125697733374568037475186676265961665615884681450263337042522664141862157046825684773360944326737493676674915098953768112945831626643856479027816385730291542667725665642276826058264393884514911976419675509290208592713156362983290989441052732125187249527501314071676405516936190781821236701912295767363117054126589929916482008515781751955466910902838729232224509906388638147771255227782631322385756948819393658889908993670874516860653098411020299853816281564334981847105777839534742531499622103488807584513705769839763993103929665046046121166651345131149513657400869056334867859885025601787284982567787314407216524272262997319791568603629406624740101482697559533155736658800562921274680657285201570401940692285557800611429055755324549794008939849146812639860750085263298820224719585505344773711590656682821041417265040658600683844945104354998812886801316551551714673388323340851763819713591312372548673734783537316341517369387565212899726597964903241208727348690699802996369265070088758384854547542272771024255049902319275830918157448205196421072837204937293516175341957775422453152442280391372407717891661203061040255830055033886790052116025408740454620938384367637886658769912790922323717371343176067483352513629123362885893627132294183565884010418727869354439077085278288558308427090461075019007184933139915558212752392329879780649639075333845719173822840501869570463626600235265587502335595489311637509380219119860471335771652403999403296360245577257963673286654348957325740999710567131623272345766761937651408103999193633908286420510098577454524068106897392493138287362226257920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
real 0m0.298s
user 0m0.190s
sys 0m0.070s