Reference · Math

math.comb

signature
math.comb(int, int) -> int

What 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))
output
2598960 10 120true

Edge cases

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))
output
0 1 1 17219428434016265740 155117520
this one fails
print(math.comb(100, 50))
output
line 1: integer overflow

Errors

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.

this one fails
print(math.comb(-1, 2))
output
line 1: `math.comb` is not defined for negative numbers (-1)
this one fails
print(math.comb(5, 2.5))
output
line 1: `math.comb` expects an int as argument 2, got float

Chaining

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)))
output
2598960[1, 5, 10, 10, 5, 1]

See also

math.perm, math.factorial, math.lgamma

Thread
8Useful? Like it, or add to its thread below.

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