Open In App
Related Articles

Python Program to Find the Factorial of a Number

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. 

For example factorial of 6 is 6*5*4*3*2*1 which is 720.

Find the Factorial of a Number Using Recursive approach

This Python program uses a recursive function to calculate the factorial of a given number. The factorial is computed by multiplying the number with the factorial of its preceding number.

python3

# Python 3 program to find
# factorial of given number
def factorial(n):
     
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
# Driver Code
num = 5
print("Factorial of",num,"is",factorial(num))

                    

Output:

Factorial of 5 is 120

Time Complexity: O(n)
Auxiliary Space: O(n)

Find the Factorial of a Number Using Iterative approach

Example 1:

python3

# Python 3 program to find 
# factorial of given number
def factorial(n):
    if n < 0:
        return 0
    elif n == 0 or n == 1:
        return 1
    else:
        fact = 1
        while(n > 1):
            fact *= n
            n -= 1
        return fact
 
# Driver Code
num = 5
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Dharmik Thakkar

                    

Output:

Factorial of 5 is 120

Time Complexity: O(n)
Auxiliary Space: O(1)

Example 2: 

Python3

# Python 3 program to find
# factorial of given number
  
# Function to find factorial of given number
def factorial(n):
       
    res = 1
      
    for i in range(2, n+1):
        res *= i
    return res
 # Driver Code
num = 5
print("Factorial of", num, "is",
factorial(num))

                    

Output
Factorial of 5 is 120

Time Complexity: O(n)
Auxiliary Space: O(1)

Find the Factorial of a Number Using One line Solution (Using Ternary operator): 

Python3

# Python 3 program to find
# factorial of given number
 
def factorial(n):
 
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
 
# Driver Code
num = 5
print ("Factorial of",num,"is",
      factorial(num))
 
# This code is contributed
# by Smitha Dinesh Semwal.

                    

Output:

Factorial of 5 is 120

Time Complexity: O(n)
Auxiliary Space: O(n)

Find the Factorial of a Number Using using In-built function 

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.factorial() function returns the factorial of desired number.

Syntax: math.factorial(x)

Parameter:
x: This is a numeric expression.

Returns:  factorial of desired number.

Python3

# Python 3 program to find
# factorial of given number
import math
 
def factorial(n):
    return(math.factorial(n))
 
 
# Driver Code
num = 5
print("Factorial of", num, "is",
      factorial(num))
 
# This code is contributed by Ashutosh Pandit

                    

Output:

Factorial of 5 is 120

Time complexity: O(1)

Auxiliary space: O(1)

Find the Factorial of a Number Using numpy.prod 

Python3

import numpy
n=5
x=numpy.prod([i for i in range(1,n+1)])
print(x)

                    

Output 

120

Time Complexity: O(n)
Auxiliary Space: O(1)

Prime Factorization Method to find Factorial

  1. Initialize the factorial variable to 1.
  2. For each number i from 2 to n, do the following:
    a. Find the prime factorization of i.
    b. For each prime factor p and its corresponding power k in the factorization of i, multiply the factorial variable by p raised to the power of k.
  3. Return the factorial variable.

Python3

# Python 3 program to find factorial
# of a given number using prime
# factorization method.
 
# Function to find prime factors of a number
def primeFactors(n):
    factors = {}
    i = 2
    while i*i <= n:
        while n % i == 0:
            if i not in factors:
                factors[i] = 0
            factors[i] += 1
            n //= i
        i += 1
    if n > 1:
        if n not in factors:
            factors[n] = 0
        factors[n] += 1
    return factors
 
# Function to find factorial of a number
def factorial(n):
    result = 1
    for i in range(2, n+1):
        factors = primeFactors(i)
        for p in factors:
            result *= p ** factors[p]
    return result
 
# Driver Code
num = 5
print("Factorial of", num, "is", factorial(num))

                    

Output
Factorial of 5 is 120

Time Complexity: O(sqrt(n))

Auxiliary Space: O(sqrt(n))

Please refer complete article on Program for factorial of a number for more details!



Last Updated : 22 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads