Making a recursive function iterative

Viewed 121

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;

}
3 Answers

You need to remember the last three values and compute a new one each time in terms of the last one.

public static BigInteger iter(int n) {
    BigInteger a = BigInteger.valueOf(1);
    BigInteger b = BigInteger.valueOf(3);
    BigInteger c = BigInteger.valueOf(5);
    switch (n) {
        case 0: return a;
        case 1: return b;
        case 2: return c;
        default:
            for (int i = 2; i < n; i++) {
                BigInteger next = c.multiply(b.pow(2)).multiply(a.pow(3));
                a = b;
                b = c;
                c = next;
            }
            return c;
    }
}

Note this is O(n) instead of O(n^3)

To give you a hint:

Initialize an array of size n which will hold the answers. For example, ith index will store the answer for a_i. Initialize a_0, a_1 and a_2 to the values given to you (1,3 and 5 in your case). Now start iterating from index 3 onwards and use your formula to calculate a_i.

You have to store your last three results in three variables and apply the formula on these. Below you can find a simplified example using int. You can enhance this code by using BigInteger so it will work for larger numbers as well.

static int compute_iterative(int n) {
    if (n == 0) return 1;
    if (n == 1) return 3;
    if (n == 2) return 5;

    int a_n3 = 1;
    int a_n2 = 3;
    int a_n1 = 5;
    int a_n = a_n1;
    int i = 3;
    while (i <= n) {
        a_n = a_n1 * (int) Math.pow(a_n2, 2) * (int) Math.pow(a_n3, 3);
        a_n3 = a_n2;
        a_n2 = a_n1;
        a_n1 = a_n;
        i++;
    }
    return a_n;
}

Version using BigInterger:

static BigInteger compute_iterative(int n) {
    if (n < 0) {
        throw new IllegalArgumentException("Unsupported input value: " + n);
    }
    final BigInteger[] values = { BigInteger.valueOf(1), BigInteger.valueOf(3), BigInteger.valueOf(5) };
    if (n < values.length) {
        return values[n];
    }
    int i = 3;
    while (i <= n) {
        final BigInteger result = values[2].multiply(values[1].pow(2)).multiply(values[0].pow(3));
        values[0] = values[1];
        values[1] = values[2];
        values[2] = result;
        i++;
    }
    return values[2];
}
Related