>>>>> "a" == abigail <abigail@foad.org> writes: >> On Thu, Sep 14, 2000 at 10:19:38PM -0500, tayers@bridge.com wrote: >> { >> my @cache = (1); >> sub factorial { >> my $x = shift; >> return undef if $x < 0; >> unless (defined $cache[$x]) { >> my $p = $cache[$#cache]; >> $cache[$_] = $p *= $_ for ($#cache+1..$x); >> } >> return $cache[$x]; >> } >> } a> Given the pace of growth of factorial, using a cache isn't that helpful. a> If you need to do many factorials, you might as well pre calculate all a> of them and save all the conditionals in your sub. Okay. And then I could use Math::BigInt to calculate exact results and not be limited to 170! where my 32-bit machine stops. a> However, if you want a cute little function, that does factorials a> giving exact answers, no matter how large the argument is: a> sub factorial {local $" = "*"; my @a = (1, 1 .. shift); `echo '@a' | bc`} Same method as Rick's. Neat. a> (Granted, it returns the wrong results for arguments < 0, but so does the a> above code.) You lost me here. I am not a mathematician. I learned enough math so they would give me my diploma and got the hell out of there. But I thought factorials are not defined for N < 0. So I thought that returning 'undef' if $x < 0 would be okay. I'm definitely curious. Hope you have a very nice day, :-) Tim Ayers (tayers@bridge.com) ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe