Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Another recursive method! pretty stuck on this one.

1. ## Another recursive method! pretty stuck on this one.

write a recursive method sum that calculates the sum of the integers between 1 and n, using the following recursive definition: The sum of the values from 1 to n is n plus the sum of the values from 1 to n-1. The method should have one parameter n.

```
public int sum ( int number )
{
if ( number == 1 ) return 1;
else return number + sum(number - 1);

}```

It's the equation that's confusing me, so If i were to hypothetically plug in number = 10, how would it return the sum of the integers exactly since return (10) + sum(10-1), in this case, what is sum? Sorry if i'm not making much sense, i'm a little confused on how to approach this one.

2. ## Re: Another recursive method! pretty stuck on this one.

the sum is: 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 so it is 55.
how?
first you are going to call that method somewhere in your code.
let say sum(10);.
are you familiar with stack in programming? Stack (abstract data type) - Wikipedia, the free encyclopedia
every time you call a method, pass a parameter to that method, the program will push a value(usually a memory address). And the memory address that is sure will push to the stack is the memory address of the instruction that calls the method(so the method knows where memory address will it return when it reaches the end of the method) others are (return value and parameter). The statement return number + sum(number - 1); push a value to the stack(the value of sum(number-1).

Let simulate the recursive method.
First call the method with parameter of 10
so n = 10; and the return 1 will execute when n = 1.

since n is not 1, it will return n + sum(n - 1)

here it is:
1 in the return statement: I have return 10 + sum(10 - 1). oh it calls the method again.
2 inside the method sum(10 - 1) it returns 9 + sum(9 - 1) and it calls the method again
3 inside the method sum(9 - 1) it returns 8 + sum(8 - 1) and it calls the method again
4 inside the method sum(8 - 1) it returns 7 + sum(7 - 1) and it calls the method again
5 inside the method sum(7 - 1) it returns 6 + sum(6 - 1) and it calls the method again
6 inside the method sum(6 - 1) it returns 5 + sum(5 - 1) and it calls the method again
7 inside the method sum(5 - 1) it returns 4 + sum(4 - 1) and it calls the method again
8 inside the method sum(4 - 1) it returns 3 + sum(3 - 1) and it calls the method again
9 inside the method sum(3 - 1) it returns 2 + sum(2 - 1) and it calls the method again
10 inside the method sum(2 - 1), number now is 1 so it will return 1 (the last call of the method)

so line 10 returns 1.
that will make line 9 receive 1 and return 2 + 1. so it returns 3 to line 8
line 9 will make line 8 receive 3 and return 3 + 3. so it returns 6 to line 7
line 8 will make line 7 receive 6 andreturn 4 + 6. so it returns 10 to line 6
line 7 will make line 6 receive 10 andreturn 5 + 10. so it returns 15 to line 5
line 6 will make line 5 receive 15 andreturn 6 + 15. so it returns 21 to line 4
line 5 will make line 4 receive 21 andreturn 7 + 21. so it returns 28 to line 3
line 4 will make line 3 receive 28 andreturn 8 + 28. so it returns 36 to line 2
line 2 will make line 1 receive 36 andreturn 9 + 36. so it returns 45 to line 1
so line 1 will return 10 + 45 which is equal to 55.

actually values was pushed in the stack together with other values. you will understand what actually happens if you know low level programming languages like assembly, actually you can see what really happens in stack with your own eyes using a debugger tool like ollydebug,

3. ## Re: Another recursive method! pretty stuck on this one.

I understand how sum(10-1) returns 9 - sum(9-1) and so on...but a little confused on the second part, where you mention the return values in line 9....where does the return 2+1 come from and so on?

4. ## Re: Another recursive method! pretty stuck on this one.

because at line 8, your parameter number is equal to 3 and the program calls again the method sum, so it would be 3 + sum(3 - 1) right?
then that where the line 9 appears, at line 9, your parameter number is now 2 and the program calls again the method sum, so it would be like this in line 9 number + sum(number - 1) or 2 + sum(2 - 1) right? and sum(2 - 1) returns a int value of 1. so line 9 should return 2 + 1 or returns a value of 3.