I have this problem in front of me and I can’t figure out how to solve it. This is about the series 0,1,1,2,5,29,866...(Each number, except the first two, is the sum of the squares of the previous two numbers (2^2+5^2=29)). In the first part I had to write an algorithm (not a native speaker, so I really don’t know the terminology), which would get a place in the series and return its value ( 6 returned 29) Here is how I wrote it:
public static int mod(int n)
{
if (n==1)
return 0;
if (n==2)
return 1;
else
return (int)(Math.pow(mod(n-1), 2))+(int)(Math.pow(mod(n-2), 2));
}
However, now I need the algorithm to get the number and return the total amount before it in the series (6- 29+5+2+1+1+0=38) I do not know how to do this, I try, but so far I really can not understand the recursion, even if I wrote something correctly, How can I check this to be sure? And how to achieve the correct algorithm?
.
!