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.

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]

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 :