# Recursion VS Iteration – An Analysis with fibonacci and factorial

It is always difficult to choose one over the other , but recursive and iterative methods can be chosen wisely by analysing the algorithm with certain input values. Let us study the usage of recursive methods and let us analyse how recursive call works internally.

How Recursion Works ?

The concept of stack frame comes into picture when we deal with recursion, for each call a stack frame will be created and the values will be pushed into stack. Once the recursive call reach the final condition, the program will start popping out from the stack frame and returns the values . A C program to find factorial of given number and analyse it using recursive method.

Let us take an example –

Finding Factorial of a Number

long int count = 1; long int factorial(long int n){ count++;//to count how many times the method getting called if (n == 0) { return 1; } return n * factorial(n - 1); } int main(int argc, const char * argv[]) { int n = 13; printf("Factorial of %d = %lu , number of time called %lu\n",n,factorial(n),count); return 0; }

Let Analyse how it works ?

When factorial(n) will be called every time , a stack frame will be created and the call will be pushed to stack, the entire call stack looks like below. The below image shows stack operations for a given recursive call.

If I give input as 10, the recursive method will be called 12 times,

If I give input as 14, the recursive method will be called 16 times,

If I generalise the number time of call then it will be – n + 2 times.

This looks ok, but let us take different example and try to compare both. A program to find fibonacci number and analyse it with respect to call stack.

Finding Fibonacci number

long int count = 1; long int fibonacci(int n){ count++; if (n <= 1) { return n; }else{ return fibonacci(n-1) + fibonacci(n - 2); } } long int fibonacciIteration(int n) { int x = 0, y = 1, z = 1; for (int i = 0; i < n; i++) { x = y; y = z; z = x + y; } return x; } int main(int argc, const char * argv[]) { int n = 4; printf("fib value recursive %lu ",fibonacci(n)); n = 100; // let us give 100 as input for iterative method printf("\nIterative method %lu ",fibonacciIteration(100)); printf("\nnumber of time called %lu\n",count); return 0; }

Now the call stack looks as follows, memory layout of call stack for a given recursive call of fibonacci program.

Let us analyse this example ,

If we observe the callstack , fib(2) , Fib(1) and Fib(0) called multiple times, which is not necessary as the value will be calculated .

For input n = 4 , the total number of times the fibonacci method called = 10.

Now let us take few more inputs and check how many times the function is getting called.

n = 5 => 16 times,

n = 6 => 26 times ,

If I generalise the number of time the function called will give us an astonishing result ,

number of times fib function calls = (2^n – C), Where C is constant, and very less ~ 2^n

Let us take some interesting inputs 🙂

n = 30 => 2692538 times

n = 31 => 4356618 times (just by increasing n by 1 , the number of times function called became so huge)

n = 42 => 866988874 times , Unbelievable !!! These results shows how recursive method is dangerous in this case.

n = 43 => my program not even stopping , even after 5 minutes .

Let us look on iterative method

long int fibonacciIteration(int n) { int x = 0, y = 1, z = 1; for (int i = 0; i < n; i++) { x = y; y = z; z = x + y; } return x; }

This method will return , fibonacci value within few seconds even if input n = 100 .

Conclusion is , selecting between iterative and recursive methods should be decided based on the problem statement, the problem looks simple with simple inputs, but analysing the problem with bigger inputs will actually shows the big picture .

Update: Complexity of recursive Fibonacci –

The recursive equation for this algorithm is T(n)=T(n−1)+T(n−2)+Θ(1)

For this it will be T(n)=Θ(ϕn)

where ϕ is the golden ratio (ϕ=(1+5√) / 2).

I strongly recommend to read Introduction to Algorithms 3rd edition : Purchase in FlipKart