in reply to Fibonacci Numbers
Such a strategy stores the results of previous iterations for later use, so you only have to calculate each result once. Once you do, it's stored (behind the scenes) in a hash which reconstructs it on demand. This makes things much faster.# Compute Fibonacci numbers, straight from Memoize docs use Memoize; memoize('fib'); sub fib { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); } my $foo = fib(24);
enjoy!sub fib{int(((((1+sqrt(5))/2)**$_[0])/sqrt(5))+.5)}
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Fibonacci Numbers
by crashtest (Curate) on Feb 10, 2005 at 07:29 UTC | |
Re^2: Fibonacci Numbers
by blazar (Canon) on Feb 10, 2005 at 08:23 UTC | |
by dReKurCe (Scribe) on Feb 10, 2005 at 22:56 UTC | |
by lidden (Curate) on Feb 10, 2005 at 23:33 UTC | |
by blazar (Canon) on Feb 11, 2005 at 09:39 UTC | |
Re^2: Fibonacci Numbers
by ihb (Deacon) on Feb 10, 2005 at 23:49 UTC | |
by jweed (Chaplain) on Feb 15, 2005 at 04:53 UTC |