Pathologically Eclectic Rubbish Lister PerlMonks

factorialmath.

by NodeReaper (Curate)
 on Jun 24, 2002 at 12:44 UTC Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

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

Create A New User
Domain Nodelet?
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 exploiting the Monastery: (2)
As of 2023-06-05 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How often do you go to conferences?

Results (22 votes). Check out past polls.

Notices?