How to find the factorial os a number using SciPy in Python?
Last Updated :
29 Aug, 2020
SciPy is an open-source Python library used to solve scientific and mathematical problems. It is built on NumPy and it allows us to manipulate and visualizing with a wide range of high-level commands. Scipy also provides a scipy.special.factorial() function to calculate factorial of any number.
scipy.special.factorial()
This function takes two parameters, int or array_like of int another Boolean and return the factorial of the given parameter as integer or float depending on boolean.
Syntax: scipy.special.factorial(n, exact=False)
Parameters:
n : int or array_like of ints
exact : bool, optional
Returns:
nf : float or int or ndarray (Output depending on exact).
Note: If Exact is True return integer value and if False it returns floating values with using the gamma function and default is False.
Example 1:
Python3
from scipy.special import factorial
print (factorial( 3 ))
print (factorial( 4 , exact = False ))
|
Output :
6.0
24.0
Example 2: If exact value is True then it returns always an integer value.
Python3
from scipy.special import factorial
print (factorial( 4 , exact = True ))
|
Output :
24
Example 3: If we compute the negative value for factorial then it always returns zero.
Python3
from scipy.special import factorial
print (factorial( - 3 , exact = False ))
print (factorial( - 4 , exact = True ))
|
Output :
0.0
0
Example 4: Get factorial for the array element.
Python3
import numpy as np
from scipy.special import factorial
list = [ 6 , 3 , 4 , 5 ]
arr = np.array( list )
print ( "Factorial value in float : " ,
factorial(arr, exact = 0 ))
print ( "Factorial value in Integer : " ,
factorial(arr, exact = 1 ))
|
Output :
Factorial value in float : [720. 6. 24. 120.]
Factorial value in Integer : [720 6 24 120]
Share your thoughts in the comments
Please Login to comment...