Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic


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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2020-07-13 12:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found