Find a course

Module 4: CountingLesson 8 of 1214 min

Counting Principles and the Pigeonhole Principle

Count outcomes with the sum and product rules, count the complement when it is easier, and use the pigeonhole principle to prove that collisions must happen.

Step 1 of 13Learn

The product rule

How many passwords of exactly 8 characters can be made from 26 lowercase letters and 10 digits? Listing them is hopeless. Counting them takes one line.

The product rule. If a task consists of a sequence of steps, with m ways to do the first step and n ways to do the second (no matter how the first was done), then there are m·n ways to do the task. This extends to any number of steps.

Each of the 8 password positions has 36 choices, so there are 36⁸ ≈ 2.8 trillion passwords.

More examples:

  • A restaurant offers 4 appetizers, 6 mains and 3 desserts. A three-course meal can be chosen in 4·6·3 = 72 ways.
  • A license plate with 3 letters followed by 4 digits: 26³·10⁴ = 175,760,000 plates.
  • A byte is 8 bits, each 0 or 1: 2⁸ = 256 possible bytes. This is why one byte can store values from 0 to 255.

The sum rule. If a task can be done in one of several non-overlapping ways, with m ways of the first kind and n of the second, then there are m + n ways in total. If a student can pick a project from a list of 12 math topics or 9 computer science topics, with no topic on both lists, there are 12 + 9 = 21 choices.

The rules combine: passwords of length 6, 7 or 8 from 36 characters number 36⁶ + 36⁷ + 36⁸, using the product rule for each length and the sum rule across lengths.

The word to listen for: "and then" suggests multiplying; "or" (with no overlap) suggests adding.

Step 2 of 13Diagram

Product rule: 2 shirts × 3 trousers

Every shirt branches into every pair of trousers: 2 × 3 = 6 leaves, six outfits.

Product rule: 2 shirts × 3 trousersHierarchy diagram: Outfit, Red shirt, Blue shirt, Jeans, Chinos, Shorts, Jeans, Chinos, Shorts.OutfitRed shirtBlue shirtJeansChinosShortsJeansChinosShorts
Outfit
Red shirt
Blue shirt
Jeans
Chinos
Shorts
Jeans
Chinos
Shorts

Step 3 of 13Practice · Work it out

A sandwich shop offers 3 breads, 5 fillings and 4 sauces. A sandwich is one bread, one filling and one sauce. How many different sandwiches are possible?

Step 4 of 13Try the calculator

How strong is a password?

With an alphabet of N characters and length L, there are NL possible passwords. The time to try them all depends on how many guesses an attacker makes per second. Compare 8 characters from 26 letters with 12 characters from 94 printable symbols.

Possible passwords
839,299,365,868,340,200
Bits of entropy (log₂ of the count)
59.5
Years to try them all
2.66

Step 5 of 13Learn

Counting the complement

Sometimes the things you don't want are easier to count. Count everything, then subtract.

Example. How many 4-digit PINs (0000 to 9999) have at least one repeated digit?

Counting "at least one repeat" directly means handling one pair, two pairs, triples and so on. Instead, count the PINs with no repeats: 10·9·8·7 = 5,040 (each position must avoid the digits already used). There are 10⁴ = 10,000 PINs in total, so 10,000 − 5,040 = 4,960 have a repeat. Almost half.

Example: the birthday problem. In a room of 23 people, what's the chance that two share a birthday? Count the complement: the number of ways all 23 birthdays are different is 365·364·⋯·343, out of 365²³ equally likely assignments. That ratio is about 0.49, so the chance of a shared birthday is about 51%, surprising most people. With 50 people it's about 97%.

The phrase "at least one" is a strong hint to count the complement, because "at least one" covers many cases while its opposite, "none," is a single case.

Overlapping cases. The sum rule requires the cases not to overlap. If they do, use inclusion–exclusion. How many bit strings of length 8 start with 1 or end with 00? Starting with 1: 2⁷ = 128. Ending with 00: 2⁶ = 64. Both: 2⁵ = 32. Total: 128 + 64 − 32 = 160.

Step 6 of 13Practice · Chart

All 10,000 four-digit PINs

Tap the slice that is easier to count directly, then subtract.

Tap the slice that answers the question, then press Check.

All 10,000 four-digit PINsPie chart with 2 categories: No repeated digit: 5,040 (50.4%); At least one repeat: 4,960 (49.6%).50%50%
Exact counts: 10 · 9 · 8 · 7 = 5,040 and 10,000 − 5,040 = 4,960.
All 10,000 four-digit PINs
ItemPINs
No repeated digit5,040
At least one repeat4,960

Step 7 of 13Practice · Work it out

How many 5-letter strings using the 26 lowercase letters contain at least one vowel (a, e, i, o, u)? Repetition is allowed.

Step 8 of 13Learn

The pigeonhole principle

If 10 pigeons fly into 9 holes, some hole holds at least two pigeons. That obvious observation, the pigeonhole principle, proves surprisingly strong results.

Pigeonhole principle. If more than n objects are placed into n boxes, at least one box contains two or more objects.

Generalized version. If N objects are placed into k boxes, some box contains at least ⌈N/k⌉ objects (N divided by k, rounded up).

Examples:

  • In any group of 367 people, two share a birthday (366 possible birthdays, counting February 29). Notice this is a certainty, unlike the 23-person probability.
  • Among 100 people, at least ⌈100/12⌉ = 9 were born in the same month.
  • A drawer has black and white socks. How many must you pull out in the dark to guarantee a matching pair? Three: with two colors as boxes, 3 socks force a repeat.
  • Hash collisions: if a program maps a million user names to 1,000 buckets, some bucket holds at least 1,000 names. Any hash function that squeezes a large set into a smaller one must have collisions; good design can only spread them out.
  • Compression: no lossless compression algorithm can shrink every file. There are 2ⁿ files of n bits but fewer than 2ⁿ shorter files, so two inputs would have to share an output.

The principle tells you that a collision exists, not where. The art lies in choosing what counts as the pigeons and what counts as the holes.

Step 9 of 13Explore

Generalized pigeonhole: ⌈n/k⌉

With n pigeons in k holes, some hole gets at least ⌈n/k⌉. The staircase shows the guarantee as n grows.

51015 50100150200
Some hole has at least
4

Step 10 of 13Practice · Choose one

A class has 40 students. What is the largest number k that guarantees at least k students were born on the same day of the week?

Step 11 of 13Note

Step 12 of 13Practice · True or false

If 13 socks are pulled from a drawer containing 12 different colors, at least two of them must be the same color.

Step 13 of 13Practice · Choose one

How many 3-digit numbers (100 to 999) are divisible by 5 or have all three digits odd?