Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

math.comb

Conta escolhas de k posições entre n sem reposição nem ordem.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

math.comb(n, k, /)

Conta escolhas de k posições entre n sem reposição nem ordem.

Cinco vezes quatro dá 20 pares ordenados e a divisão por dois deixa 10 escolhas. Se k > n o resultado é 0.

Um inteiro negativo gera ValueError e um float gera TypeError.

import math
assert math.comb(5, 2) == 5 * 4 // 2
assert math.comb(5, 0) == 1 and math.comb(2, 5) == 0

Exemplos

import math
print(math.comb(5, 2))
print(math.comb(2, 5))

Resultado

10
0

Um inteiro negativo gera ValueError e um float gera TypeError.

Fontes