En Python, el módulo de matemáticas contiene una serie de operaciones matemáticas, que se pueden realizar con facilidad usando el módulo. math.factorial()
La función devuelve el factorial del número deseado.
Syntax: math.factorial(x) Parameter: x: This is a numeric expression. Returns: factorial of desired number.
Código #1:
# Python code to demonstrate the working of factorial() # importing "math" for mathematical operations import math x = 5 # returning the factorial print ("The factorial of 5 is : ", end ="") print (math.factorial(x))
Producción:
The factorial of 5 is : 120
Código #2:
# Python code to demonstrate the working of factorial() # importing "math" for mathematical operations import math x = 5 y = 15 z = 8 # returning the factorial print ("The factorial of 5 is : ", math.factorial(x)) print ("The factorial of 15 is : ", math.factorial(y)) print ("The factorial of 8 is : ", math.factorial(z))
Producción:
The factorial of 5 is : 120 The factorial of 15 is : 1307674368000 The factorial of 8 is : 40320
Código #3: arroja ValueError si x no es un número entero
# Python code to demonstrate the working of factorial() # importing "math" for mathematical operations import math # when x is not integer print ("math.factorial(13.7) : ", math.factorial(13.7))
Producción:
ValueError: factorial() only accepts integral values