CKTIS / Python
math.factorial
Izračunava faktorijel nenegativnog cijelog broja.
Verzija: Python 3.14; repository image, production runtime not attested
Sintaksa
math.factorial(n, /)Izračunava faktorijel nenegativnog cijelog broja.
Za n > 0 vrijedi n! = n · (n−1)!, pa 5 · 4 · 3 · 2 · 1 daje 120. Za n = 0 prazan je umnožak 1.
Negativan cijeli broj izaziva ValueError, a float izaziva TypeError.
import math
assert math.factorial(5) == 5 * math.factorial(4) == 120
assert math.factorial(0) == math.factorial(1) == 1
Primjeri
import math
print(math.factorial(5))
print(math.factorial(0))
Rezultat
120 1
Negativan cijeli broj izaziva ValueError, a float izaziva TypeError.