[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Factorial Elegance



On Thu, Sep 14, 2000 at 10:19:38PM -0500, tayers@bridge.com wrote:
> My department gives a short Perl test as part of the interview
> process. A new version was presented yesterday so I thought I'd take
> it for fun. One of the questions was to write a function to return
> x-factorial given x.
> 
> I jotted down the following (with a little tweaking afterward). It's
> not tricky or obfuscated and it even works with 'use strict;' ;-), but
> I think it's a nifty, efficient implementation due to a several
> conveniences Perl provides.  

Oh, much too much code.

use Tie::Math qw(f X);
tie %factorial, 'Tie::Math', sub { f(X) = X * f(X-1) },
                             sub { f(0) = 0;  f(1) = 1; };
print $factorial{10}, "\n";

-- 

Michael G Schwern      http://www.pobox.com/~schwern/      schwern@pobox.com
Just Another Stupid Consultant                      Perl6 Kwalitee Ashuranse
Cheating is often more efficient.
        - Seven of Nine

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe