IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b1
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

comb

comb(n: Int, k: Int) -> Int

Computes the number of ways to choose k items from n items without repetition and without order (binomial coefficient).

Equivalent to Python's math.comb(n, k).

Examples:

from std.math import comb
print(comb(5, 2)) # 10
print(comb(10, 0)) # 1
print(comb(3, 5)) # 0

Args:

  • n (Int): The total number of items. Must be non-negative.
  • k (Int): The number of items to choose. Must be non-negative.

Returns:

Int: The binomial coefficient C(n, k). Returns 0 if k > n. Asserts if either argument is negative.