6 6 votes int func(int n) { if(n<3) return 1; else return func(n-1)+func(n-3)+1; } How many invocations for calculating func(func(5)) Programming in C programming-in-c recursion + – dd 1.6k views answer comment Share Follow Print See all 3 Comments 3 3 Comments reply Kapil commented Dec 23, 2016 reply Follow flag Do we need to save the function calls in a table after calculating or every function call is a fresh call ? 0 0 replyShare Arjun commented Dec 23, 2016 reply Follow flag Every call is a fresh - there is no saving in the given code. 1 1 replyShare Kapil commented Dec 23, 2016 reply Follow flag Yes, Sir right and also they have not mentioned anything :P 1 1 replyShare Please log in or register to add a comment.
Best answer 9 9 votes $F(5) = F(4) + F(2) + 1$ $F(4) = F(3) + F(1) + 1$ $F(3) = F(2) + F(0) + 1$ Hence, Solving all gives $F(5) = 7$ with $3$ distinct invocations. $F(7) = F(6) + F(4) + 1$ $F(6) = F(5) + F(3) + 1$ $F(5) = F(4) + F(2) + 1$ $F(4) = F(3) + F(1) + 1$ $F(3) = F(2) + F(0) + 1$ And, Solving all gives $F(7) = 17$ with $8$ invocations, assuming there is no saving in the above code. Or, Kapil answered Dec 23, 2016 • edited Dec 23, 2016 by dd Kapil comment Share Follow See all 4 Comments 4 4 Comments reply dd commented Dec 23, 2016 reply Follow flag Will add a art work later :) 1 1 replyShare Kapil commented Dec 23, 2016 reply Follow flag Yes, Sure :) 0 0 replyShare Subham Nagar commented Jul 11, 2018 reply Follow flag why should not it be 24 as first func(5) will be calculated with 7 calls, then func(7) with 17 calls total invocations = 17+7= 24 where am i wrong? 0 0 replyShare codelite commented Jun 14, 2020 reply Follow flag The answer will be 8??.( After including func(func(5)) call. ) 0 0 replyShare Please log in or register to add a comment.
–1 –1 vote 17 function calls required navya n answered Sep 15, 2017 navya n comment Share Follow 0 reply Please log in or register to add a comment.