Factorial function is a mathematical formula and used in many areas of mathematics. It is the product of all positive numbers less than or equal to n. Factorial number is represented by an exclamation mark ‘!’.
factorial of numbers
In Python, factorial number can be found using the different functions as below.
User-defined factorial function.
SciPy factorial function.
Math factorial function.
Numpy factorial function.
All above are the same function of <function math.factorial> class except first. Check the below screen shot along with it’s output to verify the math class function.
User defined factorial function.
Below program demonstrate how to calculate the factorial of any given number using User defined factorial function .
In above program we takes input from user and store that value to variable (user_input) and pass this value to user defined function(factorial()) to calculate the factorial of given number.
This function checks if the number is 0 it return 1 (0!=1)and if we provides any non positive number to this function then it return output as Factorial does not exist for negative number .Recursive function is used in above example to compute the factorial of given number
SciPy factorial function
SciPy is an open-source library for mathematics, science, and engineering. The SciPy library depends on NumPy. Scipy also provides a factorial function to calculate factorial of any number. Scipy provides two different functions to calculate the factorial of the numbers.
scipy.math.factorial() -This function take single parameter (n) and return integer value as output.if n is negatives number then it raise ValueError.
scipy.special.factorial()-This function takes 2 parameters. n-int or array_like of int. exact-If True return integer value and if False it return floating values.By default it return false.
Math factorial function
Math module in python contains a number of various mathematical operations like sin,sqrt,trunc and log etc. We can also use math module to find factorial of any number using math.factorial() function.
Numpy factorial function
In numpy ,factorial function take one integer arument to calculate factorial of desired number.if we provide negative argument to this function then python will raise a value error as shown in below example.
This is all about factorial number and different functions used to generate the factorial of the desired number. It is not too much complicated to generate factorial of any values using the above mention functions. If you want to see more python function with an interesting example then click here.