Permutations, combinations and Pascal’s triangle
Combinatorics is the mathematics of counting without listing. Two questions decide almost every basic counting problem: does the order of the choices matter, and can the same item be chosen twice? The calculator below answers all four combinations of those questions exactly.
On this page
Two rules everything is built on
The product rule. If a task is done in two stages, with ways to do the first and, for each of them, ways to do the second, there are ways overall. A shirt (5 choices) and trousers (3 choices) make outfits.
The sum rule. If a task can be done in one of two ways that don’t overlap, with options for the first and for the second, there are options. If the ways do overlap, subtract the overlap: . That correction, extended to more sets, is the inclusion–exclusion principle.
The four basic counts
Choose items from distinct ones.
| Repetition | Order matters | Order does not matter |
|---|---|---|
| No repetition | ||
| Repetition allowed |
Order matters, no repetition: permutations
For the first position there are choices, then , down to . By the product rule that is . Arranging all items is the special case : orderings. Gold, silver and bronze among 8 finalists: = 336 podiums.
Order doesn’t matter, no repetition: combinations
Each unordered selection of items can be put in order in ways, so every combination is counted times among the permutations. Divide it out: . A committee of 3 from 10 people: = 120. A five-card poker hand from a 52-card deck: = 2,598,960.
Order matters, repetition allowed: sequences
Each of the positions independently takes any of values: . Eight-letter lowercase passwords: = 208,827,064,576. Binary strings of length : .
Order doesn’t matter, repetition allowed: multisets
Four scoops of ice cream from 3 flavours, where only how many of each flavour counts. The trick, called stars and bars, is to write a selection as stars (the scoops) separated by bars (the boundaries between flavours): ★★|★|★ means two of the first flavour, one of the second, one of the third. Every arrangement of stars and bars is one selection, so the count is . For the ice cream, = 15.
Pascal’s triangle
Write the binomial coefficients in rows, row holding . Each entry is the sum of the two above it:
There is a one-line counting proof. Fix one particular item. A -element selection either includes it, and then the other come from the remaining items, or it doesn’t, and then all come from the remaining . The two cases don’t overlap, so the sum rule gives the identity. Click any entry below to see it applied.
A few patterns to look for in the triangle:
- Row sums are powers of 2. , because both sides count the subsets of an -element set.
- Symmetry. : choosing which to take is the same as choosing which to leave.
- The binomial theorem. Row holds the coefficients of . Row 4 is 1, 4, 6, 4, 1, and .
- Odd entries form a fractal. Turn on “Colour the odd numbers” and add rows: the pattern is Sierpiński’s triangle.
Counting in computer science
Counting is how you size a search space before deciding how to search it. There are subsets of items, orderings, and pairs, which is exactly the number of comparisons a naive “compare every pair” loop makes. The orderings also give the lower bound for comparison sorting explained on the sorting page. And because appears in probability, “how likely is this hash collision?” and “how many test cases cover every pair of options?” are counting questions too.
References
- R. L. Graham, D. E. Knuth and O. Patashnik, Concrete Mathematics, 2nd ed., Addison-Wesley, 1994. Chapter 5 is about binomial coefficients.
- K. H. Rosen, Discrete Mathematics and Its Applications, 8th ed., McGraw-Hill, 2019. Chapter 6.
- “Stars and bars (combinatorics)”, Wikipedia.