How to Calculate the Factorial of a Number in Python?

Factorials are a fundamental concept in mathematics, often used in permutations and combinations, calculus, and other mathematical analyses. The factorial of a number ‘n’ is the product of all positive integers less than or equal to ‘n’. It’s represented as ‘n!’.

In this blog post, we will explore how to write a Python program to calculate the factorial of a number.

Calculate the Factorial of a Number in Python

We will see here below 3 different approaches how to calculate the factorial of a number in Python.

Iterative Approach

Here’s how you could do it using a simple iterative approach: Below is the simple program to calculate the factorial of a number in Python.

def factorial_iterative(n):
    # Base case: factorial of 0 is 1
    if n == 0:
        return 1
    else:
        factorial = 1
        for i in range(1, n + 1):
            factorial *= i
        return factorial

In this code:

  • We first check if the input number is zero. If it is, we return 1 since the factorial of zero is defined as one.
  • If the number is not zero, we initialize a variable factorial to 1.
  • We then start a for loop from 1 up to and including the input number. For each number in this range, we multiply the current value of factorial by this number.
  • Finally, we return the resulting factorial.

For example, the factorial of 5 using this function would be calculated as:

print(factorial_iterative(5))  # Output: 120

Check out the screenshot below:

Calculate the Factorial of a Number in Python

This function works fine, but there’s another way to calculate factorials that’s often more intuitive for people who are familiar with recursion: the recursive approach.

Recursive Approach

Here’s the equivalent function using recursion in Python to calculate the factorial of a number in Python:

def factorial_recursive(n):
    # Base case: factorial of 0 is 1
    if n == 0:
        return 1
    else:
        return n * factorial_recursive(n - 1)

In this code:

  • We again start by checking if the input number is zero, and if it is, we return 1.
  • If the number is not zero, we multiply the number by the factorial of one less than the number. This is done by recursively calling the factorial_recursive function with the argument n - 1.

For example, the factorial of 5 using this function would be calculated as:

print(factorial_recursive(5))  # Output: 120

Tail Recursive Approach

A more efficient recursive method would be tail recursion, where the recursive call is the final operation in the function:

Here is the code to calculate the factorial of a number in Python using rail recursive approach.

def factorial_tail_recursive(n, accumulator=1):
    # Base case: factorial of 0 is 1
    if n == 0:
        return accumulator
    else:
        return factorial_tail_recursive(n - 1, n * accumulator)

In this code:

  • The function takes two arguments: the number for which we want to calculate the factorial, and an ‘accumulator’ that keeps track of the product so far.
  • If the number is zero, the function returns the accumulator, which by now has been multiplied by every number from the original input down to 1.
  • If the number is not zero, the function makes a recursive call, passing in one less than the current number, and the product of the current number and the current accumulator.

For example, the factorial of 5 using this function would be calculated as:

print(factorial_tail_recursive(5))  # Output: 120

Conclusion

In this Python tutorial, we have learned how to calculate the factorial of a number in Python using both iterative and recursive methods.

You may also like: