Recursion Factorial

 Recursion 

                                        is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Recursion occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, where a function being defined is applied within its own definition. While this apparently defines an infinite number of instances (function values), it is often done in such a way that no infinite loop or infinite chain of references can occur.

 

Python allows functions to call itself. 


A Recursive Solution comprises of:
  • Base Part
                        It is the solution to the smallest version of the problem.
  • Inductive part
                        It is the Recursive part of the problem That reduces the complexity of the problem. the solution is expressed in terms of a smaller version of itself. 
 

Factorial:

            The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, indicated by n!.
                                       $$ n! = n * (n-1) * (n-2) * (n-3) * ... * 3 * 2 * 1 $$  

The above function is the Iterative approach to compute the factorial of a positive integer n.
Since we are using for loop and the loop iterates from 1 to n+1 to calculate the factorial of n.

 $$factorial(n) =\begin{cases}1&\quad\text{if } n\text{ == 0 or 1}\\n * factorial(n-1)&\quad\text{if }n\text{ > 1}\end{cases}$$


The above example shows us the recursive approach of implementing factorial of any positive integer n.

Post a Comment

0 Comments