ProgrammingHardGATE CS · Campus placements

int f(int n) { return n < 2 ? 1 : f(n - 1) + f(n - 2); } How many calls to f happen in total, counting the first call, when evaluating f(5)?

  1. A. 9
  2. B. 11
  3. C. 13
  4. D. 15

Correct answer: D. 15

Let c(n) be the call count. c(0) = c(1) = 1 and c(n) = 1 + c(n-1) + c(n-2), so c(2)=3, c(3)=5, c(4)=9, c(5)=15. This explosion is why naive recursion is exponential.

More C Programming questions

Exams that ask this topic