I just got this question on an interview and had no idea how to calculate the answer.
How many additional function calls does fib(n) require if “LINE 3” is removed? The answer should be in terms on n.
int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
if(n == 2) return 1; //LINE 3 HERE <---
return fib(n - 1) + fib(n - 2);
}
It can be easily calculated. The old code:
The new code:
The difference is computed simply by subtracting those two:
Which means the difference is a Fibonacci sequence beginning with 0,0,2. It is also possible to calculate a closed form expression for it.