in reply to Re: fibonacci numbers using subroutine?
in thread fibonacci numbers using subroutine?
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: fibonacci numbers using subroutine?
by JavaFan (Canon) on Aug 20, 2010 at 12:59 UTC | |
by alexbio (Monk) on Aug 20, 2010 at 21:17 UTC | |
by JavaFan (Canon) on Aug 20, 2010 at 22:10 UTC | |
by alexbio (Monk) on Aug 22, 2010 at 10:35 UTC |