Adding sizes over-counts overlaps
An element in two sets is counted twice, one in three is counted three times. Subtracting pairwise intersections over-corrects the triples, so they are added back.
When sets overlap the sum rule over-counts. Inclusion and exclusion corrects it exactly, with alternating signs, and it is the tool behind derangements, surjection counts and Euler's totient function.
Core ideas
Each element must be counted exactly once. The alternating signs are what makes that happen for every element regardless of how many sets it lies in.
An element in two sets is counted twice, one in three is counted three times. Subtracting pairwise intersections over-corrects the triples, so they are added back.
An element in exactly m of the sets is counted C(m,1) − C(m,2) + C(m,3) − ⋯, which is 1 for every m ≥ 1 by the alternating-sum identity of “Binomial Coefficients and Pascal's Triangle”.
"None of the properties" equals the total minus the union, and that is how derangements and surjection counts are computed.
The material
The two-set and three-set cases, the general statement, and why it is exact.
|A ∪ B| = |A| + |B| − |A ∩ B| and |A ∪ B ∪ C| = |A| + |B| + |C| − |A∩B| − |A∩C| − |B∩C| + |A∩B∩C|.
For finite sets A₁, …, A_n, |A₁ ∪ ⋯ ∪ A_n| = Σ|Aᵢ| − Σ_{i<j}|Aᵢ∩Aⱼ| + Σ_{i<j<k}|Aᵢ∩Aⱼ∩A_k| − ⋯ + (−1)^{n+1}|A₁∩⋯∩A_n| — an alternating sum over all nonempty subsets of the indices.
Take an element lying in exactly m of the sets, m ≥ 1. It is counted C(m,1) times in the first sum, C(m,2) times in the second, and so on, so its net contribution is C(m,1) − C(m,2) + C(m,3) − ⋯ = 1 − [C(m,0) − C(m,1) + C(m,2) − ⋯].
The bracket is the alternating row sum, which is 0 for m ≥ 1 (“Binomial Coefficients and Pascal's Triangle”). So the contribution is exactly 1.
An element in none of the sets contributes 0 to every term. Hence each element of the union is counted once and nothing else is counted.
□
That proof explains the signs rather than asserting them: they are exactly what is needed to make every element's contribution collapse to 1, and the collapse is the alternating-sum identity from Pascal's triangle.
If Aᵢ is the set of objects with property i, then the number with none of them is |U| − Σ|Aᵢ| + Σ|Aᵢ∩Aⱼ| − ⋯, the same alternating sum with the signs shifted.
How many of 1, …, 1000 are divisible by none of 2, 3, 5? 1000 − (500 + 333 + 200) + (166 + 100 + 66) − 33 = 266. Each term is a floor division: ⌊1000/6⌋ = 166 counts the multiples of both 2 and 3.
That intersection term is ⌊N/6⌋ because 2 and 3 are coprime. In general the multiples of both a and b are the multiples of lcm(a, b), not of ab: of 1, …, 100, the numbers divisible by both 4 and 6 are the multiples of 12, ⌊100/12⌋ = 8 of them, and ⌊100/24⌋ = 4 undercounts. The lab computes every intersection through the lcm for exactly this reason.
The count of integers in 1, …, n coprime to n is φ(n) = n·Π(1 − 1/p) over the distinct primes p dividing n. That product is inclusion and exclusion over the prime divisors, multiplied out. Number Theory and Cryptography uses φ throughout.
The number of surjections from an m-set onto an n-set is Σ_{k=0}^{n} (−1)^k C(n,k)(n−k)^m — total functions minus those missing at least one target, corrected.
The pattern in all three: the direct count is hard and the count of "violates condition i" is easy, so count the violations and alternate. Recognising that shape is what this lesson is for.
Interactive
Formula and brute force, side by side
Method
Define the sets so the intersections are easy.
Usually the bad property. Counting Aᵢ and its intersections must be easier than counting the objects you want.
Pairs, triples, and so on. Symmetry often makes many of them equal, which is what keeps the sum manageable.
Add the singles, subtract the pairs, add the triples. Off by one in the sign pattern gives an answer that is wrong by twice the correction.
The answer must lie between 0 and the total. A negative result means a sign error; a result above the total means a missed subtraction.
How many functions from {1,…,5} onto {a,b,c} hit every target?
0⁵ = 0 is the right value here: no function can miss all three targets when the domain is nonempty. The convention 0⁰ = 1 would matter if the domain were empty, which is exactly the edge case such formulas are tested on.
Count the permutations of 1, …, 6 in which no element stays in its own position. Define Aᵢ as "element i is fixed", compute the intersections, and alternate. “Derangements” is that answer in general.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.