Ok... So I have the following program:
public int seq2 (int n)
{
if (n <= 0)
return 1;
else
return seq2 (n - 1) + 2 * n - 1;
}
It gives the following output:
1 2 5 10 17 26 37 50 65 82
So that means seq2(0) = 1 and I see how that happened.
But I don't get the rest :/
For seq2(1), it would just go to the else statement and execute:
return seq2 (1-1) + 2 * 1 - 1
which is 0 + 2*1 -1
which is 2-1 = 1.
So it's doing seq2 (1) all over again?
Just trace it upto seq2(2) to explain please.
also, remove the = in the if(n <= 0) and trace that to seq2(2).. (that would give you all the perfect squares.)
Also... Why is recursion so hard :( !