Skip to content
Related Articles

Related Articles

How to find the factorial os a number using SciPy in Python?
  • Last Updated : 29 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

 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.


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)


n : int or array_like of ints 

exact : bool, optional 


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:


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

Output :


Example 2: If exact value is True then it returns always an integer value.


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

Output :


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


# importing module
from scipy.special import factorial
print(factorial(-3, exact=False))
print(factorial(-4, exact=True))

Output :


Example 4: Get factorial for the array element.


# 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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :