Introduction
In this article we will be going through different ways to find the factorial of a given number in Python.
What is Python?
Python is a language for computer programming that is regularly used to create websites and software, automate tasks, and analyze data.
What is Factorial?
In mathematics, factorial refers to the product of all positive integers that are less than or equal to a particular positive integer, represented by that integer with an exclamation point.
The factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n.
Example:
The factorial of 7 is 5040 using the general formula:
n! = n x (n-1) x (n-2)...x 1
where n=7
7! = 7 x 6 x 5 x 4 x 3 x 2 x 1
So, 7! = 5040
So instead of using this normal method in case of working with many numbers i will be showing a few approaches you can use in python where you just need to enter your desired number and it will bring out the solution instead of using a calculator.
Approach to Solving Factorial
Below are the main approach to finding the factorial of a given number.
Using In-Built Functions:
In Python, the math module contains several mathematical operations that can be easily performed using the module. math.factorial() function returns the factorial of the entered number.
Syntax: math.factorial(x)
Parameter:
x: This is a numeric expression.
Returns: factorial of the desired number.
# Python Program to find the factorial of the entered #number
# by using the Python In-built function
import math
def factorial(n):
return(math.factorial(n))
#input + Code
number = int(input("Enter any number: "))
print("Factorial of the",number,"is",factorial(number))
Output:
The output or should I say the result of the factorial of each approach may vary based on the input supply, according to the code above it will ask the user for input(the desired number you wish to get the factorial for). Imagine we input 8 as our number:
Enter any number: 8
Factorial of the 8 is 40320
Iterative Approach:
Iteration is the repetition of a procedure to produce a series of results. Each iteration of the process is a single repetition of the process, and the conclusion of each iteration serves as the beginning point for the next iteration.
# Python Program to find the factorial of an entered number
# using the Iterative Approach
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
number = int(input("Enter any number: "))
print("Factorial of the",number,"is",factorial(number))
Output:
The result of the factorial will be based on the input entered, according to the code above it will ask the user for input(the desired number you wish to get the factorial for). Imagine we input 6 as our number:
Enter any number: 6
Factorial of the 6 is 720
Recursive Approach:
A recursive function is defined using self-referential statements. This signifies that the function will keep calling itself and repeating its actions until some condition is fulfilled and a result is returned.
# Python Program to find the factorial of the entered input
# using the Recursive approach
def factorial(n):
# using a single line of code to find the factorial
return 1 if (n==1 or n==0) else n * factorial(n - 1);
# Main Code
number = int(input("Enter any number: "))
print("Factorial of the",number,"is",factorial(number))
Output:
The output of the factorial will be based on the number entered, according to the code above it will ask the user for input(the desired number you wish to get the factorial for). Imagine we input 5 as our number and the answer of 5 factorial is given below:
Enter any number: 5
Factorial of the 5 is 120
Conclusion
Hopefully, after reading this article, you should understand and be able to pull this off any day for usage. Instead of pulling out your calculator for basic factorial, you can just use Python to solve your factorial problem by just importing the math library, then defining the function factorial. You could specify for input because it saves more time in case you need the same program to find the factorial of another number.
Thanks for reading.
Top comments (0)