0

I have to find the output, base case, recursive case, and depth of recursion of this method. I can't figure out how to find the output. This is the given method:

public int m5(int n){       //use n = 6 for initial n
         if(n>=3){
                n += m5(n-3);
                n += m5(n/2);
         }
         return n;
}

1 Answer 1

1

Your algorithm does not converge. You can compute by hand the first 6 values of m5(x) which are:

x  m5
0   0
1   1
2   2
3   4
4   7
5  11
6  21

But now take a look at the computation of m5(7) you will have to compute md5(7-3) = md5(4) = 7. Then after the sum n is 7 + 7 = 14. So in the next call you have to compute md5(14/2) = md5(7) but m5(7) is exactly what we are trying to compute so you have an infinity recursion.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer but how did you get the values of m5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.