Skip to main content

Mojo function

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.

Was this page helpful?