Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

factorialmath.

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

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


Reason: (buzzcutbuddha) Delete - Smells strongly of homework and no effort shown

For more information on this node visit: this

Replies are listed 'Best First'.
Re: factorialmath.
by BooK (Curate) on Jun 24, 2002 at 13: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 18: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,$/,$@
Re: factorialmath.
by rdfield (Priest) on Jun 24, 2002 at 12:50 UTC
    perlfaq has all the answers.

    rdfield

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://176757]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2020-07-05 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?