GATE CSE
First time here? Checkout the FAQ!
x
+4 votes
158 views
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))

 

asked in Programming by Veteran (48.5k points)   | 158 views
Do we need to save the function calls in a table after calculating or every function call is a fresh call ?
Every call is a fresh - there is no saving in the given code.
Yes, Sir right and also they have not mentioned anything :P

1 Answer

+7 votes
Best answer

$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,

 

answered by Veteran (47.4k points)  
edited by
Will add a art work later :)
Yes, Sure :)


Top Users Jun 2017
  1. Bikram

    3704 Points

  2. Arnab Bhadra

    1502 Points

  3. Hemant Parihar

    1502 Points

  4. Niraj Singh 2

    1481 Points

  5. junaid ahmad

    1432 Points

  6. Debashish Deka

    1384 Points

  7. Rupendra Choudhary

    1220 Points

  8. rahul sharma 5

    1220 Points

  9. Arjun

    1158 Points

  10. srestha

    1006 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 26 - Jul 02
  1. Arjun

    198 Points

  2. akankshadewangan24

    152 Points

  3. Debashish Deka

    138 Points

  4. Hira Thakur

    130 Points

  5. Soumya29

    104 Points


23,399 questions
30,111 answers
67,489 comments
28,424 users