Python

Factorial in Python NumPy

The NumPy package has a sub-module named “math,” which contains various methods that are used to perform the mathematical operation. One such method is the “factorial()” method, which is used to calculate the factorial of a positive integer value. However, to use the factorial() method, the user needs to ensure that the NumPy package has been installed in his environment.

Prerequisite: Install NumPy Package

To install the NumPy package, type the following command inside the command prompt window or inside the terminal of any code editor:

pip install numpy

Once the Numpy Package has been installed, the user can start using it by simply importing the package inside the program

How to Use “factorial()” Method From NumPy?

As mentioned above, this function is used to get the factorial of a positive integer value and returns the value to the calling variable or function. The syntax of this function is defined below:

resultVar = numpy.math.factorial(intValue)

To understand the output, take a look at the below-given examples.

Example 1: Finding Factorial of Positive Integer Through NumPy
Start by importing the numpy module into the code and then pass an integer value into the factorial function using the following lines of code:

import numpy
resultVar = numpy.math.factorial(5)

At the end, simply print out the value of the “resultVar” using the print():

print(resultVar)

Upon executing this code, the terminal will display the following output:

The output verified that the factorial of “5” is “120”.

Try finding the factorial of “0” by using the following lines of code:

import numpy
resultVar = numpy.math.factorial(0)
print(resultVar)

This code will show the following output on the terminal:

The output shows that the factorial of the value “0” is “1,” and that explains the use of the factorial() method from the NumPy library.

Example 2: Finding the Factorial of Negative Integer Values Through NumPy
Factorial of negative integer values does exist, but most of the functions do not support this. The same is the case for the factorial() method from the NumPy library. If the user tries passing a negative integer value, then the function returns an error in the output.

To demonstrate this, take the following code:

import numpy
resultVar = numpy.math.factorial(-12)
print(resultVar)

Upon executing, this code produces the following error on the terminal:

The output proves that the factorial() method cannot be used to find the factorial of negative integer values.

Conclusion

Finding the factorial through the use of pre-built methods is rather an easy task. The NumPy provides a factorial() method that can be used to find the factorial of any “positive integer” value. However, if the user tries to pass in a negative value or a floating point value, then this function runs into an error and causes the program to crash.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.