Enter the sizes of the sets and their overlaps in the panel on the right: ∣A∣, ∣B∣, the pairwise intersections, and with three sets the triple intersection too.
The 2 sets and 3 sets buttons switch between the three-term formula and the seven-term one.
The large number in each region is how many times that region has been counted so far — not how many elements it holds.
The small grey number underneath is the element count of that region, worked out from the sizes you entered.
Press Next term to apply the terms one at a time, Previous to step back, and Reset to return to zero.
Play steps through the whole formula automatically, and Pause stops it wherever it has reached.
The region being changed by the current term is tinted: green when the term is being added, red when it is being subtracted.
The running total beside the diagram is the value of the formula so far, and the counter reads which term you are on.
The goal is to get every region reading exactly 1. That is the moment the running total equals the size of the union.
A region marked with a cross has a negative element count, which means the sizes you entered cannot describe any real collection of sets.
Times counted
term 0 of 7
added by this termsubtracted by this termalready at 1
Nothing counted yet
Every region reads 0, because no term has been applied.
The big number in each region is how many times that region has been counted, not how many elements it holds. Its element count is the small grey number underneath. The job of the formula is to get every big number to exactly 1: each element counted once, no more and no less.
The tool walks the inclusion-exclusion formula one term at a time and shows what each term does to the picture.
Enter the sizes on the right — ∣A∣, ∣B∣, and the overlaps — then press Next term repeatedly. Each press applies one term of the formula and updates every region.
Two numbers sit in each region, and mixing them up makes the whole display confusing:
• The large number is how many times that region has been counted so far. • The small grey number underneath is how many elements the region actually holds.
The large numbers all start at 0 and the entire job of the formula is to get every one of them to exactly 1. When that happens, each element of the union has been counted once and the running total is the answer.
Before any term is applied every region reads 0. The picture is drawn, the element counts are known, and the bookkeeping has not started.
Before any term, frozen
Large numbers all zero, small grey element counts already final. Only the bookkeeping changes from here.
This is the state to look at when the two numbers per region are still unfamiliar, because they are maximally different here. The small grey numbers are already final — they are the element counts, fixed by the sizes you entered. The large numbers are all zero, because no term has counted anything.
Nothing about the sets changes from here on. Every step that follows changes only the large numbers, moving each region towards 1.
That framing is what makes the walk-through a proof rather than a demonstration. The claim is not that the formula gives the right answer on these numbers; it is that each region ends at a multiplicity of exactly 1, which forces the total to be the size of the union whatever the sizes happen to be.
The right-hand panel takes the sizes, and with three sets there are seven of them: the three set sizes, the three pairwise intersections, and the triple intersection.
Those seven numbers determine everything else. The tool works backwards from them to the size of each region, which is why the small grey numbers change as soon as you type.
The relationship runs both ways. A region count is what is left after removing the overlaps — the part of A alone is ∣A∣−∣A∩B∣−∣A∩C∣+∣A∩B∩C∣, which is inclusion-exclusion applied to a single set. The input-mode toggle lets you enter region counts directly instead, and the sizes are then derived from those.
Not every combination of numbers is possible, and the tool says so rather than drawing something incoherent — see sizes that cannot happen.
Set every overlap to zero and the formula has nothing to correct, which is worth seeing on its own: nothing overlaps.
When Nothing Overlaps
Set all the intersections to zero and the sets are pairwise disjoint. Every overlap region holds no elements, and the formula has nothing to correct.
Every overlap zero, frozen
The three overlap regions hold nothing, so the corrections have nothing to correct and the union is a plain sum.
Here the union is simply the sum: ∣A∪B∪C∣=∣A∣+∣B∣+∣C∣. Stepping through confirms it — the three additions do all the work, and the four correction terms each subtract or add zero.
This is worth seeing before the general case, because it isolates what the principle is for. Inclusion-exclusion exists only to repair double counting. With no overlap there is no double counting, and the formula degenerates into the obvious answer.
It also explains why the addition rule for disjoint events in probability needs no correction term while the general one does. Disjointness is exactly the condition that makes the intersections vanish.
Give any one intersection a positive size and the correction terms start doing work — the ordinary case runs from the first addition onward.
Sizes That Cannot Happen
Enter ∣A∣=10 alongside overlaps that need more room than that, and a region comes out negative. The tool marks it with a cross instead of an element count.
Sizes that cannot happen, frozen
A crossed region has a negative element count — a proof that no collection of sets has these sizes.
A negative region is not a rounding artefact — it is a proof that no collection of sets has the sizes you entered. Region counts are element counts, and no set has a negative number of elements.
Two constraints are being violated, and the second is easy to miss. An overlap can never exceed either set it sits inside, so ∣A∩B∣ is at most the smaller of ∣A∣ and ∣B∣. Beyond that, once the triple overlap is fixed, each pairwise overlap has to leave enough room for what remains — push ∣A∩B∩C∣ up too far and the pairwise-only slivers go negative while every individual number still looks reasonable.
The crossed region tells you where to start working backwards. This is also why a walk-through can end with regions that are not all 1: the arithmetic is faithful, and it is reporting that the input was impossible — see when the walk-through does not resolve.
Stepping Through the Terms
With three sets the formula has seven terms, applied in a fixed order: the three set sizes first, then the three pairwise intersections, then the triple.
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣
Each press of Next term applies exactly one of these. The regions that term touches are tinted — green while a term is being added, red while one is being subtracted — and their large numbers move by one in that direction.
Watching the order matters, because the story has three distinct phases. The additions build an over-count. The subtractions overshoot in the middle. The final addition repairs the overshoot.
With ∣A∣ and ∣B∣ applied, every region inside those sets has gone up by one. The lens where they meet reads 2, and the tint marks what the current term just touched.
|A| and |B| applied, frozen
The lens reads 2. Elements in both sets were counted once by each, which is the over-count the subtractions exist to fix.
The over-count appears immediately, and it appears for a reason that needs no arithmetic: an element in both A and B was counted once when A was added and once again when B was added. Nothing was done wrong — adding the sizes is exactly what the first terms are supposed to do.
Regions reading 2 or more are tinted amber to mark them as unfinished. Every subsequent subtraction is aimed at those regions, and at nothing else.
Notice too that regions in exactly one set are already correct at 1, and every later term leaves them alone. The corrections are surgical: each one touches only the regions inside the intersection it names.
Three terms applied, and the running total is too big. Regions in one set read 1, regions in two read 2, and the centre — in all three — reads 3.
All three sets in, frozen
Ones, twos, and a three in the centre: every element counted once per set containing it. The peak of the over-count.
This is the peak of the over-count, and the numbers say exactly how bad it is. Each element has been counted once for every set containing it, so its multiplicity is the number of sets it lies in.
The repair is not a guess. Each region reading 2 lies in exactly one pairwise intersection, so subtracting that intersection brings it to 1. Every over-counted region is reachable by a term the formula already contains, which is why the corrections are the intersections and not something invented.
The centre is the case that does not resolve so neatly. It reads 3 and needs to reach 1, so it needs removing twice — but it lies inside all three pairwise intersections, so the three subtractions will reach it three times. That mismatch is the whole reason the formula has a seventh term.
The first pairwise overlap is being removed. The regions inside A∩B drop by one and are tinted red, and the running total falls.
Subtracting |A ∩ B|, frozen
The A∩B sliver lands on 1 and stops. The centre drops too, from 3 to 2, because it sits inside this overlap as well.
For the region in A and B but not C, this is the end of the story: it was counted twice, one count has been removed, and it now reads 1 and stays there.
For the centre it is not. The centre lies inside A∩B too, so it also drops — from 3 to 2 — even though it needs a different amount of correction than its neighbour. The subtraction cannot distinguish them, because both are inside the intersection being subtracted.
That is the structural problem the formula has to solve. A term aimed at one region unavoidably hits every region contained in it, so corrections overshoot on the deeper overlaps and the overshoot has to be repaired in turn.
All three pairwise subtractions are done. Every region in exactly two sets now reads 1 and is correct — and the centre reads 0.
All three subtractions done, frozen
Counted up three times, removed three times: the centre reads 0, and nothing in all three sets is being counted.
The centre was counted 3 times by the three sets, then removed 3 times by the three pairwise overlaps, because it lies inside every one of them. Three up, three down, and elements in all three sets are currently not being counted at all.
This is the moment that makes the final term inevitable. +∣A∩B∩C∣ is not an adjustment someone found by trial; it is the unique correction that brings a region at 0 back to 1.
It is also where the two most common wrong versions of the formula fail. Stopping here leaves the total short by the size of the triple overlap. Subtracting the triple instead of adding it drives the centre to −1, which is visibly impossible.
Both are easy to spot once you are watching the region numbers rather than the algebra — which is the argument for reading the picture at all. One more term and every region reads one.
Every Region Counted Once
The triple overlap is added back and the centre returns to 1. Every region on the diagram now reads exactly 1, and the running total is the size of the union.
All seven terms applied, frozen
Every region reads exactly 1. The running total is now the size of the union, and the proof is finished.
That is the proof, finished. The formula is not a recipe to memorise — it is a bookkeeping correction: add everything, remove what you double counted, put back what you removed too often.
The check is available directly. The element counts of all seven regions sum to the same number the formula produced, because the regions partition the union and each is now counted exactly once.
The pattern continues past three sets without changing character. For four sets you add the four sizes, subtract the six pairs, add the four triples, and subtract the quadruple — alternating by the number of sets in each term, for the reason set out in why the signs alternate.
What generalises is not the list of terms but the requirement they satisfy: every region ends at a multiplicity of one.
Reading the Region Numbers
The large number in a region is its multiplicity — the number of times the terms applied so far have counted it.
Colour tracks what that number means. A region reading exactly 1 is tinted in the accent colour, because it is correct. A region reading 2 or more, or less than 0, is tinted amber, because it is wrong and still needs work. Regions the current term is touching take the term's own colour instead.
Reading the diagram this way turns the formula into a visible process rather than an identity to trust. At any moment you can see which parts of the union are already right and which are still miscounted, and the next term is always aimed at the amber ones.
The small grey number never changes as you step, because the elements do not change — only the bookkeeping does. When a region's element count would be negative the tool prints a cross instead, which is a statement about your input rather than about the step you are on.
Two Sets and Three Sets
The 2 sets and 3 sets buttons switch between two versions of the same idea.
With two sets the formula is short enough to hold in your head:
∣A∪B∣=∣A∣+∣B∣−∣A∩B∣
Three terms, one correction, and the whole argument is that the lens in the middle got counted twice and needs removing once.
With three sets the count jumps to seven terms, and a new phenomenon appears that two sets cannot show: the triple overlap is corrected too many times by the pairwise subtractions and has to be added back. That is the smallest case where the alternating signs stop looking arbitrary.
Start on two sets if the pattern is unfamiliar, then switch. The two-set walk-through takes three presses and the three-set one takes seven, and the second makes sense fastest immediately after the first.
Switching sizes is worth doing as well. The shape of the argument does not depend on the numbers, so any sizes that are actually possible will produce the same sequence of over-count and repair — only the running total differs.
What the Principle Says
The inclusion-exclusion principle computes the size of a union from the sizes of the sets and their intersections.
For two sets:
∣A∪B∣=∣A∣+∣B∣−∣A∩B∣
For three:
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣
The pattern continues for any number of sets: add the individual sizes, subtract the pairwise intersections, add the triples, subtract the quadruples, alternating by the number of sets in each term.
Simply adding ∣A∣+∣B∣ over-counts whatever lies in both sets, and the corrections exist to undo that. The principle is a bookkeeping statement rather than a deep theorem, but the bookkeeping is delicate enough that the signs are worth deriving rather than memorising.
It is also worth noting what the formula does not require. The sets need not be finite in any structured way, need not be nested, and need not overlap at all — the statement holds in every configuration, including the degenerate ones. For the general statement, see cardinality.
Why the Signs Alternate
The alternating signs are not a convention. They are forced, and the reason is visible in the region numbers.
Take an element lying in exactly k of the sets. The single-set terms count it k times, once for each set containing it. The pairwise terms subtract it once for every pair of those sets, which is (2k) times. The triples add it back (3k) times, and so on.
Its total multiplicity is therefore
(1k)−(2k)+(3k)−⋯
and that alternating sum equals exactly 1 for every k≥1. It follows from the binomial theorem applied to (1−1)k=0, rearranged.
So every element in the union is counted once, whatever number of sets it happens to lie in, and elements in no set are never counted at all. Any other choice of signs would break this.
The walk-through is that argument performed on one example rather than in general. Each region holds the elements lying in one particular collection of sets, and its large number is the running value of that alternating sum. For the coefficients, see binomial coefficients.
Where It Gets Used
Counting a union directly is usually harder than counting its parts, which is what makes the principle worth having.
In probability the same statement holds with measures in place of sizes: P(A∪B)=P(A)+P(B)−P(A∩B), and the three-event version has the same seven terms. The addition rule for probability is inclusion-exclusion.
In combinatorics it counts arrangements avoiding several forbidden patterns at once — how many permutations leave no element fixed, how many integers below a bound avoid a list of divisors. The sieve of Eratosthenes and Euler's totient function are both inclusion-exclusion in disguise.
In computing, the same reasoning counts records matching at least one of several filters without materialising the union, which matters when the individual counts are cheap and the union is not.
The practical rule of thumb: whenever a question asks how many things satisfy at least one of several conditions, and the conditions can hold together, this is the formula that applies. If the conditions are mutually exclusive, the corrections all vanish and plain addition is enough.
Common Mistakes
Three errors account for most wrong answers, and each one has a signature in the region numbers.
Stopping after the subtractions. With three sets this leaves the centre at 0, so elements in all three sets are not counted at all. The running total comes out too small by exactly ∣A∩B∩C∣.
Subtracting the triple overlap instead of adding it. The same mistake with a worse ending: the centre lands on −1, which is visibly impossible once you are watching the numbers rather than the algebra.
Entering overlaps larger than the sets they sit inside. An intersection can never exceed either set containing it, and with three sets there is a subtler version — pairwise overlaps that leave no room once the triple overlap is fixed.
All three are easy to catch by the same habit: step through and check that every region lands on 1. The formula is right when the picture is right, and not before.
When the Walk-Through Does Not Resolve
Every term has been applied and at least one region is still not counted exactly once. With valid sizes this cannot happen, so the input is what to check.
All terms applied, still not resolved
The terms are right and the regions still do not land on 1, because the entered sizes were never consistent.
The formula does not fail. What fails is the premise that the entered numbers describe a real collection of sets, and the walk-through is reporting that faithfully rather than hiding it.
This is worth distinguishing from an arithmetic slip. Every term was applied in the right order with the right sign; the multiplicities are correct for the terms; and the regions still do not settle, because the region sizes underlying them were never consistent.
Look for a region flagged with a cross and work outward from it, as at sizes that cannot happen. Reducing an overlap, or increasing the set that contains it, is usually enough.
The general lesson is one worth carrying beyond this tool: a counting formula applied to impossible data returns an answer, and the answer is meaningless. The check that every region lands on 1 is what catches it.
Related Concepts and Tools
Cardinality — sizes of sets, and the general statement of the principle for n sets.
Set Operations — union and intersection, the operations the formula relates.
Venn Diagrams — the two- and three-set pictures the regions come from.
Binomial Coefficients — the (jk) counts that make the alternating sum collapse to one.
Probability Rules — the addition rule, which is this principle stated for measures.
Combinatorics — counting problems where the principle does the heavy lifting.
Venn Diagram Generator — for shading expressions rather than counting them.