Skip to main content

The Way Recursion Works

Recursive function is the function that, as part of its execution, it invokes itself.

For example, if we at a function fact(n) that does factorial of number n.

   fact(1) = 1
   fact(2) =2 * 1
   fact(3) =3 * 2 * 1
   fact(4) =4 * 3 * 2 * 1
   fact(5) =5 * 4 * 3 * 2 * 1
   ......

We can define the above lines like this:

   fact(1) = 1
   fact(2) =2 * fact(1)
   fact(3) =3 * fact(2)
   fact(4) =4 * fact(3)
   fact(5) =5 * fact(4)
    ........

 So, we can say that,
  fact(n) = n * fact(n-1)   is the formula to find the factorial of any number.

Every recursive function has two parts:
Base Case: It terminates the recursive process.
Recursive Case: Where the recursion actually occurs. In other words, where it calls the function itself. From this case, the program keeps running, means it keeps calling the function.
To terminate the program from this state, we the base case. When, program faces the base case, it stops calling the function.

In this example for base case , we can assume when n is 1, it will return 1.
Else, it will recall the function and for this, we can plug the factorial calculation formula we found above.

Python code for factorial calculation is given below:


Let's take another example of finding the Fibonacci series:
In this example for base case , we can assume

fibonacci(1)==1 and fibonacci(0) == 0
 
Else, it will recall the function and for this,

fibonacci(5) = fibonacci(4) + fibonacci(3)

fibonacci(3) = fibonacci(2) + fibonacci(1)

fibonacci(4) = fibonacci(3) + fibonacci(2)

fibonacci(2) = fibonacci(1) + fibonacci(0)


So, we can say that,
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)  is the formula to find Fibonacci series.

Now let's look at the python code for this:

 
GitHub Link for the code is here

Comments

Popular posts from this blog

DFS Performance Measurement

Completeness DFS is not complete, to convince yourself consider that our search start expanding the left subtree of the root for so long path (maybe infinite) when different choice near the root could lead to a solution, now suppose that the left subtree of the root has no solution, and it is unbounded, then the search will continue going deep infinitely, in this case , we say that DFS is not complete. Optimality  Consider the scenario that there is more than one goal node, and our search decided to first expand the left subtree of the root where there is a solution at a very deep level of this left subtree , in the same time the right subtree of the root has a solution near the root, here comes the non-optimality of DFS that it is not guaranteed that the first goal to find is the optimal one, so we conclude that DFS is not optimal. Time Complexity Consider a state space that is identical to that of BFS, with branching factor b, and we start the search fro...

Difference between a Singly LinkedList and Doubly LinkedList

Difference between abstract class and interface in OOP

Source: Amit Sethi In Interface: > All variables must be public static final. > No constructors. An interface can not be instantiated using the new operator.   > All methods must be public abstract .