You can import them like this:

In [7]: import scipy, numpy, math

In [8]: scipy.math.factorial, numpy.math.factorial, math.factorial
Out[8]: 
(<function math.factorial>,
 <function math.factorial>,
 <function math.factorial>)

scipy.math.factorial and numpy.math.factorial seem to simply be aliases/references for/to math.factorial, that is scipy.math.factorial is math.factorial and numpy.math.factorial is math.factorial should both give True.

Answer from Ashwini Chaudhary on Stack Overflow
Top answer
1 of 6
106

You can import them like this:

In [7]: import scipy, numpy, math

In [8]: scipy.math.factorial, numpy.math.factorial, math.factorial
Out[8]: 
(<function math.factorial>,
 <function math.factorial>,
 <function math.factorial>)

scipy.math.factorial and numpy.math.factorial seem to simply be aliases/references for/to math.factorial, that is scipy.math.factorial is math.factorial and numpy.math.factorial is math.factorial should both give True.

2 of 6
64

The answer for Ashwini is great, in pointing out that scipy.math.factorial, numpy.math.factorial, math.factorial are the same functions. However, I'd recommend use the one that Janne mentioned, that scipy.special.factorial is different. The one from scipy can take np.ndarray as an input, while the others can't.

In [12]: import scipy.special

In [13]: temp = np.arange(10) # temp is an np.ndarray

In [14]: math.factorial(temp) # This won't work
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-039ec0734458> in <module>()
----> 1 math.factorial(temp)

TypeError: only length-1 arrays can be converted to Python scalars

In [15]: scipy.special.factorial(temp) # This works!
Out[15]: 
array([  1.00000000e+00,   1.00000000e+00,   2.00000000e+00,
         6.00000000e+00,   2.40000000e+01,   1.20000000e+02,
         7.20000000e+02,   5.04000000e+03,   4.03200000e+04,
         3.62880000e+05])

So, if you are doing factorial to a np.ndarray, the one from scipy will be easier to code and faster than doing the for-loops.

🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.special.factorial.html
factorial — SciPy v1.17.0 Manual
With exact=False the factorial is approximated using the gamma function (which is also the definition of the complex extension): ... Try it in your browser! >>> import numpy as np >>> from scipy.special import factorial >>> arr = np.array([3, 4, 5]) >>> factorial(arr, exact=False) array([ 6., 24., 120.]) >>> factorial(arr, exact=True) array([ 6, 24, 120]) >>> factorial(5, exact=True) 120
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-for-factorial-of-a-number
Factorial of a Number - Python - GeeksforGeeks
This method computes the factorial using Python’s built-in factorial() function, which performs the entire calculation internally without requiring loops or recursion in user code. ... NumPy performs multiplication through optimized C-level operations.
Published   November 29, 2025
🌐
NumPy
numpy.org › devdocs › reference › routines.math.html
Mathematical functions — NumPy v2.5.dev0 Manual
sin(x, /[, out, where, casting, order, ...]) · Trigonometric sine, element-wise
🌐
Medium
medium.com › @whyamit101 › how-to-use-numpy-math-factorial-c9d459234e7c
How to Use numpy.math.factorial() | by why amit | Medium
February 9, 2025 - Apply and Print: With just one line — factorial_vectorized(numbers)—you calculate the factorial for every element in the array. And voilà, you’ve got your results! ... Efficiency: Processing arrays with numpy.vectorize is faster than looping through elements one at a time.
Find elsewhere
🌐
w3resource
w3resource.com › numpy › snippet › numpy-factorial-guide.php
NumPy Factorial Guide: Applications, Code Examples
January 31, 2025 - The factorial of a non-negative integer nn, denoted as n!n!, is the product of all positive integers less than or equal to nn. Example: 5!=5×4×3×2×1=1205!=5×4×3×2×1=120. ... Combinatorics: Calculating permutations and combinations.
🌐
O'Reilly
oreilly.com › library › view › numpy-beginners-guide › 9781785281969 › ch03s33.html
Time for action – calculating the factorial - Numpy Beginner's Guide (Update) [Book]
June 24, 2015 - b = np.arange(1, 9) print("b =", b) print("Factorial", b.prod())
Author   Ivan Idris
Published   2015
Pages   348
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy factorial
NumPy Factorial | Learn the Examples of NumPy Factorial Function
April 3, 2023 - Numpy.math.factorial() is a mathematical function in python that is used to compute the factorial of a given positive number. But before we start, what exactly is factorial? The factorial of a number is the product of all the positive non-zero ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python Guides
pythonguides.com › python-numpy-factorial
Python Numpy Factorial
May 5, 2025 - The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. NumPy provides efficient ways to calculate factorials, especially when dealing with arrays of numbers, which is much faster ...
🌐
NumPy
numpy.org › doc › stable › reference
NumPy reference — NumPy v2.4 Manual
December 21, 2025 - This reference manual details functions, modules, and objects included in NumPy, describing what they are and what they do. For learning how to use NumPy, see the complete documentation · Large parts of this manual originate from Travis E. Oliphant’s book Guide to NumPy (which generously ...
🌐
SciPy
docs.scipy.org › doc › scipy-1.12.0 › reference › generated › scipy.special.factorial.html
scipy.special.factorial — SciPy v1.12.0 Manual
With exact=False the factorial is approximated using the gamma function: \[n! = \Gamma(n+1)\] Examples · >>> import numpy as np >>> from scipy.special import factorial >>> arr = np.array([3, 4, 5]) >>> factorial(arr, exact=False) array([ 6., 24., 120.]) >>> factorial(arr, exact=True) array([ 6, 24, 120]) >>> factorial(5, exact=True) 120
🌐
Python.org
discuss.python.org › python help
Factorial optimization - Python Help - Discussions on Python.org
July 9, 2024 - I have a function that takes two numbers (the first is greater than the second) and returns the result of dividing their factorials. import math def factorial_division(n, d): return math.factorial(n) / math.factorial(d) However, with large quantities, it works for a very long time because of ...
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
2 weeks ago - Return factorial of the nonnegative integer n.
🌐
IncludeHelp
includehelp.com › python › factorial-in-numpy-and-scipy.aspx
Python - Factorial in numpy and scipy
January 20, 2023 - However, scipy.special.factorial is different as it can take a ndarray as an input while others can't. ... # Import numpy import numpy as np # Import scipy special import scipy.special # Creating a numpy array arr = np.array([3, 4, 5]) # Display original array print("Original array:\n",arr,"\n") res = scipy.special.factorial(arr) # Display the result print("Result:\n",res)
🌐
W3Schools
w3schools.com › python › ref_math_factorial.asp
Python math.factorial() Method
The math.factorial() method returns the factorial of a number.
🌐
GeeksforGeeks
geeksforgeeks.org › python › factorial-in-python
factorial() in Python - GeeksforGeeks
NumPy · Pandas · Practice · Django · Flask · Last Updated : 18 Dec, 2025 · The factorial of a number n (written as n!) is the product of all positive integers from 1 to n. For example: 5! = 1 * 2 * 3 * 4 * 5 = 120.
Published   December 18, 2025
🌐
NumPy
numpy.org › devdocs › reference
NumPy reference — NumPy v2.5.dev0 Manual
This reference manual details functions, modules, and objects included in NumPy, describing what they are and what they do. For learning how to use NumPy, see the complete documentation · Large parts of this manual originate from Travis E. Oliphant’s book Guide to NumPy (which generously ...