Counting and combinations
About this lesson
How many ways can something happen? Follow one example, five items A to E with three to pick, and count it without listing: the product rule gives 5 × 4 × 3 = 60 ordered picks, dividing by 3! = 6 orders gives 10 teams, and the same 10 appears again from bit patterns, symmetry and Pascal's rule.
Five items, pick three
Counting means finding how many ways something can happen, without listing every way. Our example: five items, A to E. We pick three of them to form a team. How many different teams are there?
>>> items = "ABCDE" >>> len(items) 5
Read the lesson step by step
Five items, pick three
Counting means finding how many ways something can happen, without listing every way. Our example: five items, A to E. We pick three of them to form a team. How many different teams are there?
>>> items = "ABCDE" >>> len(items) 5Product rule: choices multiply
Fill three slots in turn. The first has 5 choices. A is now used, so the second has 4, then the third has 3. The product rule: when a choice is made in stages, multiply. 5 × 4 × 3 = 60 ordered picks.
>>> 5 * 4 * 3 60 >>> import math >>> math.perm(5, 3) # ordered picks of 3 from 5 60Three items, 3! orders
Order counts in those 60 picks. The same three items stand in 3 × 2 × 1 = 6 orders: ABC, ACB, BAC, BCA, CAB, CBA. We write 3!, said 'three factorial'. In general n! = n × (n − 1) × … × 1.
>>> from itertools import permutations >>> ["".join(p) for p in permutations("ABC")] ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA'] >>> import math >>> math.factorial(3) 6Order gone: divide by 3!
A team ignores order: ABC and CBA are the same team. So each team was counted 3! = 6 times among the 60 picks. Divide: 60 ÷ 6 = 10 teams. This is a combination, written C(5, 3) and read '5 choose 3'.
>>> import math >>> math.perm(5, 3) // math.factorial(3) 10 >>> math.comb(5, 3) 10The choose formula
Choosing k items from n gives C(n, k) = n! / (k! (n − k)!). The n! / (n − k)! part counts ordered picks; dividing by k! forgets the order. Here 5! = 120, and 120 / (3! × 2!) = 10.
>>> import math >>> f = math.factorial >>> f(5) // (f(3) * f(2)) 10List all ten teams
Check by listing. A team is a pattern of five lamps with exactly three lit. Python's itertools.combinations lists them in order: ABC, ABD, ABE, and so on to CDE. There are ten, just as the formula said.
>>> from itertools import combinations >>> teams = ["".join(t) for t in combinations("ABCDE", 3)] >>> teams ['ABC', 'ABD', 'ABE', 'ACD', 'ACE', 'ADE', 'BCD', 'BCE', 'BDE', 'CDE'] >>> len(teams) 10In or out: 2⁵ subsets
Drop the size limit. Each item is in or out: two choices, five times, so 2⁵ = 32 subsets. Read the lamps as bits: only 10 of the 32 patterns have exactly three lit.
>>> 2 ** 5 32 >>> sum(1 for m in range(32) if bin(m).count("1") == 3) 10Choosing 3 = leaving 2
Picking the 3 who play is the same as picking the 2 who sit out. Flip every lamp and each team of three becomes a pair of two. So C(5, 3) = C(5, 2) = 10. In general, C(n, k) = C(n, n − k).
>>> import math >>> math.comb(5, 3), math.comb(5, 2) (10, 10)Pascal's rule
Split by cases. Is E in? Yes: pick 2 more from A to D, C(4, 2) = 6. No: pick 3 from A to D, C(4, 3) = 4. No team is in both, so the sum rule adds: 6 + 4 = 10.
>>> import math >>> math.comb(4, 2) + math.comb(4, 3) 10 >>> [math.comb(5, k) for k in range(6)] # row 5 of Pascal's triangle [1, 5, 10, 10, 5, 1]Counting in Python
Python's math module counts for you: math.comb(5, 3) is 10 and math.perm(5, 3) is 60. Counting first tells you how big a search is before you run it: choosing 3 from 50 already gives 19,600 teams.
>>> import math >>> math.comb(5, 3), math.perm(5, 3) (10, 60) >>> math.comb(50, 3) 19600
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A café offers 3 drinks and 4 cakes. How many different drink-and-cake pairs can you order?
The product rule: 3 choices for the drink, then 4 for the cake, whatever drink you chose, so 3 × 4 = 12.
02Gold, silver and bronze go to 3 of 8 runners. How many ways can the medals be given out?
Order matters (gold is not bronze), so it is a permutation: 8 × 7 × 6 = 336, which is math.perm(8, 3).
03How many ways are there to choose a committee of 2 people from 6?
Order does not matter: 6 × 5 = 30 ordered picks, and each pair was counted 2! = 2 times, so 30 ÷ 2 = 15 = C(6, 2).
04Why is C(10, 7) equal to C(10, 3)?
Every choice of 7 leaves exactly one group of 3 behind, and the other way round, so the counts match: both are 120.
05A set has 6 items. How many subsets does it have, counting the empty set and the whole set?
Each item is in or out, two choices six times: 2⁶ = 64. The C(6, k) for k = 0 to 6 add up to the same 64.
Glossary
- Product rule
- If a task is done in stages, with m ways for the first stage and n ways for the second whatever happened first, there are m × n ways in all.
- Sum rule
- If the outcomes split into cases that cannot both happen, the total is the sum of the counts of the cases.
- Factorial (n!)
- The product n × (n − 1) × … × 1: the number of orders of n different items. 0! is defined as 1.
- Permutation
- An ordered pick. The number of ways to pick k items from n in order is P(n, k) = n! / (n − k)!.
- Combination
- An unordered pick, a subset. The number of k-item subsets of n items is C(n, k) = n! / (k! (n − k)!), read 'n choose k'.
- Binomial coefficient
- Another name for C(n, k), because it is the coefficient of xᵏ in (1 + x)ⁿ.
- Pascal's rule
- C(n, k) = C(n − 1, k − 1) + C(n − 1, k): split on whether one fixed item is chosen.
Notes and sources
- The product rule needs the same number of choices at each stage whatever came before (4 remain after the first pick, whichever item it was). The sum rule needs cases that cannot both happen, so no outcome is counted twice.
- 0! is defined to be 1. Then the formula also works at the edges: C(n, 0) = C(n, n) = 1, because there is exactly one way to choose nothing, and one way to choose everything.
- If items may repeat, like the digits of a PIN, every slot keeps all its choices: a 4-digit PIN has 10 × 10 × 10 × 10 = 10,000 possibilities.
- C(n, k) is also called a binomial coefficient: it is the number in front of xᵏ when (1 + x)ⁿ is multiplied out. Row 5 of Pascal's triangle, 1, 5, 10, 10, 5, 1, gives (1 + x)⁵.
- Counts grow fast: 10! = 3,628,800, and C(50, 25) = 126,410,606,437,752, about 1.26 × 10¹⁴. That is why brute-force search over all subsets only works for small n.
- MIT OCW 6.042J Mathematics for Computer Science (Lehman, Leighton, Meyer)
- Mathematics for Computer Science (2018 text), chapter Cardinality Rules (PDF)
- Rosen, Discrete Mathematics and Its Applications, 8th ed., chapter 6 Counting
- Knuth, The Art of Computer Programming, vol. 1, sections 1.2.5 and 1.2.6
- Python library: math.comb, math.perm, math.factorial
- Python library: itertools.combinations and permutations
Quiz Unlocks after the last step.