# composition() Function & Examples

Use the composition() combinatorics function in Calcul.io. Review its syntax, edit working examples, understand the result, and explore related math functions.

## composition

#### composition(n, k)

Try it yourself:

```calculio
composition(5, 3)
```

[combinations](https://calcul.io/function/combinations/index.md)

---

## Count selections with the composition function

**composition(n, k)** counts the ways to write a non-negative integer `n` as a sum of `k` non-negative parts. Order matters: splitting 5 as 2 + 3 differs from 3 + 2. This is the familiar “stars and bars” counting problem and is valuable for distributions, allocations, and coefficient calculations.

## What the result represents

For example, `composition(5, 3)` counts distributions of five identical items among three labelled boxes, allowing empty boxes. A result can be checked with the binomial expression `combinations(n + k - 1, k - 1)`. The related [combinations](https://calcul.io/function/combinations/index.md) function counts choices where order does not matter, while [permutations](https://calcul.io/function/permutations/index.md) counts ordered arrangements of distinct objects.

## Examples and modelling choices

If three teams receive a total of ten identical tokens, use `composition(10, 3)`. If every team must receive at least one token, allocate one token to each team first, then calculate `composition(7, 3)`. Be explicit about whether parts may be zero; that assumption changes the problem. For a multinomial allocation of distinguishable items, use [multinomial](https://calcul.io/function/multinomial/index.md) instead.

## Domain caveats

`n` and `k` should be non-negative integers, and `k` should describe a meaningful number of parts. Counts grow rapidly, so exact integer output can become very large. Use [factorial](https://calcul.io/function/factorial/index.md) only for the corresponding formula when you understand its larger intermediate values. For repeated distribution calculations, [combinationsWithRep](https://calcul.io/function/combinationsWithRep/index.md) is also worth comparing.

[All functions](https://calcul.io/functions/index.md)
