The Sum and Product Rules
Counting answers "how many?" without listing everything: how many passwords, how many possible inputs to test, how many steps an algorithm can take, how likely an event is. Almost every counting argument starts from two rules: add when choices are alternatives, multiply when choices are made in sequence.
The sum rule
If are disjoint sets, then
Example: a menu has 4 starters, 6 mains and 3 desserts. Choosing one dish gives options. (If the sets overlap, use inclusion-exclusion instead.)
The product rule
For any sets, the number of sequences with is
Choosing a three-course meal gives options. The number of subsets of an -element set is , because a subset is a sequence of in/out choices (Finite Cardinality).
Combining the rules: passwords
A system requires passwords of 6 to 8 characters, each a letter (upper or lower case, 52 options) or a digit (10 options), with at least one digit. Count them.
Split by length (sum rule), and within each length count all strings minus those with no digit (product rule twice):
Counting "at least one" directly is messy; counting the complement is easy. This complement trick is one of the most useful in counting and probability.
from itertools import product
total = sum(62 ** k - 52 ** k for k in range(6, 9))
print(f"passwords: {total:,}")
# sanity check of the method on a tiny alphabet: letters {a, b}, digits {0}, length 1..3
letters, digits = "ab", "0"
brute = sum(1 for k in range(1, 4) for s in product(letters + digits, repeat=k) if any(c in digits for c in s))
formula = sum(3 ** k - 2 ** k for k in range(1, 4))
print("tiny case, brute force:", brute, " formula:", formula)passwords: 167,410,949,583,040
tiny case, brute force: 25 formula: 25
Checking a counting formula against brute force on a tiny instance is a habit worth keeping: off-by-one and double-counting errors are common.
Counting loop iterations
The product rule counts nested loops with independent ranges, and the sum rule counts branches.
count = 0
for i in range(10): # 10 choices
for j in range(5): # 5 choices each
if (i + j) % 2 == 0:
for k in range(3):
count += 1
else:
count += 1
print("work units:", count)
print("by counting: pairs with i+j even:", sum(1 for i in range(10) for j in range(5) if (i + j) % 2 == 0))work units: 100
by counting: pairs with i+j even: 25
There are pairs; 25 have even and do 3 units of work, the other 25 do 1, for units.
In software
The product rule sizes search spaces: a function with 3 Boolean flags and an enum of 5 values has input combinations, which tells you whether exhaustive testing is feasible (pairwise testing tools exist because these products explode). It also sizes key spaces: a 128-bit key has possibilities. UUID version 4 has 122 random bits, about values.
Exercises
Exercise 1: Outfits
You own 5 shirts, 3 pairs of trousers and 4 pairs of shoes, and may also choose to wear one of 2 hats or no hat. How many outfits?
Answer
Solution
The hat choice has options (sum rule). Then (product rule).
Exercise 2: Licence keys
A key is 4 groups of 5 characters from 0–9 and A–Z. How many keys? How many if the last character is a check digit computed from the others?
Answer
; with a check character, .
Solution
20 positions, 36 choices each. A check character is determined by the other 19, so it adds no choices.
Exercise 3: At least one vowel (code)
How many 4-letter strings over the 26 lowercase letters contain at least one vowel (a, e, i, o, u)? Use the complement and check by brute force over a smaller alphabet.
Answer
.
Solution
All strings minus the strings with only consonants:
from itertools import product
print(26 ** 4 - 21 ** 4)
alpha, vowels = "abcde", "ae"
brute = sum(1 for s in product(alpha, repeat=4) if any(c in vowels for c in s))
print(brute, 5 ** 4 - 3 ** 4)262495
544 544
Test yourself
When do you use the sum rule?
- When choices are made in sequence
- When choosing one item from disjoint groups
- When order doesn't matter
- Never
How many 8-bit strings are there?
- 8
- 16
- 64
- 256
How many 3-digit PINs (000–999) have at least one 7?
- 271
- 300
- 729
- 100