CKTIS / Python
math.factorial
Berechnet die Fakultät einer nichtnegativen ganzen Zahl.
Version: Python 3.14; repository image, production runtime not attested
Syntax
math.factorial(n, /)Berechnet die Fakultät einer nichtnegativen ganzen Zahl.
Für n > 0 gilt n! = n · (n−1)!, daher ergibt 5 · 4 · 3 · 2 · 1 den Wert 120. Bei n = 0 hat das leere Produkt den Wert 1.
Negative Argumente lösen ValueError und float-Werte TypeError aus.
import math
assert math.factorial(5) == 5 * math.factorial(4) == 120
assert math.factorial(0) == math.factorial(1) == 1
Beispiele
import math
print(math.factorial(5))
print(math.factorial(0))
Ergebnis
120 1
Negative Argumente lösen ValueError und float-Werte TypeError aus.