I need some help with recursive methods.

Here's a method.

public int m1(int n)

{

If(n==1)

return 25;

else

return n + m1(n-1)

}

then you solve for:

m1(6)

alright, so I get you this up to the 25. How does the 25 get there, why wouldn't it be 1+m1(0)? and the answer for the problem is 45. Could someone explain how you get 25 and 45?

6+m1(5)

5+m1(4)

4+m1(3)

3+m1(2)

2+ m1(1)

25

m1(6)==45

thanks.

[471 byte] By [fighterfish13a] at [2007-10-2 18:27:03]
# 1
> How does the 25 get there, why wouldn't it be 1+m1(0)?Because the method is defined that way. Recursion stops when argument value is 1. An in that case, it returns 25.Is there something you don't understand in the if/else statement?
TimTheEnchantora at 2007-7-13 19:48:13 > top of Java-index,Java Essentials,Java Programming...
# 2

> How does the 25 get there

You put it there.

> the answer for the problem is 45

It may be easier to see this if you write your list in the reverse order:m(1) = 25

m(2) = 2 + m(1)= 27

m(3) = 3 + m(2)= 30

m(4) = 4 + m(3)= 34

m(5) = 5 + m(4)= 39

m(6) = 6 + m(5)= 45

pbrockway2a at 2007-7-13 19:48:13 > top of Java-index,Java Essentials,Java Programming...
# 3
oh, I thought it was more complicated, i feel dumb now. Thanks,
fighterfish13a at 2007-7-13 19:48:13 > top of Java-index,Java Essentials,Java Programming...