So the following is not optimized fibonacci function:
sub fib {
my ( $n ) = @_;
return 1 if( $n == 1 || $n == 2 );
return fib( $n - 1 ) + fib( $n - 2 );
}
I actually printed the number of times fib(3)
was called when we try to find the fibonacci of 20.
It is 2584. fib(2)
is called 4181.
As far as I know this algorithm shows exponential behavior.
My question is how could I calculate that fib(3)
would have been called 2584 times without actually keeping a counter in the code and printing the function calls? How is the exponential part calculated?