Hello coders!! This article will learn different methods to find a number in python. There is more than one way, like the numpy factorial or the scipy factorial. Let us learn about all these methods in detail.
What is factorial of a number?
The product of all the integers from 1 to a number is known as the number factor. The factorial of a number n is denoted n!. For example, the 5! = 1*2*3*4*5 = 120.
Let us now learn the different ways to calculate the factorial of a given number in Python.
Method 1: Using for loop:
n = 5
fact = 1
if n < 0:
print("Factorial does not exist for negative numbers")
elif n == 0:
print("The factorial of 0 = 1")
else:
for i in range(1,n + 1):
fact = fact*i
print("The factorial of",n,"=",fact)
Output:
The factorial of 5 = 120
In the above example, we first declared a number, and we also declare a variable fact to calculate the factorial of the number. We then checked the value of the number. If the number is less than 0, i.e., negative, its factorial can’t be calculated. The factorial of 0 is 1. And lastly, if the number is positive, we iterate from 1 to that number and keep multiplying the digits to calculate the number factorial.
Method 2: Using Recursion:
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
n = 5
if n< 0:
print("Factorial does not exist for negative numbers")
elif n == 0:
print("The factorial of 0 = 1")
else:
print("The factorial of", n, "=", factorial(n))
Here, we have used recursion to calculate the given number’s factorial. Like the previous example, we have checked the number for negative, zero, and positive. If the number is positive, we call the factorial() function. Inside the function, we keep calling the factorial function recursively until the number becomes 1. We multiply the return value of the factorial() method while we call it recursively, every time decreasing the value of the number by 1.
Method 3: Calculate the Factorial Using NumPy:
The NumPy module of Python contains an in-built function numpy.math.factorial(n) to calculate the factorial of the given number n.
Syntax:
np.math.factorial(n))
Parameters:
- n: number whose factorial is required
Return Value:
- Factorial of the given number
import numpy as np
n = 5
print("The factorial of", n, "=", np.math.factorial(n))
Output:
The factorial of 5 = 120
In this example, we have used the in-built factorial() method of the NumPy module to calculate the factorial of the given number.
Method 4: Calculate the Factorial Using Scipy:
Scipy library of Python is a collection of libraries and modules used primarily for scientific computing. An inbuilt method of the scipy library, scipy.math.factorial(), can be used to calculate a number factorial.
Syntax:
scipy.misc.factorial(n)
Parameter:
- n: number or array of integers
- exact: It is a bool parameter. If this parameter is set as True, it calculates the answer exactly using long integer arithmetic. However, if it is set as False, the result is approximated in floating-point rapidly using the gamma function.
Return Value:
Factorial of the given number.
from scipy.special import factorial
n=5
print(factorial(n, exact=True))
Output:
120L
In this example, we used the inbuilt factorial() method of the scipy module to calculate the given number factorial. As the ‘exact’ parameter is set as True, the result is approximated to floating-point.
Method 5: Using Math library:
Syntax:
math.factorial(n))
Parameters:
- n: number whose factorial is required
Return Value:
- Factorial of the given number
import math
n = 5
print("The factorial of", n, "=", math.factorial(n))
Output:
The factorial of 5 = 120
In this example, we have used the in-built factorial() method of the Math module to calculate the given number factorial.
Conclusion: Numpy Factorial
With this, we come to an end with this article. I hope you learned various ways to calculate the factorial of a given number in Python.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!