Combination counting
Counting distinct fixed-size selections from a finite collection.
SciPy · Function
Computes the number of ways to choose k elements from N items, optionally allowing repetition. It can return a floating-point result or an exact integer for integer inputs.
comb computes the number of ways to select k elements from N items. It can use floating-point arithmetic or return an exact integer, and repetition=True switches to combinations where selections may repeat.
scipy.special.comb(N, k, *, exact=False, repetition=False)| Name | Type | Description |
|---|---|---|
N | int or ndarray | Number of things. |
k | int or ndarray | Number of elements taken. |
exact | bool, optional | Controls whether integer inputs use exact computation instead of floating-point precision. |
repetition | bool, optional | Controls whether combinations with repetition are counted. |
val — int, float, or ndarray: The total number of combinations.
from scipy.special import comb
print(comb(10, 3))
print(comb(10, 3, exact=True))
print(comb(10, 3, exact=True, repetition=True))Computes ordinary, exact-integer, and repetition-allowed combinations.
Counting distinct fixed-size selections from a finite collection.
Calculating exact integer counts where rounding is unacceptable.
Counting selections in which an item may be chosen repeatedly.
comb(N, k) returns the number of combinations of N items taken k at a time. By default it uses floating-point precision, while exact=True computes an exact result for integer inputs.
Follow-up: How does repetition change the calculation? With repetition=True, it counts selections in which an item can be chosen more than once.
N and k may be integers or ndarrays. The returned value can be an integer, float, or ndarray, matching the scalar or array-oriented computation.
Follow-up: What should be checked when using arrays? Ensure N and k have compatible shapes for the intended elementwise calculation.
Set repetition=True to count combinations with repetition. For example, comb(10, 3, exact=True, repetition=True) returns 220, whereas the ordinary exact result is 120.
Follow-up: What happens when k exceeds N without repetition? The function returns 0 rather than a combination count.
scipy.special.permscipy.special.factorialscipy.special.binom