I agree, recursive function follows better the mathematical definition and is easier to implement (no useless loop).
Here is an example (if anyone is interested as derpp solved):
sub fibonacci { my $n = shift; return undef if $n < 0; my $f; if ($n == 0) { $f = 0; } elsif ($n == 1) { $f = 1; } else { $f = fibonacci($n-1) + fibonacci($n-2); } return $f; } print fibonacci(25);
In reply to Re^2: fibonacci numbers using subroutine?
by alexbio
in thread fibonacci numbers using subroutine?
by derpp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |