Factorial

Osama Mohamed
3 min readOct 16, 2023

--

Factorial

Understanding Factorial Calculation Using Python Recursion

Factorial is a fundamental concept in mathematics, has a wide range of applications, from permutations and combinations to algorithmic analysis. Python provides an excellent platform for understanding and implementing the factorial function, particularly through recursion which is a powerful programming paradigm. In this article, I will show you how to calculate the factorial of a number using recursion in Python.

What is Factorial?

In mathematics, the factorial of a non-negative integer n is the product of all positive integers less than or equal to n. It is denoted by n!

n! = n × (n-1) × (n-2) × ⋯ × 2 × 1

OR

n! = n * (n -1)!

For example:

  • 5! = 5 × 4 × 3 × 2 × 1 = 120
  • 4! = 4 × 3 × 2 × 1 = 24
  • 3! = 3 × 2 × 1 = 6

The factorial function has a base case: 0! = 1.

Understanding Recursion

Recursion occurs when a function calls itself within its body. Recursive functions must have a base case to stop the recursion. otherwise, the function will continue calling itself indefinitely.

Python Recursive Function for Factorial

Here’s how you can implement the factorial function using Python recursion:

def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print(factorial(5))

When you call factorial(n), it keeps calling itself with smaller values until it reaches n = 0, at this point it returns 1.

All these returned values are then multiplied together to give the factorial of n.

For example, calling factorial(5) would perform the following calculations behind the scenes:

5 x factorial(4)
5 x (4 x factorial(3))
5 x (4 x (3 x factorial(2)))
5 x (4 x (3 x (2 x factorial(1))))
5 x (4 x (3 x (2 x (1 x factorial(0)))))
5 x (4 x (3 x (2 x (1 x 1))))
5 x (4 x (3 x (2 x 1)))
5 x (4 x (3 x 2))
5 x (4 x 6)
5 x 24
120

So, factorial(5) = 120.

Other way to understand the working mechanism for factorial(5):

factorial(5) = 5 × factorial(4)
factorial(4) = 4 × factorial(3)
factorial(3) = 3 × factorial(2)
factorial(2) = 2 × factorial(1)
factorial(1) = 1 × factorial(0)
factorial(0) = 1 (Base case)

The function effectively uses recursion to perform each step, returning the final value of 120 when n = 5.

def factorial(n):

This line defines a function called factorial(), which takes a single argument n, which represents the number that you want to find the factorial of it.

if n == 0:

This line checks if n is equal to 0.

return 1

If n is equal to 0, the function returns 1, because the factorial of 0 is defined to be 1.

return n * factorial(n-1)

Otherwise, the function returns n multiplied by the factorial of n - 1. This is the recursive step, where the function calls itself with a smaller value.

print(factorial(5))

This line calls the factorial function with an argument of 5 and prints the returned value, which is 120.

Here is an example of the output of the code:

120

Limitations and Considerations

While the recursive approach is elegant and straightforward, it might lead to a stack overflow error for large inputs due to excessive function calls. Therefore, it’s crucial to be aware of this limitation when working with recursion.

Conclusion

Understanding the factorial function and its implementation using Python recursion is a great way to grasp both a mathematical concept and a programming paradigm. While recursion offers a clean and simple approach, it’s essential to be mindful of its limitations for large-scale applications.

Here is the source code on GitHub :

Factorial

--

--

No responses yet