Doubt with recursiveness

Asked

Viewed 238 times

7

I took a table test but the results will never come 0 because I subtract 1 of n but then adds the result with n. The result of this question was 36 and I don’t understand why.

public class Recursivo{

    public static void main(String[] args) {

        int result = sum(8);

        System.out.println(result);
    }

    public static int sum(int n){
        if( n == 0){
            return 0;
        }
        else
        {
            return n + sum( n - 1);
        }
    }
}

What really does it?

return n + sum( n - 1);      
  • Why should there be 0?

  • It arrives at 0 yes. The line you are in doubt is the most important, there calling the recursive function. 0 is the stop point, it subtracts the number reported from 1 to 1 until 0.

  • @Georgewurthmann AP refers to the result of n + sum( n - 1);

2 answers

7

What really does it?

return n + sum( n - 1);    

The method sum() keeps calling itself always passing as parameter the very number that received as argument minus one, until that number reaches zero that will be when it returns zero. That is, if you pass the number 8, it will return 8 plus the return method sum(7), which will return 7 plus the return of the method sum(6), which will return 6 plus the return of the method sum(5), that will return 5 more.... until reaching 0, ie each time you call the method sum(), it sums this value with all its lower values to zero.

For the case in question the return would be: 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0, that is to say: 36.

To try to debug, I put some prints to show what happens in each iteration:

public class Recursivo{

    public static void main(String[] args) {

        int result = sum(8);

        System.out.println("Resultado final: " + result);
    }

    public static int sum(int n){
        if( n == 0){
            System.out.println("Fim da recursividade");
            return 0;
        }
        else
        {
            System.out.println("O valor de n nessa iteração é: " + n);
            int ret =  n + sum( n - 1);
            System.out.println("O valor a ser retornado nessa iteração é: " + ret);
            return ret;
        }
    }
}

Upshot:

The value of n in this iteration is: 8
The value of n in this iteration is: 7
The value of n in this iteration is: 6
The value of n in this iteration is: 5
The value of n in this iteration is: 4
The value of n in this iteration is: 3
The value of n in this iteration is: 2
The value of n in this iteration is: 1
End of recursion
The value to be returned in this iteration is: 1
The value to be returned in this iteration is: 3
The value to be returned in this iteration is: 6
The value to be returned in this iteration is: 10
The value to be returned in this iteration is: 15
The value to be returned in this iteration is: 21
The value to be returned in this iteration is: 28
The value to be returned in this iteration is: 36
Final result: 36

Realize there are two prints within the else in the method sum(), but only the first is shown until the end point of the recursion is reached, which is when n comes to 0, and instead of calling the method itself again it simply returns a value.

  • Yes, but he’s adding n + sum( n - 1); I don’t understand why he adds up...

  • 1

    Yes, it adds up n, which in this case is 8, with the value that returns a new call to the method sum(), which in this case will be sum(7), which in turn will make new calls, until it will add up to 0 and stops calling the method sum(). I will try to explain better in the answer.

7


return n + sum( n - 1):

In this line is added the value of n with the value resulting from the method sum(n-1).

In the example:

sum(8) = 8 + sum(7)
sum(7) = 7 + sum(6)
sum(6) = 6 + sum(5)
sum(5) = 5 + sum(4)
sum(4) = 4 + sum(3)
sum(3) = 3 + sum(2)
sum(2) = 2 + sum(1)
sum(1) = 1 + sum(0)
sum(0) = 0.


Notice that sum(0) does not call the method sum again. This is the stopping point of the recursive method. When recursiveness finds the stopping point, just go back over the whole string to add up the values and get the return.

This replaces the values:

sum(0) = 0.
sum(1) = 1 + sum(0) = 1 + 0  = 1
sum(2) = 2 + sum(1) = 2 + 1  = 3
sum(3) = 3 + sum(2) = 3 + 3  = 6
sum(4) = 4 + sum(3) = 4 + 6 = 10
sum(5) = 5 + sum(4) = 5 + 10 = 15
sum(6) = 6 + sum(5) = 6 + 15 = 21
sum(7) = 7 + sum(6) = 7 + 21 = 28
sum(8) = 8 + sum(7) = 8 + 28 = 36

Analyzing the method sum more closely, it sums up all the numbers of 1 until n, in the given example: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36

  • Great response, especially the first example that made it clear that the method calls keep piling up until it returns something and interrupts recursion, +1

  • Ahnnnnn I get it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.