When the sets being counted overlap, adding their sizes counts some elements more than once, and the question is how to correct the total exactly.
The sum rule adds the sizes of sets that do not overlap, and the previous lesson ended where it fails: of the integers from to , fifty are divisible by and thirty-three by , but is too many, because the multiples of are in both lists. This lesson builds the exact correction for any number of sets and spends it on three problems that are hard to attack any other way. It assumes the language of sets and the binomial coefficient , the number of element subsets of an element set.
Two overlapping sets
Write for the number of elements of a finite set . The sum counts every element of , but it counts the elements of twice, once in and once in . Subtracting removes the second copy and nothing else:
Check this element by element, because that check is the proof that generalises. An element in only contributes to the right side, through ; one in only contributes through ; one in both contributes ; one in neither contributes nothing. Each element of the union is counted once.
For the question left open, let be the multiples of up to and the multiples of . An integer is in both exactly when it is divisible by , and there are of those, where means rounded down. So , and integers are divisible by neither, which is often the count actually wanted.
The one step needing thought was the intersection. For divisors sharing a factor it is the multiples of the least common multiple: divisible by both and means divisible by , not .
Three sets
With three sets, start from and subtract the three pairwise intersections, since elements in two sets were counted twice. An element in all three sets, though, was counted three times and then subtracted three times, once for each pair, so it is now counted times and has to be put back:
Element by element: one in exactly one set is counted once; one in exactly two, say and , is counted by and and subtracted by alone, a net ; one in all three is counted time. The numbers being added and subtracted, ; ; , are rows of Pascal's triangle, which is the clue to the general case.
Example. How many integers from to are divisible by at least one of , and ?
Let , and be the multiples of , and . The singles are , and , totalling . The pairs are the multiples of , and , numbering , and , totalling . The triple is the multiples of . So the union has elements, and integers up to are divisible by none of the three. Testing each integer by computer gives the same .
Now you. How many integers from to are divisible by at least one of , and ?
Answer
The singles are , and . The pairwise intersections are the multiples of the least common multiples , and , numbering , and , and the triple is the multiples of . The count is . Using products (, , and ) instead would give a wrong answer.
The general formula
Take finite sets . For each from to , let be the result of choosing of the sets in every possible way, intersecting each choice, and adding up the sizes of those intersections. So is , adds the sizes of all pairwise intersections, and is the size of the intersection of all sets. The principle of inclusion and exclusion says
In words: add the sizes of the sets, subtract the pairwise overlaps, add the triple overlaps, and keep alternating.
The proof counts how many times each element is included. Take an element in exactly of the sets, with . An intersection of sets contains precisely when all chosen sets are among the that contain it, and there are such choices. So is counted times in (zero times once ), and its net count is
The previous lesson proved that the alternating sum of a row of Pascal's triangle vanishes, for , because a nonempty set has as many subsets of even size as of odd size. The displayed sum is that row with its first term, , removed and every sign flipped, so it equals . Every element of the union is counted exactly once, and one in none of the sets appears in no term.
Usually the sets live inside a universe of elements, each collecting the elements with some unwanted property, and the question is how many have none of the properties. Subtracting the union from flips every sign, giving the sieve form used in the rest of the lesson:
Abraham de Moivre used the idea in his Doctrine of Chances of 1718. Its cost is that sets have intersections, over a billion for thirty sets, so it is a practical method only when every intersection of sets has the same size, say . Then and the formula collapses to one sum of terms, as in each application below.
Onto functions
A function from a set to a set is onto if every element of is assigned to something. If and , the product rule gives functions in all. "Everything is hit" constrains the whole function at once, but its failure splits into simple pieces: the function misses some particular element.
So let be the functions that miss the th element of . A function missing given elements sends all of into the other , which can be done in ways whichever were chosen. The intersections are uniform, and the number of onto functions is
Two quick checks: with and nothing is onto, and the formula gives ; with onto means bijection, and it gives .
Example. Five different tasks are shared among three people so that everyone gets at least one. In how many ways?
An allocation is a function from tasks to people, and "everyone gets at least one" means onto. The count is .
Now you. Six students are assigned to four different projects, each project needing at least one student. How many assignments are there?
Answer
Count onto functions from students to projects: .
Derangements and the hat check problem
At a party, guests leave their hats at the door, and the attendant, having lost the tickets, hands them back at random. What is the chance that nobody gets their own hat? Pierre Rémond de Montmort posed the question in 1708 as a card game called treize.
Handing back the hats is a permutation of the guests, one of . A permutation that sends nothing to itself is a derangement, and is the number of derangements of elements. Let be the permutations that give guest their own hat. Fixing given guests leaves the other hats to be arranged freely, in ways, so and the sieve gives
Example. Five guests. How many ways are there to return the hats so that nobody gets their own, and what is the probability?
The terms are for to with alternating signs, so , and the probability is . Listing all permutations by computer finds the same .
Now you. Six guests. What is the probability that exactly two of them get their own hats back?
Answer
Choose the two lucky guests in ways; the other four must form a derangement, and . So of the permutations qualify, a probability of .
The probability that nobody gets their own hat is , the bracket in the formula, and its values are strange.
| 2 | 1 | 0.500000 |
| 3 | 2 | 0.333333 |
| 4 | 9 | 0.375000 |
| 5 | 44 | 0.366667 |
| 6 | 265 | 0.368056 |
| 7 | 1854 | 0.367857 |
| 8 | 14833 | 0.367882 |
| 10 | 1334961 | 0.367879 |
The answer barely depends on : eight guests or a thousand agree to four decimal places. The bracket is the start of the series at , so it converges to . It converges fast, because in an alternating series with shrinking terms, stopping early is wrong by less than the first term left out, here . Multiplying by , is within of , so is rounded to the nearest integer: , and .
Euler's totient
For a positive integer , Euler's totient counts the integers from to that share no factor with except . So , counting , , and , and for a prime . Euler introduced it in 1763, and it drives a later lesson's generalisation of Fermat's little theorem and with it the RSA cryptosystem.
An integer shares a factor greater than with exactly when some prime divisor of divides it. So let be the distinct primes dividing , and the multiples of from to . A number lies in several of these sets exactly when it is divisible by the product of those primes, and since that product divides , its multiples up to number exactly divided by it. Subtract for each prime, add back for each pair of primes, subtract for each triple, and so on. The alternating sum factorises:
To see why, expand the product as the previous lesson expanded , choosing or from each bracket. Each set of primes arises once, as the brackets that supplied , with sign for an even number of primes and for an odd number. That is the sieve, term for term.
Take . The sieve reads , and the product reads . For , . Only which primes divide matters, not how often, and a computer check of every number confirms both.
Counts built from smaller counts
The three applications share a shape. Each negated a condition on the whole object (every person busy, nobody with their own hat, no prime shared) into simple failures, one per person, guest or prime, whose intersections depended only on how many failures were demanded. That uniformity is what to look for.
The derangement numbers hide something else: , so that . Guest gets some guest 's hat, in ways. If guest gets hat back, the other guests form a derangement, in ways. If not, treat hat as guest 's forbidden hat, and the guests other than guest form a derangement, in ways.
That description needs no alternating sum: it defines each count through smaller versions of itself. Such descriptions are recurrences, and the next lesson is about setting them up and solving them.