Before version 2.6 of Python, you had to calculate the factorial by writing your own function/code.
From version 2.6 and above, the factorial can be calculated by using the built-in function math.factorial(x) which is part of the math module.
In this tutorial, I will show you examples of getting the factorial both ways: Using the math module’s factorial function and writing custom functions for getting the factorial.
How to use the factorial function?
The built-in factorial function can be used as follows:
The function returns the factorial of argument x.
An example of factorial function
In this example, I used four different integer values to get the factorial by using the math.factorial function.
See the code and output below:
#Factorial function demo from math import factorial print ("The factorial of 10 = " ,factorial(10)) print ("The factorial of 20 = " ,factorial(20)) print ("The factorial of 25 = " ,factorial(25)) print ("The factorial of 100 = " ,factorial(30))
Output:
The factorial of 10 = 3628800
The factorial of 20 = 2432902008176640000
The factorial of 25 = 15511210043330985984000000
The factorial of 100 = 265252859812191058636308480000000
What is factorial? In Mathematics, the factorial is the product of the whole numbers from the given number to 1. For example, the factorial of 5 is:
5 * 4 * 3 * 2 * 1 = 120
What if a non-integer is passed to factorial function?
The example shows using a float number in the factorial function. See what it results in?
#Facotial function demo from math import factorial print ("The factorial of float number 11.5 = " ,factorial(11.5))
The result:
Writing a custom function for getting factorial
If you still prefer writing your own function to get the factorial then this section is for you.
Here, I will give three different functions for getting the factorial of a number.
The aim of each function is to multiply the numbers downwards until it reach 1 from the given number:
Function Number 1:
#Custom Factorial function demo def factorial(x): #If 0 is passed as argument then result should be 1 if x == 0: return 1 else: return x * factorial(x-1) print ("Factorial of 0 = ", factorial(0)) print ("Factorial of 1 = ", factorial(1)) print ("Factorial of 2 = ", factorial(2)) print ("Factorial of 3 = ", factorial(3)) print ("Factorial of 4 = ", factorial(4)) print ("Factorial of 5 = ", factorial(5)) print ("Factorial of 6 = ", factorial(6)) print ("Factorial of 7 = ", factorial(7))
The output:
Using for loop and range example
In the following custom function for getting the factorial, a for loop and range is used. The factorial of 0 and other numbers is as shown in the output below:
#Custom Factorial function #2 demo def factorial(n): x = 1 for i in range(n,0,-1): x = x * i return x print ("Factorial of 0 = ", factorial(0)) print ("Factorial of 1 = ", factorial(1)) print ("Factorial of 17 = ", factorial(17)) print ("Factorial of 19 = ", factorial(19)) print ("Factorial of 21 = ", factorial(21)) print ("Factorial of 23 = ", factorial(23)) print ("Factorial of 25 = ", factorial(25)) print ("Factorial of 27 = ", factorial(27))
The output:
Factorial of 0 = 1
Factorial of 1 = 1
Factorial of 17 = 355687428096000
Factorial of 19 = 121645100408832000
Factorial of 21 = 51090942171709440000
Factorial of 23 = 25852016738884976640000
Factorial of 25 = 15511210043330985984000000
Factorial of 27 = 10888869450418352160768000000
Using while loop to calculate factorial
This function uses a while loop for calculating the factorial of the given number:
#Custom Factorial function by while loop def factorial(number): x = 1 while number >= 1: x = x * number number = number - 1 return x print ("0 = ", factorial(0)) print ("1 = ", factorial(1)) print ("11 = ", factorial(11)) print ("15 = ", factorial(15)) print ("17 = ", factorial(17)) print ("19 = ", factorial(19)) print ("21 = ", factorial(21)) print ("23 = ", factorial(23))
The result:
1 = 1
11 = 39916800
15 = 1307674368000
17 = 355687428096000
19 = 121645100408832000
21 = 51090942171709440000
23 = 25852016738884976640000