TExcelTAD
Home › Discrete Mathematics › The Sum and Product Rules

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 A1,…,AnA_1, \ldots, A_n are disjoint sets, then

∣A1∪A2∪⋯∪An∣=∣A1∣+∣A2∣+⋯+∣An∣\lvert A_1 \cup A_2 \cup \cdots \cup A_n \rvert = \lvert A_1 \rvert + \lvert A_2 \rvert + \cdots + \lvert A_n \rvert

Example: a menu has 4 starters, 6 mains and 3 desserts. Choosing one dish gives 4+6+3=134 + 6 + 3 = 13 options. (If the sets overlap, use inclusion-exclusion instead.)

The product rule

For any sets, the number of sequences (a1,…,an)(a_1, \ldots, a_n) with ai∈Aia_i \in A_i is

∣A1×A2×⋯×An∣=∣A1∣⋅∣A2∣⋯∣An∣\lvert A_1 \times A_2 \times \cdots \times A_n \rvert = \lvert A_1 \rvert \cdot \lvert A_2 \rvert \cdots \lvert A_n \rvert

Choosing a three-course meal gives 4⋅6⋅3=724 \cdot 6 \cdot 3 = 72 options. The number of subsets of an nn-element set is 2n2^n, because a subset is a sequence of nn 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):

P=∑k=68(62k−52k)P = \sum_{k=6}^{8} \left(62^k - 52^k\right)

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.

python · try it yourself
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.

python · try it yourself
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 10⋅5=5010 \cdot 5 = 50 pairs; 25 have i+ji + j even and do 3 units of work, the other 25 do 1, for 75+25=10075 + 25 = 100 units.

In software

The product rule sizes search spaces: a function with 3 Boolean flags and an enum of 5 values has 23⋅5=402^3 \cdot 5 = 40 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 21282^{128} possibilities. UUID version 4 has 122 random bits, about 5.3×10365.3 \times 10^{36} 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 2+1=32 + 1 = 3 options (sum rule). Then 5⋅3⋅4⋅3=1805 \cdot 3 \cdot 4 \cdot 3 = 180 (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

3620≈1.34×103136^{20} \approx 1.34 \times 10^{31}; with a check character, 361936^{19}.

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

264−214=26249526^4 - 21^4 = 262495.

Solution

All strings minus the strings with only consonants:

python · try it yourself
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?

  1. When choices are made in sequence
  2. When choosing one item from disjoint groups
  3. When order doesn't matter
  4. Never

How many 8-bit strings are there?

  1. 8
  2. 16
  3. 64
  4. 256

How many 3-digit PINs (000–999) have at least one 7?

  1. 271
  2. 300
  3. 729
  4. 100