Many questions in mathematics and computing ask how many, and listing the answers stops being possible almost at once: there are more subsets of a forty element set than there are stars in the Milky Way.
This lesson collects the four tools that count without listing. Two of them, the sum and product rules, build a count from smaller counts. The third counts a set by pairing it perfectly with a set that is easier to count. The fourth, the pigeonhole principle, does something stranger: it uses a count to prove that an object exists without ever producing it. The lesson assumes only sets and functions, and it recalls what a bijection is where it is needed.
The sum rule
If a set splits into pieces that do not overlap, its size is the sum of the sizes of the pieces. Written with sets, if and are disjoint then , and the same holds for any finite number of pairwise disjoint pieces. The proof is hardly a proof: counting every element of the union once is the same as counting the elements of and then carrying on through , and disjointness guarantees that nothing is met twice.
The rule is almost too obvious to state, and its value lies in what it permits. A hard count can be broken into cases, each case counted by whatever method suits it, and the answers added. The one condition is that the cases must not overlap, and a case analysis that quietly double counts is the most common error in the subject. When the pieces do overlap, the sum overcounts by exactly the elements that are shared, and correcting for that exactly is the business of a later lesson on inclusion and exclusion.
A close relative is counting by complement. If sits inside a universe , then and its complement split into two disjoint pieces, so . This is the sum rule read backwards, and it is the right move whenever a condition says "at least one": the complement says "none", which is usually far easier to count. How many integers from to contain at least one digit ? Pad each with leading zeros to three digits; the strings with no anywhere number , which includes , so of the integers from to avoid the digit, and the remaining contain it.
That last count already used a multiplication, and the rule behind it deserves its own section.
The product rule
The probability course derives the product rule from a grid of choices: if a first choice can be made in ways and, whatever it was, a second in ways, the pair can be made in ways. With stages it becomes a product of factors. The proof is the sum rule in disguise: group the outcomes by the first choice, so that there are disjoint groups of each, and add.
That grouping also shows exactly what the rule needs. The later stages may depend on the earlier ones in which options they offer, but not in how many. Consider four digit PINs in which no two adjacent digits are equal. The first digit has options. The second must differ from the first, and whichever the first was, that leaves . The third must differ from the second, again , and the fourth more, giving . The set of allowed digits changes at every stage, but its size does not, and size is all the rule looks at.
Two real systems show the rule at work. The Dartmouth BASIC of 1964 allowed a variable name to be a single letter or a letter followed by one digit. Those two kinds of name are disjoint, so the sum rule adds them, and the product rule counts the second kind: variables in all. The genetic code reads DNA, an alphabet of four bases, in words called codons that specify twenty amino acids and a stop signal. Words of two bases would give only possibilities, too few for twenty one meanings; words of three give , which is enough with room to spare, and the real code is indeed read in triplets, most amino acids having several codons.
Example. How many integers from to contain no digit ?
Split by the number of digits, since integers of different lengths form disjoint sets and the sum rule will add them. An integer with digits and no zero has each of its digits chosen from to , so by the product rule there are of them. The total is . Padding with leading zeros would not have worked directly here, because the padded zeros are exactly what the condition forbids, which is why the cases were needed.
Now you. How many integers from to have all their digits different?
Answer
Split by length again. One digit integers: . Two digits: the first from to , the second any of the digits other than the first, so . Three digits: , since the third must avoid two digits already used. The total is .
Counting one set by counting another
A bijection from to is a function that pairs every element of with exactly one element of and leaves nothing in unpaired. For finite sets that is the definition of having the same size, so as soon as a bijection exists, and the second set may be much easier to count than the first. Much of combinatorics consists of finding the right .
The cleanest illustration is a knockout tournament. The Wimbledon singles draw has players; how many matches are played? One could count rounds: in the first, then , , , , and the final, totalling . The bijection argument is shorter and more general. Every match produces exactly one loser, and every player except the champion loses exactly once, since a loss ends their tournament. So matches pair off perfectly with the players who are not the champion, and there are of them. The same argument works for a draw of players with byes, where counting by rounds becomes awkward: matches, whatever the arrangement of the byes.
A bijection is a claim that has to be checked in both directions. It must be a function (each element of goes somewhere definite), it must be one to one (different elements go to different places), and it must be onto (everything in is hit). The usual way to settle the last two at once is to write down the inverse: a rule taking each element of back to the element of it came from. In the tournament, the inverse takes a non-champion to the match they lost, which is plainly well defined.
Divisors give a more arithmetical example. Every positive integer factors into primes in exactly one way, a fact proved in a later lesson, so . A divisor of can only use these primes, and no more of each than has, so it is with , and . Uniqueness of factorisation makes the map from exponent triples to divisors a bijection, and the triples are counted by the product rule.
Example. How many positive divisors does have?
By the bijection just described, the divisors correspond to triples with one of the values to , one of the values to , and one of the values or . The product rule gives . In general, a number whose factorisation has exponents has divisors, the in each factor accounting for the option of leaving that prime out.
Now you. How many positive divisors does have, and how many of them are perfect squares?
Answer
, so there are divisors. A divisor is a perfect square exactly when both exponents are even, which allows and each to be , , or , giving square divisors.
Subsets and bit strings
How many subsets does a set of elements have? List the elements in some fixed order, , and send each subset to the string of bits whose th bit is if and if not. The subset of a four element set becomes , the empty set becomes and the whole set . The inverse reads the string and collects the positions holding a , so this is a bijection, and the bit strings are counted by the product rule: each of the positions has options, giving .
The same picture explains why computers store sets this way. A subset of a element set fits exactly in one bit machine word, union becomes bitwise OR, and intersection becomes bitwise AND. It also shows how fast subsets multiply: a thirty element set has of them, so an algorithm that checks every subset of its input is hopeless well before the input reaches a hundred items.
Bijections can also compare two sets without counting either. Among the subsets of , with , which are more numerous, those of even size or those of odd size? Pair each subset with the one obtained by toggling the element : add it if absent, remove it if present. Toggling twice returns the original subset, so the map is its own inverse and therefore a bijection, and it changes the size by exactly one, so it pairs every even subset with an odd one. The two kinds are equal in number, each, and no formula for the number of subsets of a given size was needed to see it.
The pigeonhole principle
If more than objects are placed in boxes, some box holds at least two. The proof is by contradiction: if every box held at most one, there would be at most objects. What makes the principle powerful is what it does not say. It names no box and no pair, so it proves existence without construction, and the art is entirely in choosing the boxes.
A counting fact about files shows the flavour. No lossless compressor can shorten every file. There are files of exactly bits, but only files shorter than that. If every bit file were sent to a shorter one, two would land on the same output, and decompression could not tell them apart. Every compressor that shrinks some files must lengthen others.
Example. Prove that any integers chosen from to include two of which one divides the other.
Write each integer as with odd; is its odd part. The odd parts of numbers from to lie among the ten odd numbers , so these are the boxes. Eleven integers in ten boxes put two, say and with , in the same box, and then the first divides the second. The bound is sharp: the ten integers to contain no such pair, since the smallest multiple of beyond itself is . This is a result of Paul Erdős, stated for integers from to .
Now you. Prove that any integers chosen from to include two that sum to .
Answer
Use as boxes the five pairs , , , and , each summing to and together covering to . Six integers in five boxes put two in the same pair, and they sum to . Five integers are not enough: to contains no such pair.
The generalised form
If objects go into boxes, some box holds at least of them, the ceiling meaning rounded up. Again by contradiction: if every box held at most , which is less than , the total would be less than .
The 2021 census counted people in London. A human scalp carries around a hundred thousand hairs, and certainly nobody has a million, so the possible hair counts are to , a million boxes. Some hair count is therefore shared by at least Londoners. Nobody will ever find them, and the argument does not need to.
Read the other way, the principle gives thresholds. To guarantee that three people share a birth month, people are needed: twenty four could be spread two per month.
What the rules leave open
The sum and product rules, bijections and the pigeonhole principle count anything whose structure can be described, and the structures that turn up most often are arrangements and selections. How many orders can ten runners finish in, or how many committees of three can ten people form? The product rule answers the first and, with a correction for order, the second, but the same reasoning recurs so often that it deserves formulas of its own. The next lesson derives them, and extends them to selections in which repetition is allowed.