Number of calls for nth Fibonacci number

后端 未结 6 2110
名媛妹妹
名媛妹妹 2020-12-30 12:09

Consider the following code snippet:

int fib(int N)
{
   if(N<2) return 1;
   return (fib(N-1) + fib(N-2));
}

Given that fib

6条回答
  •  春和景丽
    2020-12-30 12:54

    This is a classic problem for solving with Recurrence Relations.

    Specifically, the fibonacci problem has the following parameters:

    f(0) = 1
    f(1) = 1
    f(n) = f(n-1) + f(n-2)
    

    Once you master solving recurrences, you'll have no problem reaching the solution (which, incidently, is exactly the same as fib(n)).

提交回复
热议问题