Simple recursion description

Here is a recursive static method in Java.

public static int mystery(int m, int n) {
    int result = 1;   

    if (m > 0) {
      result = n * mystery(m-1, n);
    }       

    System.out.println (m + "  " + result);
    return result;
}

What will be printed to standard output if we create a call mystery (3,4) method? What will be the ultimate return value from the call for secrecy (3.4)?

What is the explanation of the answer for the standard output part.

Conclusion:

0 1
1 4
2 16
3 64

The final return value is 64.

+3
source share
4 answers

Consider nboth fixed (which is for all goals and objectives) and f(m)be mystery(m,n).

Then

f(0) = 1
f(1) = n * f(0) = n
f(2) = n * f(1) = n * n
f(3) = n * f(2) = n * n * n

Can you see the big picture? Can you give a closed form for f(n)?

+5
source

Given your code

public static int mystery(int m, int n) {
int result = 1;   

if (m > 0) {
  result = n * mystery(m-1, n);
}       

System.out.println (m + "  " + result);
return result;
}

Let's start with m = 3 and n = 4, try to emulate it, trying to be a debugger ...

mystery(3,4){
   int result = 1
   if(3 > 0){
       result = 4 * mystery(3-1,4);
       //We proceed from this point only after evaluating mystery(2,4)
       mystery(2,4){
            int result = 1
            if(2 > 0){
                result = 4*mystery(2-1,4);
                //Now we have to evaluate mystery(1,4)
                mystery(1,4){
                    int result = 1;
                      if(1 > 0){
                          result = 4*mystery(1-1,4);
                          //Evaluate mystery(0,4)
                          mystery(0,4){
                             int result = 1;
                             if(0 > 0){
                                //Not evaluated
                             }
                             System.out.println(0 + " "+1);
                             return 1;
                          }...mystery(0,4) done continue with evaluation of mystery(1,4)
                          result = 4*1 //1 is what is returned by mystery(0,4)
                          System.out.println(1+ "" + 4);
                          return 4; 
                       }//done with the evaluation of mystery(1,4), resume evaluation of mystery(2,4)
                result = 4*4 //4 is the value returned by mystery(1,4)
                System.out.println(2 + " " + 16);
                return 16;            
                }//At this point we are done with evaluating (2,4) and on the way to resume evaluation of mystery(3,4)
       result = 4 * 16
       System.out.println(3 + " "+ 64)
       return 64;
       }
   }

Hope this helps

+2

m n. 64.

?

+1

The first challenge is mystery (3,4), which then causes a secret (2,4), which then causes a secret (1,4), which then causes a secret (0,4). In this example, the base case is secret (0.4), i.e. M> 0 evaluates to false, and result = n * mystery (m-1, n) will not be executed (recursion ends here). The base unit is on top of the call stack, and the bottom of your stack is a mystery (3.4). Rate the top of the call stack at the bottom ...

enter image description here

0
source

All Articles