CKTIS / Python
math.factorial
Vypočítá faktoriál nezáporného celého čísla.
Verze: Python 3.14; repository image, production runtime not attested
Syntaxe
math.factorial(n, /)Vypočítá faktoriál nezáporného celého čísla.
Pro n > 0 platí n! = n · (n−1)!, takže 5 · 4 · 3 · 2 · 1 dává 120. Pro n = 0 má prázdný součin hodnotu 1.
Záporné celé číslo vyvolá ValueError a float vyvolá TypeError.
import math
assert math.factorial(5) == 5 * math.factorial(4) == 120
assert math.factorial(0) == math.factorial(1) == 1
Příklady
import math
print(math.factorial(5))
print(math.factorial(0))
Výsledek
120 1
Záporné celé číslo vyvolá ValueError a float vyvolá TypeError.