0

I am self taught and thought that I understood recursion, but I can not solve this problem:

What is returned by the call recur(12)?

What is returned by the call recur (25)?

 public static int recur (int y)
 {
      if(y <=3)
             return y%4;
      return recur(y-2) + recur(y-1) + 1;
  }

Would someone please help me with understanding how to solve these problems?

RBT
  • 24,161
  • 21
  • 159
  • 240
Texasteach
  • 21
  • 1

3 Answers3

0

First of all, I assume you mean:

public static int recur(int y)

but the results of this method are discovered by placing a print statement at the beginning of the method:

public static int recur(int y)
  {
    System.out.println(y);
      if(y <=3)
             return y % 4;
      return recur(y-2) + recur(y-1) + 1;
  }

I am not sure what you mean by what is returned because there are several returns, though. Anyway, these are the steps to figure this out:

  1. is 12 <= 3? No
  2. recur(10) Don't proceed to the next recursion statement yet
  3. is 10 <= 3? No
  4. recur(8) Don't proceed to the next recursion statement yet
  5. Continue this pattern until y <= 3 is true. Then you return y % 4 (whatever that number may be).
  6. Now you are ready to go to the second recursive statement in the most recent recur() call. So, recur(y - 1).
  7. Is y <= 3? If so, return y % 4. If not, do a process similar to step 1
  8. once you return you add the result of recur(y - 2) + recur(y - 1) + 1. This will be a number of course.
  9. continue this process for many iterations.

Recursion is difficult to follow and understand sometimes even for advanced programmers.

Here is a very common (and similar) problem for you to look into:

Java recursive Fibonacci sequence

I hope this helps! Good luck!

Community
  • 1
  • 1
Genjab
  • 1
  • 1
0

I have removed the modulus there since any nonegative n less than 4 will just become n so I ended up with:

public static int recur (int y)
{
      return y <= 3 ?
             y :
             recur(y-2) + recur(y-1) + 1;
}

I like to start off by testing the base case so what happens for y when they are 0,1,2,3? well the argument so 0,1,2,3 of course.

What about 4? Well then it's not less or equal to 3 and you get to replace it with recur(4-2) + recur(4-1) + 1 which is recur(2) + recur(3) + 1. Now you can solve each of the recur ones since we earlier established became its argument so you end up with 2 + 3 + 1.

Now doing this for 12 or 25 is exactly the same just with more steps. Here is 5 which has just one more step:

recur(5); //=>
recur(3) + recur(4) + 1; //==>
recur(3) + ( recur(2) + recur(3) + 1 ) + 1; //==>
3 + 2 + 3 + 1 + 1; // ==>
10

So in reality the recursion halts the process in the current iteration until you have an answer that the current iteration adds together so I could have done this in the opposite way, but then I would have paused every time I now have used a previous calculated value.
You should have enough info to do any y.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
0

This is nothing more than an augmented Fibonacci sequence. The first four terms are defined as 0, 1, 2, 3. Thereafter, each term is the sum of the previous two terms, plus one. This +1 augmentation is where it differs from the classic Fibonacci sequence. Just add up the series by hand:

0
1
2
3
3+2+1 = 6
6+3+1 = 10
10+6+1 = 17
17+10+1 = 28
...
Prune
  • 76,765
  • 14
  • 60
  • 81