Inclusion–Exclusion Principle Calculator

Find the number of elements in the union of 2 to 6 finite sets. Enter the size of every set and intersection; the calculator applies alternating signs, shows each step, and checks whether the counts can describe real sets.

Enter set cardinalities

Use whole numbers greater than or equal to zero. An intersection means elements common to all named sets, including any that may also belong to additional sets.

Private by design: all counts are validated and calculated on this device. Inputs are not uploaded, saved, or added to the page URL.

Union result

The 3-set example is calculated below.

Advertisement

Inclusion–exclusion formula

For finite sets A₁, A₂, …, Aₙ, the size of their union is:

|A₁ ∪ ··· ∪ Aₙ| = ∑|Aᵢ| − ∑|Aᵢ ∩ Aⱼ| + ∑|Aᵢ ∩ Aⱼ ∩ Aₖ| − ··· + (−1)ⁿ⁺¹|A₁ ∩ ··· ∩ Aₙ|

Two sets

|A ∪ B| = |A| + |B| − |A ∩ B|

The overlap is included in both single-set counts, so subtract it once.

Three sets

|A ∪ B ∪ C| = |A| + |B| + |C| − |A∩B| − |A∩C| − |B∩C| + |A∩B∩C|

Subtracting all three pairs removes the triple overlap too many times, so add it back once.

In general, intersections of an odd number of sets are added and intersections of an even number are subtracted. This alternating correction makes every element in the union contribute exactly once.

Worked inclusion–exclusion example

Suppose |A| = 60, |B| = 45, |C| = 30, the pairwise intersections have sizes 20, 15, and 10, and |A ∩ B ∩ C| = 5.

|A ∪ B ∪ C| = 60 + 45 + 30 − 20 − 15 − 10 + 5 = 95

The single-set sum is 135. Pairwise correction reduces it to 90, while adding the triple intersection back gives a final union size of 95. The inputs imply nonnegative counts in all seven exact Venn regions, so they are internally consistent.

Method, assumptions, and limits

The calculator generates all 2ⁿ − 1 nonempty intersections for the selected number of sets. It groups equal-order terms, adds odd-order totals, subtracts even-order totals, and uses exact integer arithmetic. With six sets, this means 63 input terms.

Every input is interpreted as a cardinality of finite sets, so only nonnegative integers are accepted. Values may contain up to 100 digits. Decimal values, percentages, estimates, and infinity are outside the calculator’s scope.

To test feasibility, the calculator derives each exact Venn-region count by Möbius inversion: for a region, it alternately subtracts and adds every stricter intersection that contains it. If any exact region is negative, the entered counts cannot all be true at once. Passing this check proves that the supplied intersection data can be represented inside the union; it does not determine how many elements lie outside every set.

The formula and alternating-sign convention follow the finite-set treatment in MIT Mathematics, Principle of Inclusion and Exclusion.

Editorial review: Formula signs, exact-region validation, and representative outputs checked by the Starlight Tools editorial team. Last reviewed: .

Inclusion–exclusion calculator FAQ

What is the inclusion–exclusion principle?

It counts elements in a union without double-counting overlaps: add individual set sizes, subtract pairwise intersections, add triple intersections, and continue with alternating signs.

What is the formula for two sets?

|A ∪ B| = |A| + |B| − |A ∩ B|. If A and B are disjoint, their intersection is 0 and their sizes simply add.

What is the formula for three sets?

Add the three single-set sizes, subtract the three pairwise intersections, then add the triple intersection: |A∪B∪C| = |A|+|B|+|C|−|A∩B|−|A∩C|−|B∩C|+|A∩B∩C|.

Why are the signs alternating?

An element belonging to several sets is overcounted by the single-set sum. Successive intersection orders correct earlier over- and under-counting until that element contributes exactly once.

Do I need to enter every intersection?

Yes for an exact general result. A three-set calculation needs three singles, three pairs, and one triple. A six-set calculation needs all 63 nonempty intersections.

Can I enter zero?

Yes. Zero represents an empty set or intersection. If a pairwise intersection is zero, every higher-order intersection containing that pair must also be zero.

Why does the calculator say my counts are inconsistent?

The counts imply at least one negative exact Venn region. For example, an intersection cannot be larger than a set containing it, and overlapping subregions must fit within their broader intersections.

Can this calculate a probability union?

The alternating formula also has a probability form, but this tool accepts only whole-number finite-set cardinalities. For probabilities, use the same event intersections with probability values and keep all units consistent.

Does the calculator store my data?

No. The calculation, consistency check, copy action, and CSV creation all run locally in your browser.

Explore more tools