> (Please note that running time increasing > dramatically as n increases, making the code only > practical for n <= 4.) > > #!/usr/bin/perl -l > sub f {$_[0]<2?$_[0]:$_[0]*f($_[0]-1)} > sub g { > $_[2]=$_[0]**$_[1]; > $_[2]-=(f($_[0])/(f($_[0]-$_)*f($_)))*g($_,$_[1])for 1..$_[0]-1; > $_[2] > } > print g(f($ARGV[0]),$ARGV[1])/f($ARGV[0])**$ARGV[1]
Your version repeatedly calculates g(i,x) for 1<=i<n!. How about this version, which avoids this: #!/usr/bin/perl -l use bignum; sub f {$_[0]<2?$_[0]:$_[0]*f($_[0]-1)} sub g { #$_[2]=new Math::BigFloat($_[0])**$_[1]; $_[2]=($_[0]+0.1-0.1)**$_[1]; $_[2]-=(f($_[0])/(f($_[0]-$_)*f($_)))*$g[$_]for 1..$_[0]-1; $_[2] } $g[$_]=g($_,$ARGV[1])for 1..f($ARGV[0]); print $g[f($ARGV[0])]/f($ARGV[0])**$ARGV[1] Also note I used bignum, because you want that when you're dealing with numbers this size. Still takes a while, but I think it's a shorter while. - Roie