Factorial Program in Python

Ayushi Trivedi Last Updated : 06 Aug, 2024
3 min read

Introduction

Suppose for instance that you are cooking a meal that will have a certain taste that you desire if only the sequence of processes is followed as expected. Likewise, in mathematics and programming, getting factorial definition of a number requires a unique sequence of multiplication of a series of decrement positive integers. Factorials’ operation is known and used as a base characteristic in several branches including combinatorics, algebra, and computer sciences.
Wth the help of this article, the reader will learn how to solve a factorial in Python, reveal the meaning of such a program, and understand what approaches can be used to achieve this goal.

Factorial Program in Python

Learning Outcomes

  • Learn about what a factorial is and it’s importance in mathematics.
  • Learn how to write a factorial program in Python using iterative and recursive methodologies to perform operations.
  • For specific questions about the calculations of factorials in Python you have come to the right place.

What is a Factorial?

A 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! ).

For example:

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

Special Case:

  • 0!=1 (by definition)

Why Factorial is useful?

Factorials are widely used in:

  • Permutations and Combinations: Figuring out how many ways there are to choose or organize things.
  • Probability Theory: Recognizing the probability of happenings.
  • Algebra and Calculus: Completing series expansions and equations.
  • Computer Algorithms: Implementing various mathematical algorithms.

Writing a Factorial Program in Python

There are several ways to calculate the factorial of a number in Python. We’ll cover the most common methods: iterative and recursive.

Iterative Method

In the iterative method, we use a loop to multiply the numbers in descending order.

def factorial_iterative(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

# Example usage
number = 5
print(f"The factorial of {number} is {factorial_iterative(number)}")

Output:

The factorial of 5 is 120

Recursive Method

In the recursive technique, a function solves smaller cases of the same problem by invoking itself until it reaches the base case.

def factorial_recursive(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial_recursive(n - 1)

# Example usage
number = 5
print(f"The factorial of {number} is {factorial_recursive(number)}")

Output:

The factorial of 5 is 120

Using Python’s Built-in Function

Python provides a built-in function in the math module to calculate the factorial.

import math

number = 5
print(f"The factorial of {number} is {math.factorial(number)}")

Output:

The factorial of 5 is 120

Efficiency and Complexity

  • Iterative Method: Since, the iterative approach involves three operations and has a time complexity of O_(n) traversing through large input values and a space complexity of O of 1 without requiring additional space.
  • Recursive Method: This recursive function also has a time complexity of O(n) but it also served a space complexity of O(n) because of the call stack, which can be problematic when handling very large inputs.
  • Built-in Method: Prefer using the built-in function for its efficiency, simplicity, and performance.

Conclusion

Computing the factorial of the given number is a simple function in mathematics and computer science. Python offers many approaches, ranging from cycles to recursion and functions with a built-in map. This knowledge identifies the advantages and disadvantages of each method to ensure the right method is used in the right context. Whether you are working on a combinatorial problem or finding a solution for a certain algorithm, being able to calculate factorials is always helpful.

Frequently Asked Questions

Q1: What is a factorial?

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

Q2. How do you calculate the factorial of a number in Python?

A. You can calculate the factorial using iterative loops, recursion, or Python’s built-in math.factorial function.

Q3. Which method is the most efficient for calculating factorials in Python?

A. Using the built-in math.factorial function is generally the most efficient and simplest method.

Q4. Can the recursive method handle very large inputs?

A.Python’s recursion depth limit and call stack size can limit the recursive method, making it less suitable for very large inputs compared to the iterative method.

Q5. What are some applications of factorials?

A. Permutations, combinations, probability theory, algebra, calculus, and a variety of computer techniques all make use of factorials.

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details