Saturday, October 25, 2025
HomeLanguagesHow to find the factorial os a number using SciPy in Python?

How to find the factorial os a number using SciPy in Python?

 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




# importing module
from scipy.special import factorial
  
  
print(factorial(3))
  
# False always return float value
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




# importing module
from scipy.special import factorial
  
  
# True always return int value
print(factorial(4, exact=True))


Output :

24

Example 3: If we compute the negative value for factorial then it always returns zero.

Python3




# importing module
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




# importing module
import numpy as np
from scipy.special import factorial
  
# creating list
list = [6, 3, 4, 5]
  
# creating NumPy Array
arr = np.array(list)
  
# computing factorial and display
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]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS