CKTIS / Python
math.comb
Cuenta selecciones de k posiciones entre n sin reposición ni orden.
Versión: Python 3.14; repository image, production runtime not attested
Sintaxis
math.comb(n, k, /)Cuenta selecciones de k posiciones entre n sin reposición ni orden.
Cinco por cuatro da 20 pares ordenados y al dividir por dos quedan 10 selecciones. Si k > n el resultado es 0.
Un entero negativo provoca ValueError y un float provoca TypeError.
import math
assert math.comb(5, 2) == 5 * 4 // 2
assert math.comb(5, 0) == 1 and math.comb(2, 5) == 0
Ejemplos
import math
print(math.comb(5, 2))
print(math.comb(2, 5))
Resultado
10 0
Un entero negativo provoca ValueError y un float provoca TypeError.