El módulo matemático en Python contiene una serie de operaciones matemáticas, que se pueden realizar con facilidad usando el módulo. math.comb()
El método en Python se usa para obtener la cantidad de formas de elegir k elementos de n elementos sin repetición y sin orden. Básicamente se evalúa como n! / (k! * (n – k)!) cuando k n. También se le conoce como coeficiente binomial porque es equivalente al coeficiente del k-ésimo término en la expansión polinomial de la expresión (1 + x) n .
Este método es nuevo en la versión 3.8 de Python.
Sintaxis: matemáticas.comb(n, k)
Parámetros:
n : Un entero no negativo
k : Un entero no negativoDevuelve: un valor entero que representa el número de formas de elegir k elementos de n elementos sin repetición y sin orden.
Código #1: Uso del math.comb()
método
# Python Program to explain math.comb() method # Importing math module import math n = 10 k = 2 # Get the number of ways to choose # k items from n items without # repetition and without order nCk = math.comb(n, k) print(nCk) n = 5 k = 3 # Get the number of ways to choose # k items from n items without # repetition and without order nCk = math.comb(n, k) print(nCk)
45 10
Código #2: Cuando k > n
# Python Program to explain math.comb() method # Importing math module import math # When k > n # math.comb(n, k) returns 0. n = 3 k = 5 # Get the number of ways to choose # k items from n items without # repetition and without order nCk = math.comb(n, k) print(nCk)
0
Código #3: Uso del math.comb()
método para encontrar el coeficiente del k-ésimo término en la expansión binomial de la expresión (1 + x) n
# Python Program to explain math.comb() method # Importing math module import math n = 5 k = 2 # Find the coefficient of k-th # term in the expansion of # expression (1 + x)^n nCk = math.comb(n, k) print(nCk) n = 8 k = 3 # Find the coefficient of k-th # term in the expansion of # expression (1 + x)^n nCk = math.comb(n, k) print(nCk)
10 56
Referencia: biblioteca matemática de Python