Question: Consider the following recursive function: public static int mystery(int a, int b) { if (b == 0) return 0; if (b % 2 == 0)

Consider the following recursive function:
public static int mystery(int a, int b)
{
if (b == 0) return 0;
if (b % 2 == 0) return mystery(a+a, b/2);
return mystery(a+a, b/2) + a;
}
What are the values of mystery(2, 25) and mystery(3, 11)? Given positive
integers a and b, describe what value mystery(a, b) computes. Then answer the
same question, but replace + with * and return 0 with return 1.

Step by Step Solution

3.49 Rating (152 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The mystery function appears to be a recursive implementation of the multiplication operation Here i... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithm Design Questions!