[Title] set computing Fibonacci number n the function definition of the term as follows - if a function call expression fib (7), the number of times the function is called fib is how much?

Calculating the Fibonacci function definition of n items listed below: 

int fib(int n){ 
     if(n==0) 
        return 1; 
     else if(n==1) 
        return 2; 
     else 
        return fib(n-1)+fib(n-2);
}

Will fib (1) -fib (9) the number of times each function call fib

answer:

For small amounts of data for n

f (1) call 1

f (2) call 3 times

f (3) call 5 times

f (4) calls 9 times

Found a pattern: open start from n = 3, the number of calls for the first two plus one

That is: f (3) = f (2) + f (1) + 1 = 5

      f(4) =f(3) +f(4) +1=9

      f(5) =f(4) +f(3) +1=15

      f(6) =f(5) +f(4) +1=25

      f(7) =f(6) +f(5) +1=41

       f(8) =f(7) +f(6) +1=67

       f(9) =f(8) +f(7) +1=109

Guess you like

Origin blog.csdn.net/qq_15698613/article/details/94022417