Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Problems? Is your data what you think it is?
 
PerlMonks  

factorialmath.

by NodeReaper (Curate)
on Jun 24, 2002 at 08:44 UTC ( [id://176757]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: factorialmath.
by rdfield (Priest) on Jun 24, 2002 at 08:50 UTC
    perlfaq has all the answers.

    rdfield

Re: factorialmath.
by BooK (Curate) on Jun 24, 2002 at 09:17 UTC

    Let the effort be in explaining this code, if the AM ever uses it.

    #!/usr/bin/perl # There is everything you need: # a sentinel, $f{0} = sub { 1 }; # a recursive function, sub f { $f{$n = pop()-1} ||= sub { f($n) }; ($n+1) * $f{$n}->(); } # a call to the recursive function... print f(shift);

    I hope it doesn't need to pass -w and use strict... ;-) And what did he mean, test if the parameter is a number?!

      Student: Umm, well see it has something called a sub, and when you "pop" it it pulls the number and . . .

      Professor: F-

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

Re: Factorial Problem
by tadman (Prior) on Jun 24, 2002 at 14:05 UTC
    Since this clearly isn't homework (cough), here's an idea of how to solve the problem:
    #!/usr/bin/perl package F;sub TIESCALAR{bless[],pop} sub FETCH{$v=$n=pop()->[$|];$v*=--$n while(1<$n);$v}sub STORE{shift()->[0 ]=pop}tie($f,F);$f=pop;print$f,$/,$@

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://176757]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.