retagged by
555 views
0 0 votes
int foo(int n){
if(n<3)
return 1;
else
       return (foo(n-1) + foo(n-3) + 1);
}
Let ‘m’ denote the number of invocations of function foo and ‘n’ denote the return value when the function is called as foo(foo(5)). What is the value of
m - n ?

1 Answer

0 0 votes

first we calculate $f(5)$ which comes out to be 7 and took 7 invocations.
While calculating $f(5)$ we noticed $f(4)$ returns value as 5 and takes 5 invocations.
While calculating $f(5)$ we noticed $f(3)$ returns value as 3 and takes 3 invocations.

so till now, we calculated value of $f(5) = 7$ with 7 invocations. 
now, $f(f(5)) = f(7)$



so total number of invocations = 17 + 7 = 24 and value returned is 17.
so m = 24, n = 17 
m-n = 24-17 = 7
 
or 

you can just use the pattern that number of invocation is equal to the  value returned

 

Position:
Show:

Related questions

28 28 votes
2 answers 2 answers
9.2k
9.2k views
go_editor asked Apr 21, 2016
9,203 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return...
45 45 votes
7 answers 7 answers
12.6k
12.6k views
go_editor asked Feb 16, 2015
12,563 views
Suppose $c = \langle c[0], \dots, c[k-1]\rangle$ is an array of length $k$, where all the entries are from the set $\{0, 1\}$. For any positive integers $a \text{ and } n...
80 80 votes
9 answers 9 answers
32.0k
32.0k views
Misbah Ghaya asked Feb 13, 2015
31,973 views
Consider the following C function.int fun1 (int n) { int i, j, k, p, q = 0; for (i = 1; i < n; ++i) { p = 0; for (j = n; j 1; j = j/2) ++p; for (k = 1; k < p; k = k * 2)...
84 84 votes
12 answers 12 answers
33.2k
33.2k views
go_editor asked Feb 12, 2015
33,196 views
Consider the following C function.int fun(int n) { int x=1, k; if (n==1) return x; for (k=1; k<n; ++k) x = x + fun(k) * fun (n-k); return x; }The return value of $fun(5)$...