CKTIS / Python
math.factorial
Izračuna fakulteto nenegativnega celega števila.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
math.factorial(n, /)Izračuna fakulteto nenegativnega celega števila.
Za n > 0 velja n! = n · (n−1)!, zato 5 · 4 · 3 · 2 · 1 da 120. Pri n = 0 ima prazen produkt vrednost 1.
Negativno celo število sproži ValueError, float pa TypeError.
import math
assert math.factorial(5) == 5 * math.factorial(4) == 120
assert math.factorial(0) == math.factorial(1) == 1
Primeri
import math
print(math.factorial(5))
print(math.factorial(0))
Rezultat
120 1
Negativno celo število sproži ValueError, float pa TypeError.