Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Easy

Python Factorial

Author Avni Gupta
0 upvote
gp-icon
Basics of python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

One of the most fundamental concepts of mathematics is the factorial, a mathematical function used to find the product of all positive integers up to a given number. The factorial function is useful for solving problems that involve counting permutations or combinations of objects.

Python Factorial

In Python, there are several ways to calculate the factorial of a number, and in this article, we will look at some of these methods.

What is a Factorial?

Factorial is a mathematical function denoted by the symbol "!". It calculates the product of all positive integers up to a given number.

Remember: The factorial of 0 is 1.  

For example, 

The factorial of 1 is 1! = 1.

2! = 2 x 1 = 2.

3! = 3 x 2 x 1 = 6.

4! = 4 x 3 x 2 x 1 = 24

5! = 5 x 4 x 3 x 2 x 1 = 120. 

So, we can conclude that for any number n,

The factorial of n = n x (n-1) x (n-2) x ……3 x 2 x 1.

Factorials are widely used in various mathematical and scientific computations, and they have many applications in statistics, probability theory, and combinatorics.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Calculating the Factorial of a Number

Approach 1- Using Recursion

Recursion is one of the most common ways to calculate Python factorial. Recursion is a programming technique in which a function calls itself repeatedly until the specified condition is met. Here is an example of how to find out the factorial of a number using recursion in Python.

Code in Python

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

print("Factorial of 7 =", factorial(7))

 

Output

Factorial of 7 = 5040

In this code, the `factorial` function takes an integer `n` as input and returns the product of all positive integers up to `n`. The base case of the recursion is when `n` is equal to 0, in which case the function returns 1. Otherwise, the function multiplies `n` by the result of calling `factorial(n-1)`.

Approach 2- Using a Loop

Another way to calculate Python factorial is by using a loop. Here is an example of how to find out the factorial of a number using a loop in Python.

Code in Python

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

 

Output

Factorial of 6 = 720

In this code, the `factorial` function takes an integer `n` as input and returns the product of all positive integers up to `n`. The function initializes a variable `result` to 1, and then uses a `for` loop to multiply `result` by each integer from 1 to `n`.

Approach 3-Using the Python module

The Factorial of a number can also be calculated using the Python method, math.factorial(). Here is how to 

Syntax

Here is the syntax of this function after importing the math library in python:

math.factorial(x)

 

Parameters

x = the number you want to find the factorial of.

Return Value

The factorial of the number entered.

Errors and exceptions

ValueError - This factorial method only works for positive integers and gives an error if used with negative integers.

Examples

Code in Python - Factorial of a positive number

import math
print("Factorial of 5 = :" ,math.factorial(5))


Output

Factorial of 5 = : 120

Code in Python - Factorial of a Negative Number

import math
print("Factorial of -5 = :" ,math.factorial(-5))

 

Output

output of Factorial of a negative number

Now that we have seen the ways of finding Python factorial, we will look at some frequently asked questions on the same.

Also see, Convert String to List Python

Frequently Asked Questions

What is the largest factorial that can be calculated in Python?

The largest factorial that can be calculated in Python is limited by the maximum size of an integer in Python. In most implementations, this is 2^31-1 or 2^63-1, depending on whether the implementation is 32-bit or 64-bit.

What is the time complexity of calculating factorial using recursion?

The time complexity of calculating factorial using recursion is O(n), where n is the input number. This is because the recursion involves n function calls, each taking O(1) time.

What is the time complexity of calculating factorial using a loop?

The time complexity of calculating factorial using a loop is also O(n), where n is the input number. This is because the loop involves n iterations, each taking O(1) time.

Conclusion

In this article, we explored three ways to calculate Python factorial: recursion, a loop, and the function in Python. The first two methods have a time complexity of O(n) and can be used to calculate the factorial of any positive integer. 

The choice between these methods depends on personal preference and the application's specific requirements. Factorials are an essential mathematical concept widely used in various fields, and Python provides several ways to calculate them efficiently.

Try to solve the Factorial of a Number problem to practice what you learned!

Check out python Factorial of a Number for the solution.

Check out some of the amazing Guided Paths on topics such as Basics of PythonOops in pythonBasics of C, etc., along with some Contests and Interview Experiences only on Coding Ninjas Studio.

Have a look at the Interview Guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like AmazonAdobeGoogle, etc. on Coding Ninjas Studio.

Happy Learning!!

Guided path
Free
gridgp-icon
Basics of python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass