Inclusion-Exclusion Principle — Add Singles, Subtract Pairs, Add Triples

Count the union of overlapping sets without double counting: |A∪B∪C| = Σ|A| − Σ|A∩B| + |A∩B∩C|. The left panel lights each Venn term as it is added (green) or subtracted (red). The right panel runs the same alternating sum as a bitmask subset enumeration to count integers in [1, N] divisible by at least one of your numbers, with intersections sized by floor(N / lcm).

step 0

Venn regions — alternating add / subtract

A B C
Press Step to begin building the union formula.
term added (+) term subtracted (−) current term
current subset
|subset| → sign
running union0

Bitmask subsets — divisible by at least one

answer so far: 0 integers in [1, 100] divisible by ≥1 of {6,10,15}
masksubset|S|lcmfloor(N/lcm)signrunning
This visualization runs PIE two ways at once. Both reach the same alternating sum: odd-size subsets add, even-size subtract (union form).

Complexity

OperationCostNote
Full PIE over n setsO(2ⁿ · n)2ⁿ subsets, n ops each
With subset-tree reuseO(2ⁿ)one incremental lcm/AND per node
DFS-pruned (lcm > N)≤ 2ⁿ, data-dependentzero terms have zero supersets
Same-size collapseO(n)derangements / surjections only
Brute-force oracleO(N · n)scan [1,N], test each
Independent of N?yescost depends on n, not N
cheap / sub-exponential exponential in n

Operation log

How to read this animation

Self-contained visualization (no external resources). Both panels compute the identical alternating sum Σ∅≠S (−1)|S|+1 |⋂ Aᵢ|. The sign is driven solely by popcount(mask). See middle.md for the coprime/divisible-by derivations, senior.md for pruning and scale, and professional.md for the proofs and the Möbius generalization.