CKTIS / Python
math.factorial
Vypočíta faktoriál nezáporného celého čísla.
Verzia: Python 3.14; repository image, production runtime not attested
Syntax
math.factorial(n, /)Vypočíta faktoriál nezáporného celého čísla.
Pre n > 0 platí n! = n · (n−1)!, takže 5 · 4 · 3 · 2 · 1 dáva 120. Pre n = 0 má prázdny súč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
Príklady
import math
print(math.factorial(5))
print(math.factorial(0))
Výsledok
120 1
Záporné celé číslo vyvolá ValueError a float vyvolá TypeError.