Reference · Math
math.comb
math.comb(int, int) -> intWhat it does
The number of ways to choose k items from n without regard to order, the binomial
coefficient, as an int, as Python's math.comb: math.comb(52, 5) is 2598960, the
poker hands. It is computed without the full factorials, so it reaches well past where
math.factorial(n) overflows.
Examples
print(math.comb(52, 5), math.comb(5, 2), math.comb(10, 3))print(math.comb(10, 3) == math.comb(10, 7))2598960 10 120trueEdge cases
kgreater thanngives0, not an error.kof0or ofngives1, andmath.comb(0, 0)is1.- The result is an int of 64 bits:
math.comb(66, 33)fits andmath.comb(67, 33)is a runtime error,integer overflow, as ismath.comb(100, 50), in the browser as on the server. Python's result grows without bound. - A float that is a whole number is accepted as an int.
print(math.comb(5, 6), math.comb(5, 0), math.comb(5, 5), math.comb(0, 0))print(math.comb(66, 33), math.comb(30, 15.0))0 1 1 17219428434016265740 155117520print(math.comb(100, 50))line 1: integer overflowErrors
A negative n or k is a runtime error, not an error value, with a message that says
so, as Python's ValueError. A fraction, text or null is a runtime error as well.
print(math.comb(-1, 2))line 1: `math.comb` is not defined for negative numbers (-1)print(math.comb(5, 2.5))line 1: `math.comb` expects an int as argument 2, got floatChaining
In a pipeline n comes first, so n | math.comb(k). A row of Pascal's triangle:
print(52 | math.comb(5))print(range(0, 6) | map((k) => math.comb(5, k)))2598960[1, 5, 10, 10, 5, 1]See also
math.perm, math.factorial, math.lgamma
Thread 0 posts
On math.comb(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo