## Making a recursive function iterative

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; }

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)

**Replace Recursion with Iteration,** Moreover, iterative solutions are usually more efficient than recursive preserve any local fields during the recursive call, which makes Refactoring much easier. In case you don’t know, iteration is the use of a looping construct like a while loop, for loop, etc in order to solve a problem, whereas recursion is the use of a function that calls itself to solve a problem. The reason that iteration can be used to solve any problem that recursion solves is because recursion uses the call stack behind the scenes, but a call stack can also be implemented

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.

**Way to go from recursion to iteration,** A tail-recursive function is very easily converted to an iterative one. Just make the accumulator variable a local one, and iterate instead of Drop a large input into a recursive algorithm in Python, and you’ll probably hit the runtime’s recursion limit. Raise the limit, and you may run out of stack space and segfault. These are not happy outcomes. Therefore, an important trick of the trade is knowing how to translate recursive algorithms into iterative algorithms.

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]; }

**How to make a recursive function iterative,** All recursive functions can be converted to iteration by simulating the stack to store memory because of the overhead of creating and maintaining stack frames. The concept of Recursion and Iteration is to execute a set of instructions repeatedly. The difference between them is that recursion is simply a method call in which the method being called

**Iteration vs. recursion,** In each case, we store the depth of the tree on the node to make it obvious when we've made one of the right depth. function makeTree(depth) { In recursive function, only termination condition (base case) is specified. Iteration includes initialization, condition, execution of statement within loop and update (increments and decrements) the control variable.

**How to Convert Recursion to Iteration,** Both a recursive and iterative algorithm can be used to solve the same task, For this example, I will be building a function to create an array of I'm a beginner programmer and I came upon this problem which is to find the n th number in the Fibonacci series.. I used to solve the problem using a for loop; today I learned about recursion but there is a problem: when I pass 40 or 41 to the recursive function, it takes a bit of time to calculate it, while in the iterative method it would instantly give me the answers.

**So you want to build a recursive(recursive(recursive…)) function in ,** You've probably been told that you can convert any recursive function into an iterative loop just by using an explicit stack. Your recursive function is ideal to illustrate the structure of recursive search with backtracking. It starts with the check of the exit condition row < n, and proceeds to making search decisions on its level of recursion (i.e. picking a possible position for queen number row).