I'm doing a Java exercise that will print out the nth number in a sequence number. I have just completed the normal sequence number in an array like 1,2,3,4,5,6,7,8,9,10,...So if the n=20, it print out 20 for this sequence of number.
Now, I would like to print the nth number in a sequence of number as below:
Start with a(0) = 0
The next index is #1 (odd), so add 0 + 1 = 1
The next index is #2 (even), so multiply 1 x 2 = 2
The next index is #3 (odd), so add 2 + 3 = 5
The next index is #4 (even), so multiply 5 x 4 = 20
The next index is #5 (odd), so add 20 + 5 = 25
Basically, if the index is odd, you add to the prior term. If the index is even, you multiply by the prior term.
The pattern as follows: 0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, 2127230, 2127245, 34035920, 34035937, 612646866 and so on...
The problem is, I don't know how to store those type of sequence number so that I can print the nth number. I'm stuck until:
if ( number1 % 2 == 0)
{
number1 = number1 * (1-number1);
}
else
{
number1 = number1 + (1-number1);
}
Thanks in advance.