in reply to Re: Fibonacci Numbers
in thread Fibonacci Numbers

If you stick with the original form of your answer (a recurrance-based one), try Memoizing it, viz.:
But the original form of his answer was not recursive:
sub fib{ ($val0, $val1) =@_; $sum= $val0 + $val1; }

Replies are listed 'Best First'.
Re^3: Fibonacci Numbers
by dReKurCe (Scribe) on Feb 10, 2005 at 22:56 UTC
    Honourable friar, Am I wrong to think that recurrsion is utilized by passing the value of $sum as computed by sub fib back into sub fib? Otherwise, the binet formula is extremely cool. Thanks.
      This is a recursive way to do it but it gets slow quickly for larger values unless you memoze it.
      print "$_ : ", fibo($_), "\n" for 0..23; sub fibo{ my $n = shift; if ( $n == 0){ return 0;} elsif( $n == 1){ return 1;} else { return fibo($n-1) + fibo($n-2);} }
      It is iterative. It is not recursive in that fib() does not call itself.

      FWIW I think that Binet's formula is cool too.