Inclusion-Exclusion Calculator
Set Union · PIE Formula · Venn Diagram · Probability
Compute |A∪B∪C∪D| for 2, 3, or 4 sets using the Principle of Inclusion-Exclusion. Full formula substitution, step-by-step calculation, and optional probability output.
Quick Examples
Step-by-Step Solution
Venn Diagram
What Is the Principle of Inclusion-Exclusion?
The Principle of Inclusion-Exclusion (PIE) is a fundamental counting technique in combinatorics and set theory. It provides an exact formula for the size of the union of overlapping finite sets by alternately adding and subtracting the sizes of intersections. The key insight is that when you naively sum the sizes of several sets, elements belonging to multiple sets are counted more than once — inclusion-exclusion corrects this by subtracting over-counted elements and then adding back under-counted ones, ensuring each element is counted exactly once.
For two sets A and B, the formula is simply: |A∪B| = |A| + |B| − |A∩B|. Adding |A| and |B| counts elements in A∩B twice, so we subtract |A∩B| once to compensate. This familiar identity from Venn diagrams generalizes to any number of sets.
The General Formula for n Sets
For n sets A1, A2, …, An, the inclusion-exclusion formula is:
|A1∪…∪An| = Σ|Ai| − Σ|Ai∩Aj| + Σ|Ai∩Aj∩Ak| − … + (−1)n+1|A1∩…∩An|
The sum contains 2n−1 terms (one per non-empty subset of the n sets). The sign is positive for subsets of odd size and negative for subsets of even size. For 2 sets there are 3 terms; for 3 sets, 7 terms; for 4 sets, 15 terms.
The 3-Set Formula
The most commonly tested form is the three-set formula:
|A∪B∪C| = |A|+|B|+|C| − |A∩B| − |A∩C| − |B∩C| + |A∩B∩C|
Here, elements in exactly two sets (e.g., A∩B but not C) are added twice and subtracted once, netting one count. Elements in all three sets are added three times, subtracted three times, and added back once, also netting one count. This confirms the formula counts every element exactly once.
Venn Diagrams and Region Counts
A Venn diagram for n sets has 2n regions (including the exterior), each representing a unique membership pattern. For 2 sets: A-only, B-only, A∩B, and neither. For 3 sets: 7 non-empty interior regions. For 4 sets: up to 15 interior regions (represented by 4 overlapping ellipses since four circles cannot produce all 15 distinct regions). The region counts can be derived from the intersection sizes by inclusion-exclusion applied in reverse.
Inclusion-Exclusion in Probability
In probability theory, the same principle applies to event probabilities: P(A∪B∪C) = P(A)+P(B)+P(C) − P(A∩B) − P(A∩C) − P(B∩C) + P(A∩B∩C). When the universal set size |U| is known, each probability is simply the count divided by |U|. The probability of the complement event (none of the events occurring) is P(Ac∩Bc∩Cc) = 1 − P(A∪B∪C).
Applications of the Inclusion-Exclusion Principle
- Derangements: The number of derangements of n elements is Dn = n! × Σk=0n (−1)k/k!, derived by applying inclusion-exclusion to the sets of permutations that fix at least one element.
- Euler's Totient Function: φ(n) = n × ∏p|n(1 − 1/p), where the product is over prime factors of n, is derived via inclusion-exclusion over sets of multiples of each prime factor.
- Sieve of Eratosthenes: The sieve's counting step is essentially inclusion-exclusion over sets of multiples of small primes.
- Bonferroni Inequalities: Truncating the inclusion-exclusion series gives alternating upper and lower bounds on the union size, useful when exact computation is intractable.
- Chromatic polynomials: Counting proper colorings of graphs uses inclusion-exclusion over edge sets.
- Survey analysis: Computing the number of respondents holding at least one opinion from overlapping survey categories is a direct application.
Worked Example: Divisibility in 1–100
How many integers from 1 to 100 are divisible by 2, 3, or 5? Let A = multiples of 2 (⌊100/2⌋ = 50), B = multiples of 3 (33), C = multiples of 5 (20). Intersections: A∩B = multiples of 6 (16), A∩C = multiples of 10 (10), B∩C = multiples of 15 (6), A∩B∩C = multiples of 30 (3). By inclusion-exclusion: |A∪B∪C| = 50+33+20−16−10−6+3 = 74. So 26 integers from 1 to 100 are not divisible by 2, 3, or 5.
Quick Reference Table
| Sets | Formula Terms | Signs | Total Terms |
|---|---|---|---|
| 2 | |A|, |B|, |A∩B| | +, +, − | 3 |
| 3 | Singles, pairs, triple | +, −, + | 7 |
| 4 | Singles, pairs, triples, quad | +, −, +, − | 15 |
| n | All non-empty subsets | Alternating by size | 2n−1 |