Assignment: Compute an according to the formulas:
- a0 = 1
- a1 = 3
- a2 = 5
- an = an-1 * a2n-2 * a3n-3
I am having trouble making the function iterative. I figured out how to do it recursively. How would I go about doing it specifically for this task and just in general?
My code for the recursive:
public static BigInteger recurs(int bigInteger){
BigInteger sum;
if (bigInteger == 0) {
sum = new BigInteger(String.valueOf("1"));
} else if (bigInteger == 1) {
sum = new BigInteger(String.valueOf("3"));
} else if (bigInteger == 2) {
sum = new BigInteger(String.valueOf("5"));
} else {
sum = recurs(bigInteger-1).multiply(recurs(bigInteger-2).pow(2).multiply(recurs(bigInteger-3).pow(3)));
}
return sum;
}