combinationsWithRep

combinationsWithRep(n, k)

Try it yourself:


combinationsWithRep(): choose with repetition

combinationsWithRep(n, k) counts unordered selections of k items from n types when a type may appear more than once. Selecting three scoops from five flavours, where repeats are allowed, has combinationsWithRep(5, 3) possible outcomes.

Stars and bars

The formula is combinations(n + k − 1, k). It is often called “stars and bars”: k selected items are stars, and separators allocate them among n categories. This is useful for inventory allocations, integer solutions, and multisets.

Compare related counts

Use combinations() when repeated choices are forbidden. Use permutations() if order matters. factorial() gives its factorial expansion. See catalan() for another family of discrete counts.

Input requirements

Use non-negative integer n and k. n represents available types; k represents selections. Be explicit about whether an empty selection is meaningful in your application.

All functions