Prerequisite: Set up NumPy Package deal
To put in the NumPy bundle, kind the next command contained in the command immediate window or contained in the terminal of any code editor:
As soon as the Numpy Package deal has been put in, the consumer can begin utilizing it by merely importing the bundle inside this system
The way to Use “factorial()” Technique From NumPy?
As talked about above, this perform is used to get the factorial of a constructive integer worth and returns the worth to the calling variable or perform. The syntax of this perform is outlined beneath:
resultVar = numpy.math.factorial(intValue)
To grasp the output, check out the below-given examples.
Instance 1: Discovering Factorial of Constructive Integer By means of NumPy
Begin by importing the numpy module into the code after which move an integer worth into the factorial perform utilizing the next strains of code:
import numpy
resultVar = numpy.math.factorial(5)
On the finish, merely print out the worth of the “resultVar” utilizing the print():
Upon executing this code, the terminal will show the next output:
The output verified that the factorial of “5” is “120”.
Attempt discovering the factorial of “0” through the use of the next strains of code:
import numpy
resultVar = numpy.math.factorial(0)
print(resultVar)
This code will present the next output on the terminal:
The output reveals that the factorial of the worth “0” is “1,” and that explains using the factorial() methodology from the NumPy library.
Instance 2: Discovering the Factorial of Detrimental Integer Values By means of NumPy
Factorial of unfavorable integer values does exist, however many of the capabilities don’t assist this. The identical is the case for the factorial() methodology from the NumPy library. If the consumer tries passing a unfavorable integer worth, then the perform returns an error within the output.
To show this, take the next code:
import numpy
resultVar = numpy.math.factorial(–12)
print(resultVar)
Upon executing, this code produces the next error on the terminal:
The output proves that the factorial() methodology can’t be used to seek out the factorial of unfavorable integer values.
Conclusion
Discovering the factorial by using pre-built strategies is moderately a simple job. The NumPy gives a factorial() methodology that can be utilized to seek out the factorial of any “constructive integer” worth. Nevertheless, if the consumer tries to move in a unfavorable worth or a floating level worth, then this perform runs into an error and causes this system to crash.