Recursive function be converted into a non-recursive function [closed]?

This is always possible, because you can emulate the call stack yourself. However, it's not always easy to do. The easy cases are tail recursion: these don't even require a stack.

For example, this guy is trivially converted into a for loop: def countdown(n): if n >= 0: print n countdown(n-1) Even if the function is not strictly tail-recursive, you can sometimes still get away without a stack. For example, the classical factorial function: def factorial(n): if n = pivot) return left + pivot + right Although it's definitely possible to do this without recursion, you'll have to build a stack yourself, and construct a while loop that iterates until the stack is empty.

Yes, it's always possible to convert a recursive function into a non-recursive one. You can prove this with a simple thought experiment: you can implement a simple non-recursive interpreter which emulates a Turing-complete register machine with a stack (basically the equivalent of a simple microprocessor). This is then a non-recursive piece of code that can run any recursive function.

Note that in all non-trivial cases of recursion (i.e. More complex than simple tail recursion which can just be converted into a loop), you will need to somehow capture the information contained in nested stack frames. In general, this can be done by emulating a variable-sized stack using heap memory, but it's also possible to use other techniques (e.g. Continuations or queues).

Yes every Recursive function can be converted to an iterative function.

All recursion functions can be converted to iteration functions, however it is not that simple. It is only simple in the case of tail recursion. This is when only one recursion call happens and it happens at the end.

This can be simply converted to a do-while statement. For other cases it isn't that simple. While it is true that all recursion can be turned to iteration you will need to simulate your own stack in the more complex examples.

It's always possible to turn recursion into iteration using a stack. Instead of making a recursive call with some parameters, you push the set of parameters on the stack. In each iteration, the top set of parameters is popped and handled.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions