Monocurl Essays
Series Download

Sets, Venn diagrams and inclusion-exclusion

Union, intersection and complement defined exactly, De Morgan's laws for sets, and how to count a union of overlapping sets without counting anything twice.

21 min·High school and early college·

AssumesTruth tables: and, or, not as calculationQuantifiers: for all, there exists, and negation

A class of 30 in which 18 play football and 15 play tennis. The two totals do not fix the overlap: it can be anything from 3, with nobody left outside, to 15, with every tennis player inside the football circle. The picture settles on the case where 4 students play neither.
Monocurl source
mesh box = ClassBox
mesh venn = StudentVenn(k: 3, [1, 1, 1, 1])

slide "overlap"
    play Wait(0.9)
    venn.k = 15
    play Lerp(3.2, [&venn])
    play Wait(0.9)
    venn.k = 7
    play Lerp(2.4, [&venn])
    play Wait(2.4)

Uses the shared definitions for this lesson's figures.

A class has 30 students. Eighteen of them play football and fifteen play tennis. How many play both?

The numbers look like enough to work with. They are not, and the animation shows why: the same three totals fit a class where 3 students play both, a class where all 15 tennis players also play football, and everything in between. We can say that somebody plays both, because $18 + 15 = 33$ is more than the 30 students there are. To get further we need one more fact, and we need a way of reasoning about overlapping groups that keeps track of who has been counted.

Pause and try

Why can the number who play both not be less than 3, and why not more than 15? For each extreme, how many students play neither sport?

Show answerHide answer

If $b$ students play both, then $18 - b$ play only football and $15 - b$ play only tennis, so $(18 - b) + b + (15 - b) = 33 - b$ students play at least one. That cannot exceed 30, so $b \ge 3$; at $b = 3$ all 30 play something and nobody plays neither. Those who play both are among the 15 tennis players, so $b \le 15$; at $b = 15$ only 18 students play anything and 12 play neither.

Sets and membership

The language for this is the language of sets, and it is built directly on the logic of the earlier lessons. From truth tables we use the connectives $\land$ (and), $\lor$ (or, in the inclusive sense) and $\lnot$ (not), and De Morgan’s laws, $\lnot(P \lor Q) \equiv \lnot P \land \lnot Q$ and $\lnot(P \land Q) \equiv \lnot P \lor \lnot Q$. From quantifiers we use $\forall$, “for all”.

A set can be given by listing, as in $\{2, 4, 6\}$, or by a condition on the elements of a set we already have, as in $\{n \in \mathbb{Z} : n \text{ is even}\}$, read “the set of integers $n$ such that $n$ is even”. The empty set is a subset of every set, because “every element of $\varnothing$ is in $B$” is a universal statement with no counterexample.

For a small example that we can hold completely in view, let the universe, the set of all objects under discussion, be

$$ U = \{1, 2, \dots, 12\}, $$

and inside it let $A$ be the multiples of 2 and $B$ the multiples of 3. So $A = \{2, 4, 6, 8, 10, 12\}$ and $B = \{3, 6, 9, 12\}$, with $|A| = 6$ and $|B| = 4$.

Union, intersection and complement

Draw $U$ as a box and each set as a circle, and put every number where it belongs.

  1. Every number from 1 to 12 sits in one of four regions, according to whether it is a multiple of 2 and whether it is a multiple of 3.
  2. The intersection of A and B is the overlap: the numbers in both sets, which are 6 and 12.
  3. The union of A and B is everything inside at least one circle.
  4. The complement of A is everything in the universe that is outside A.
  5. The difference A minus B is the part of A that lies outside B.
The numbers 1 to 12 sorted into a Venn diagram, with $A$ the multiples of 2 and $B$ the multiples of 3. The shaded region shows in turn the intersection, the union, the complement of $A$ and the difference $A \setminus B$.
Monocurl source
let Words = |tex, note, cnt| [
    center{[2.45, 0.85, 0]} Math(tex, 1.05, LEAF),
    center{[2.45, 0.15, 0]} Note(note, 0.78),
    center{[2.45, -0.65, 0]} Math(str_join([to_string(cnt), "\text{ numbers}"]), 0.85, INK)
]
let cnt = |f| len(filter(UNIV, f))
mesh venn = NumVenn(ff: PAPER, po: PAPER, qo: PAPER, both: PAPER)
mesh tokens = Tokens
mesh words = []

slide "Every number from 1 to 12 sits in one of four regions, according to whether it is a multiple of 2 and whether it is a multiple of 3."
    words = [
        center{[2.45, 0.6, 0]} Math("|A| = 6", 0.9, AZURE),
        center{[2.45, -0.1, 0]} Math("|B| = 4", 0.9, EMBER)
    ]
    play Fade(0.6)

slide "The intersection of A and B is the overlap: the numbers in both sets, which are 6 and 12."
    words = []
    play Fade(0.3, [&words])
    venn.both = ON
    play Lerp(0.7, [&venn])
    words = Words("A \cap B", "in A and in B", cnt(|n| inA(n) and inB(n)))
    play Fade(0.4, [&words])

slide "The union of A and B is everything inside at least one circle."
    words = []
    play Fade(0.3, [&words])
    venn.po = ON
    venn.qo = ON
    play Lerp(0.7, [&venn])
    words = Words("A \cup B", "in A or in B", cnt(|n| inA(n) or inB(n)))
    play Fade(0.4, [&words])

slide "The complement of A is everything in the universe that is outside A."
    words = []
    play Fade(0.3, [&words])
    venn.po = PAPER
    venn.both = PAPER
    venn.ff = ON
    play Lerp(0.7, [&venn])
    words = Words("A^c", "not in A", cnt(|n| not inA(n)))
    play Fade(0.4, [&words])

slide "The difference A minus B is the part of A that lies outside B."
    words = []
    play Fade(0.3, [&words])
    venn.ff = PAPER
    venn.qo = PAPER
    venn.po = ON
    play Lerp(0.7, [&venn])
    words = Words("A \setminus B", "in A, not in B", cnt(|n| inA(n) and not inB(n)))
    play Fade(0.4, [&words])

Uses the shared definitions for this lesson's figures.

Each region was described with one of the words “and”, “or”, “not”. That is the definition.

In the example, $A \cap B = \{6, 12\}$, the multiples of 6. The union is $A \cup B = \{2, 3, 4, 6, 8, 9, 10, 12\}$; the “or” is inclusive, so 6 and 12 belong to it, and each is listed once. The complement $A^c = \{1, 3, 5, 7, 9, 11\}$ depends on the universe: in a universe of 1 to 20 it would be a different set, which is why $U$ must be stated before a complement means anything.

So a set operation is a logical connective applied to membership statements, and every law about $\land$, $\lor$ and $\lnot$ becomes a law about $\cap$, $\cup$ and complement. To prove that two sets are equal, show that $x$ belongs to one exactly when it belongs to the other.

De Morgan’s laws for sets

Pause and try

In the example, list $(A \cup B)^c$. Then list $A^c$ and $B^c$ separately and find $A^c \cap B^c$. What do you notice?

Show answerHide answer

$(A \cup B)^c$ is what is left of $U$ after removing $\{2, 3, 4, 6, 8, 9, 10, 12\}$, namely $\{1, 5, 7, 11\}$. Separately, $A^c = \{1, 3, 5, 7, 9, 11\}$ and $B^c = \{1, 2, 4, 5, 7, 8, 10, 11\}$, and the numbers in both lists are $1, 5, 7, 11$. The two answers are the same set.

The agreement can be watched number by number.

A membership table for the numbers 1 to 12. A filled square means the number belongs to the set named on the left. The row for $(A \cup B)^c$ and the row for $A^c \cap B^c$ are filled in exactly the same columns.
Monocurl source
let cx = |i| -1.7 + i * 0.46
let RY = [1.05, 0.4, -0.5, -1.15]
let Mark = |pos, v, col| center{pos} fill{[PAPER, col][v]} stroke{[MIST, col][v], 2.5} Rect([0.34, 0.34])
let vals = |n| [
    inA(n),
    inB(n),
    1 - max(inA(n), inB(n)),
    (1 - inA(n)) * (1 - inB(n))
]
let COLS = [AZURE, EMBER, PLUM, PLUM]
let Column = |i| map(range(0, 4), |r| Mark([cx(i), RY[r], 0], vals(UNIV[i])[r], COLS[r]))
mesh head = [
    map(range(0, 12), |i| center{[cx(i), 1.7, 0]} Math(to_string(UNIV[i]), 0.78, INK)),
    center{[-2.85, RY[0], 0]} Math("A", 0.95, AZURE),
    center{[-2.85, RY[1], 0]} Math("B", 0.95, EMBER),
    center{[-2.85, RY[2], 0]} Math("(A \cup B)^c", 0.88, PLUM),
    center{[-2.85, RY[3], 0]} Math("A^c \cap B^c", 0.88, PLUM)
]
mesh cols = [Column(0), Column(1)]
mesh verdict = []

slide "fill"
    play Wait(0.5)
    for (k in range(3, 13)) {
        cols = map(range(0, k), |i| Column(i))
        play Fade(0.3, [&cols])
        play Wait(0.2)
    }
    verdict = [
        FrameBox(cx(0) - 0.32, cx(11) + 0.32, RY[3] - 0.3, RY[2] + 0.3, LEAF, 4),
        center{[cx(5.5), -1.85, 0]} Cap(["the rows differ", "the last two rows agree"][to_int(all(map(UNIV, |n| vals(n)[2] == vals(n)[3])))], 0.82, LEAF)
    ]
    play Fade(0.5, [&verdict])
    play Wait(2.2)

Uses the shared definitions for this lesson's figures.

Twelve agreements are evidence about one example. The theorem is about all sets, and its proof is the logical De Morgan law applied to one element at a time.

Counting a union

We return to counting. In the example $|A| = 6$ and $|B| = 4$, but the union has 8 elements, not 10. To see where the other two went, count the way a careless clerk would: go through $A$ and give each element a tick, then go through $B$ and do the same.

Counting $|A| + |B|$ by ticks. Each element of $A$ gets a blue tick and each element of $B$ an orange one. There are 10 ticks on only 8 numbers, because 6 and 12 are ticked twice.
Monocurl source
let TICKS = flatten([map(filter(UNIV, |n| inA(n)), |n| [n, 0]), map(filter(UNIV, |n| inB(n)), |n| [n, 1])])
let Ticks = |k| block {
    for (m in range(0, len(TICKS))) {
        if (k > m + 0.5) {
            let p = place(TICKS[m][0])
            . Pt([p[0] + [-0.13, 0.13][TICKS[m][1]], p[1] + 0.34, 0], [AZURE, EMBER][TICKS[m][1]], 0.09)
        }
    }
    . center{[2.45, 0.75, 0]} scale{1.5} color{INK} Number(floor(k + 0.5), 0)
}
let nA = len(filter(UNIV, |n| inA(n)))
let nB = len(filter(UNIV, |n| inB(n)))
let nAB = len(filter(UNIV, |n| inA(n) and inB(n)))
mesh venn = NumVenn(PAPER, PAPER, PAPER, PAPER)
mesh tokens = [Tokens, center{[2.45, 1.4, 0]} Note("ticks", 0.8)]
mesh ticks = Ticks(k: 0)
mesh twice = []

slide "tick"
    play Wait(0.6)
    ticks.k = nA
    play Lerp(2.4, [&ticks], linear)
    play Wait(0.4)
    ticks.k = nA + nB
    play Lerp(1.8, [&ticks], linear)
    twice = [
        map(filter(UNIV, |n| inA(n) and inB(n)), |n| center{place(n)} fill{alpha{0} PAPER} stroke{ROSE, 4} Circle(0.3, 48)),
        center{[2.45, -0.1, 0]} Cap(str_join([to_string(nAB), " ticked twice"]), 0.82, ROSE),
        center{[2.45, -0.9, 0]} Math(str_join([to_string(nA + nB), " - ", to_string(nAB), " = ", to_string(nA + nB - nAB)]), 0.9, LEAF)
    ]
    play Fade(0.5, [&twice])
    play Wait(2.2)

Uses the shared definitions for this lesson's figures.

Every element of the union received one tick, except the elements of $A \cap B$, which received two. So the number of ticks, $|A| + |B|$, overshoots the size of the union by exactly $|A \cap B|$. Here that is $6 + 4 - 2 = 8$.

To prove this for all finite sets we need one fact about counting to stand on, and it is the simplest one. The addition principle: if finite sets $X$ and $Y$ are disjoint, then $|X \cup Y| = |X| + |Y|$, and likewise for three or more sets no two of which share an element. This is what counting separate piles and adding the results means, and we take it as the starting point.

The last remark is what the opening question needed. We knew $|F| = 18$ and $|T| = 15$ for the sets $F$ of football players and $T$ of tennis players: two of the four numbers, which is one too few. Suppose we now learn that 4 students play neither sport.

  1. Thirty students, 18 who play football, 15 who play tennis, and one new fact: 4 play neither.
  2. The 4 who play neither are the complement of the union, so the union has 30 − 4 = 26 students.
  3. Adding 18 and 15 gives 33, which counts the overlap twice. So the overlap is 33 − 26 = 7.
  4. Then 18 − 7 = 11 play only football and 15 − 7 = 8 play only tennis. The four regions add up to 30.
The class question solved once a third fact is known. Four students play neither sport, so 26 are in the union, and inclusion-exclusion gives the overlap.
Monocurl source
mesh box = ClassBox
mesh venn = StudentShapes(7)
mesh nums = StudentNums(7, [0, 0, 0, 1])
mesh words = []

slide "Thirty students, 18 who play football, 15 who play tennis, and one new fact: 4 play neither."
    play Fade(0.6)

slide "The 4 who play neither are the complement of the union, so the union has 30 − 4 = 26 students."
    words = [
        center{[2.55, -1.0, 0]} Math("30 - 4 = 26", 0.82, LEAF),
        center{[2.55, -1.45, 0]} Note("in the union", 0.76)
    ]
    play Fade(0.5, [&words])

slide "Adding 18 and 15 gives 33, which counts the overlap twice. So the overlap is 33 − 26 = 7."
    words = []
    play Fade(0.3, [&words])
    nums = []
    play Fade(0.3, [&words, &nums])
    nums = StudentNums(7, [0, 1, 0, 1])
    words = [
        center{[2.55, -1.0, 0]} Math("18 + 15 = 33", 0.82, INK),
        center{[2.55, -1.45, 0]} Math("33 - 26 = 7", 0.82, LEAF)
    ]
    play Fade(0.5)

slide "Then 18 − 7 = 11 play only football and 15 − 7 = 8 play only tennis. The four regions add up to 30."
    words = []
    play Fade(0.3, [&words])
    nums = []
    play Fade(0.3, [&words, &nums])
    nums = StudentNums(7, [1, 1, 1, 1])
    words = [
        center{[2.55, -1.0, 0]} Math("11 + 7 + 8 + 4", 0.82, INK),
        center{[2.55, -1.45, 0]} Math("= 30", 0.82, LEAF)
    ]
    play Fade(0.5)

Uses the shared definitions for this lesson's figures.

In symbols: $|F \cup T| = |U| - |(F \cup T)^c| = 30 - 4 = 26$, and then $26 = 18 + 15 - |F \cap T|$ gives $|F \cap T| = 7$.

Three sets

With three sets $A$, $B$, $C$ the same idea should work: add the three sizes and subtract the three pairwise overlaps.

Pause and try

Suppose you compute $|A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C|$. Consider an element that lies in all three sets. How many times has it been counted, in total? What must be done to repair the formula?

Show answerHide answer

It is counted once in each of $|A|$, $|B|$, $|C|$, for $+3$, and once in each of the three pairwise intersections, for $-3$. The total is 0, so the element has disappeared from the count. Adding $|A \cap B \cap C|$ counts it once more and brings the total to 1.

  1. Three sets cut their union into seven regions. Nothing has been counted yet.
  2. Add the sizes of A, B and C. A region inside two sets is counted twice, and the center three times.
  3. Subtract the three pairwise intersections. The regions in exactly two sets drop to 1, but the center drops to 0.
  4. Add the triple intersection back. Every region is now counted exactly once.
Three overlapping sets cut their union into seven regions. The number in each region is how many times its elements have been counted so far: after adding the three sets, after subtracting the three pairwise intersections, and after adding the triple intersection back.
Monocurl source
let CA = [-1.85, 0.5, 0]
let CB = [-0.55, 0.5, 0]
let CC = [-1.2, -0.6, 0]
let R3 = 1.25
# each region: where its label goes and how many of the three sets contain it
let REG = [
    [[-2.5, 0.85, 0], 1], [[0.1, 0.85, 0], 1], [[-1.2, -1.3, 0], 1],
    [[-1.2, 1.05, 0], 2], [[-1.95, -0.3, 0], 2], [[-0.45, -0.3, 0], 2],
    [[-1.2, 0.1, 0], 3]
]
let tally = |m, stage| [0, m, m - choose(m, 2), m - choose(m, 2) + choose(m, 3)][stage]
let Counts = |stage| map(REG, |r| center{r[0]} Math(to_string(tally(r[1], stage)), 1.05, [[ROSE, LEAF][to_int(tally(r[1], stage) == 1)], SLATE][to_int(stage == 0)]))
let FORM = [
    ["|A| + |B| + |C|", INK],
    ["-\,|A \cap B| - |A \cap C|", ROSE],
    ["-\,|B \cap C|", ROSE],
    ["+\,|A \cap B \cap C|", LEAF]
]
let Formula = |n| map(range(0, n), |i| center{[2.35, 1.0 - i * 0.62, 0]} Math(FORM[i][0], 0.8, FORM[i][1]))
mesh circles = [
    center{CA} fill{alpha{0} PAPER} stroke{AZURE, 4} Circle(R3, 96),
    center{CB} fill{alpha{0} PAPER} stroke{EMBER, 4} Circle(R3, 96),
    center{CC} fill{alpha{0} PAPER} stroke{PLUM, 4} Circle(R3, 96),
    center{[-2.95, 1.6, 0]} Math("A", 0.95, AZURE),
    center{[0.55, 1.6, 0]} Math("B", 0.95, EMBER),
    center{[0.05, -1.65, 0]} Math("C", 0.95, PLUM),
    center{[2.35, 1.7, 0]} Note("times counted so far", 0.78)
]
mesh counts = []
mesh formula = []

slide "Three sets cut their union into seven regions. Nothing has been counted yet."
    counts = Counts(0)
    play Fade(0.6)

slide "Add the sizes of A, B and C. A region inside two sets is counted twice, and the center three times."
    counts = []
    play Fade(0.3, [&counts])
    counts = Counts(1)
    formula = Formula(1)
    play Fade(0.5)

slide "Subtract the three pairwise intersections. The regions in exactly two sets drop to 1, but the center drops to 0."
    counts = []
    play Fade(0.3, [&counts])
    counts = Counts(2)
    formula = Formula(3)
    play Fade(0.5)

slide "Add the triple intersection back. Every region is now counted exactly once."
    counts = []
    play Fade(0.3, [&counts])
    counts = Counts(3)
    formula = Formula(4)
    play Fade(0.5)

Uses the shared definitions for this lesson's figures.

The picture checks seven regions. For a proof, we reduce to the two-set theorem by treating $A \cup B$ as a single set.

Worked examples

The numbers 1 to 100 in rows of ten. The first pass marks the multiples of 2, the second marks the multiples of 3 not already marked, and the third the multiples of 5 not already marked. In all 74 squares are marked and 26 are left.
Monocurl source
let S = 0.37
let pos = |n| [-3.1 + mod(n - 1, 10) * S, 1.665 - ((n - 1) // 10) * S, 0]
let kind = |n, k| {
    if (mod(n, 2) == 0 and n <= k + 0.001) {
        return 1
    }
    if (mod(n, 3) == 0 and n <= k - 100 + 0.001) {
        return 2
    }
    if (mod(n, 5) == 0 and n <= k - 200 + 0.001) {
        return 3
    }
    return 0
}
let FILLS = [PAPER, tint(AZURE, 0.75), tint(EMBER, 0.8), tint(PLUM, 0.75)]
let EDGES = [MIST, AZURE, EMBER, PLUM]
let Sieve = |k| map(range(1, 101), |n| center{pos(n)} fill{FILLS[kind(n, k)]} stroke{EDGES[kind(n, k)], 1.5} Rect([S - 0.07, S - 0.07]))
let newly = |j| len(filter(range(1, 101), |n| kind(n, 300) == j))
let Line = |j, name| [
    center{[1.35, 1.25 - (j - 1) * 0.62, 0]} fill{FILLS[j]} stroke{EDGES[j], 1.5} Rect([0.3, 0.3]),
    center{[2.5, 1.25 - (j - 1) * 0.62, 0]} Cap(str_join([name, ": ", to_string(newly(j))]), 0.86, EDGES[j])
]
mesh ends = [
    center{[-3.48, 1.665, 0]} Math("1", 0.74, SLATE),
    center{[0.68, 1.665 - 9 * S, 0]} Math("100", 0.74, SLATE)
]
mesh sieve = Sieve(k: 0)
mesh tally = []

slide "mark"
    play Wait(0.5)
    sieve.k = 100
    play Lerp(2.2, [&sieve], linear)
    tally = [Line(1, "by 2")]
    play Fade(0.4, [&tally])
    sieve.k = 200
    play Lerp(2.2, [&sieve], linear)
    tally .= Line(2, "new by 3")
    play Fade(0.4, [&tally])
    sieve.k = 300
    play Lerp(2.2, [&sieve], linear)
    tally .= Line(3, "new by 5")
    tally .= center{[2.4, -0.9, 0]} Math(str_join([to_string(newly(1) + newly(2) + newly(3)), "\text{ marked}"]), 0.95, LEAF)
    tally .= center{[2.4, -1.5, 0]} Math(str_join([to_string(newly(0)), "\text{ left}"]), 0.9, SLATE)
    play Fade(0.5, [&tally])
    play Wait(2.4)

Uses the shared definitions for this lesson's figures.

Dividing a count by $|U|$ turns it into a proportion, so the same formula governs probabilities of equally likely outcomes: $P(A \cup B) = P(A) + P(B) - P(A \cap B)$.

The general formula

The pattern continues: add the single sets, subtract the pairs, add the triples, subtract the quadruples, and so on with alternating signs.

We state this without a full proof; here is a sketch of why it is true. Take an element that lies in exactly $m$ of the sets, with $m \ge 1$. It is counted in $\binom{m}{1}$ of the single terms, $\binom{m}{2}$ of the pair terms, and so on, where $\binom{m}{j}$ is the number of ways to choose $j$ things from $m$. Its total count is $\binom{m}{1} - \binom{m}{2} + \binom{m}{3} - \dots$, and this alternating sum always equals 1. For $m = 3$ it is $3 - 3 + 1$, as in the figure, and for $m = 4$ it is $4 - 6 + 4 - 1 = 1$. In general it follows from the binomial theorem applied to $(1 - 1)^m = 0$. A complete proof can also be given by induction on $n$, repeating the step that took us from two sets to three.

Why there is no set of all sets

Every set so far was carved out of a universe fixed in advance, and the complement was defined relative to it. One might hope for a single universe containing everything, including all sets. In 1901 Bertrand Russell showed that this cannot be had, by an argument that is a proof by contradiction in three lines.

Suppose every condition defined a set. Most sets are not elements of themselves: the set of even numbers is not an even number. So consider the condition “$x$ is a set and $x \notin x$”, and let $R$ be the set it defines. Is $R \in R$? If it is, then $R$ fails the condition for membership in $R$, so $R \notin R$. If it is not, then $R$ meets the condition, so $R \in R$. Either way $R \in R$ holds exactly when it fails, which is a contradiction. So $R$ does not exist, and “every condition defines a set” is false.

This is Russell’s paradox. The repair adopted in modern set theory is the one we have been using all along: a condition may be used to select elements from a set already in hand, as in $\{x \in U : \dots\}$, but not from “everything”. Under that rule there can be no set of all sets, because Russell’s condition would select from it a set that leads to the same contradiction (Problem 8).

Common mistakes and edge cases

Three special cases are worth checking against the formula. If $A$ and $B$ are disjoint, then $|A \cap B| = 0$ and it reduces to the addition principle. If $A \subseteq B$, then $A \cap B = A$ and it gives $|A \cup B| = |B|$, as it should. And the formula is about finite sets only: for infinite sets “size” needs a new definition, which is the subject of the next lesson.

Stepping back

The opening question could not be answered because the formula for a union links four numbers and we had only two of them, the class total being a fact about the universe and not about the union. The work was to say exactly what “both”, “either” and “neither” mean, which turned out to be “and”, “or” and “not” applied to membership, and then to count carefully: adding sizes ticks the overlap twice, so take it off once. With three sets the correction itself overcorrects, and the signs alternate from there. The habit to keep is to ask, of any total, how many times each object has been counted.

Check yourself

In a club, 12 members play piano, 9 play guitar and 4 play both. How many play at least one of the two instruments?

Problems

Problems 1 to 3 repeat the moves of the lesson, 4 to 6 combine them, and 7 and 8 go further.

Problem 1

Let $U = \{1, 2, \dots, 10\}$, $A = \{1, 2, 3, 4, 5\}$ and $B = \{4, 5, 6, 7\}$. Find $A \cup B$, $A \cap B$, $A^c$, $A \setminus B$ and $(A \cup B)^c$. Verify De Morgan’s first law and the two-set counting formula on this example.

Show solutionHide solution

$A \cup B = \{1, 2, 3, 4, 5, 6, 7\}$, $A \cap B = \{4, 5\}$, $A^c = \{6, 7, 8, 9, 10\}$, $A \setminus B = \{1, 2, 3\}$ and $(A \cup B)^c = \{8, 9, 10\}$. For De Morgan: $B^c = \{1, 2, 3, 8, 9, 10\}$, and the elements common to $A^c$ and $B^c$ are $8, 9, 10$, so $A^c \cap B^c = \{8, 9, 10\} = (A \cup B)^c$. For counting: $|A| + |B| - |A \cap B| = 5 + 4 - 2 = 7 = |A \cup B|$.

Problem 2

In a class of 40 students, 25 have a dog, 18 have a cat and 6 have neither. How many have both? How many have a dog but no cat?

Show solutionHide solution

The union has $40 - 6 = 34$ students. From $34 = 25 + 18 - |D \cap C|$ we get $|D \cap C| = 9$. Dog but no cat is $|D \setminus C| = 25 - 9 = 16$. Check: the regions are 16 (dog only), 9 (both), $18 - 9 = 9$ (cat only) and 6 (neither), and $16 + 9 + 9 + 6 = 40$.

Problem 3

How many of the integers from 1 to 200 are divisible by 4 or by 6?

Show solutionHide solution

There are $\lfloor 200/4 \rfloor = 50$ multiples of 4 and $\lfloor 200/6 \rfloor = 33$ multiples of 6. A number is divisible by both exactly when it is divisible by their least common multiple, which is 12 (not $4 \times 6 = 24$: the number 12 is divisible by both). There are $\lfloor 200/12 \rfloor = 16$ of those. So the answer is $50 + 33 - 16 = 67$.

Problem 4

Let $A$ and $B$ be subsets of a universe $U$. Prove that $A \subseteq B$ if and only if $B^c \subseteq A^c$. Which law of logic is this?

Show solutionHide solution

By definition, $A \subseteq B$ means $\forall x \in U\,(x \in A \to x \in B)$, and $B^c \subseteq A^c$ means $\forall x \in U\,(x \notin B \to x \notin A)$. For each $x$, the conditional $x \notin B \to x \notin A$ is the contrapositive of $x \in A \to x \in B$, and a conditional is equivalent to its contrapositive. So for each $x$ the two conditionals have the same truth value, and one universal statement holds exactly when the other does. The law is the equivalence of a conditional with its contrapositive. In the picture: if the circle $A$ lies inside the circle $B$, then everything outside $B$ is outside $A$.

Problem 5

How many integers from 1 to 1000 are divisible by none of 3, 5 and 7?

Show solutionHide solution

Let $A$, $B$, $C$ be the multiples of 3, 5, 7 up to 1000. Then $|A| = \lfloor 1000/3 \rfloor = 333$, $|B| = 200$, $|C| = \lfloor 1000/7 \rfloor = 142$. The pairwise intersections are the multiples of 15, 21 and 35: $\lfloor 1000/15 \rfloor = 66$, $\lfloor 1000/21 \rfloor = 47$, $\lfloor 1000/35 \rfloor = 28$. The triple intersection is the multiples of 105: $\lfloor 1000/105 \rfloor = 9$. So

$$ |A \cup B \cup C| = 333 + 200 + 142 - 66 - 47 - 28 + 9 = 543. $$

By De Morgan the numbers divisible by none are the complement of the union, and there are $1000 - 543 = 457$ of them. As a rough check, the fraction of integers divisible by none of 3, 5, 7 should be about $\frac23 \cdot \frac45 \cdot \frac67 = \frac{48}{105} \approx 0.457$.

Problem 6

Let $A$ and $B$ be subsets of a finite universe $U$. Prove that

$$ |A| + |B| - |U| \;\le\; |A \cap B| \;\le\; \min(|A|, |B|). $$

Deduce that if 70 of 100 people speak English and 60 speak Spanish, then at least 30 speak both. Can exactly 30 happen?

Show solutionHide solution

First, if $X \subseteq Y$ are finite then $|X| \le |Y|$, because $Y$ is the disjoint union of $X$ and $Y \setminus X$, so $|Y| = |X| + |Y \setminus X|$. Upper bound. $A \cap B \subseteq A$ and $A \cap B \subseteq B$, so $|A \cap B|$ is at most both $|A|$ and $|B|$. Lower bound. $A \cup B \subseteq U$, so $|A \cup B| \le |U|$. By the two-set formula $|A \cup B| = |A| + |B| - |A \cap B|$, so $|A| + |B| - |A \cap B| \le |U|$, which rearranges to the claim. Application. $|E \cap S| \ge 70 + 60 - 100 = 30$. Exactly 30 happens when the union is everyone: 40 speak only English, 30 both, 30 only Spanish, nobody neither.

Problem 7

Four letters are put into four addressed envelopes, one letter in each. In how many of the $4! = 24$ arrangements does no letter go into its own envelope? Use the general formula with $A_i$ the set of arrangements in which letter $i$ is in the right envelope. (Such arrangements are called derangements.)

Show solutionHide solution

We count the union $A_1 \cup A_2 \cup A_3 \cup A_4$, the arrangements with at least one letter correct, and subtract from 24. If letter $i$ is fixed in place, the other three can be arranged in $3! = 6$ ways, so $|A_i| = 6$, and there are 4 such sets. If two given letters are fixed, the other two can be arranged in $2! = 2$ ways, and there are $\binom42 = 6$ pairs. If three given letters are fixed, the fourth is forced: 1 way, for each of the $\binom43 = 4$ triples. All four fixed: 1 way. So

$$ |A_1 \cup A_2 \cup A_3 \cup A_4| = 4 \cdot 6 - 6 \cdot 2 + 4 \cdot 1 - 1 = 15, $$

and the number of derangements is $24 - 15 = 9$. As a check, writing each arrangement as the list of envelopes receiving letters 1 to 4, the nine are 2143, 2341, 2413, 3142, 3412, 3421, 4123, 4312, 4321.

Problem 8

Let $A$ be any set, and define $R_A = \{x \in A : x \notin x\}$. Prove that $R_A \notin A$. Deduce that no set contains every set as an element.

Show solutionHide solution

Suppose, for contradiction, that $R_A \in A$. By the definition of $R_A$, an object $x$ belongs to $R_A$ exactly when $x \in A$ and $x \notin x$. Apply this to $x = R_A$: since $R_A \in A$ by assumption, we get that $R_A \in R_A$ holds exactly when $R_A \notin R_A$ holds. A statement cannot have the same truth value as its negation, so this is a contradiction, and $R_A \notin A$. Now if some set $V$ contained every set as an element, it would contain the set $R_V$, contradicting what was just proved with $A = V$. So there is no set of all sets. Notice the difference from Russell’s original paradox: here $R_A$ exists without any difficulty, because it is selected from the set $A$, and the argument shows only that it lies outside $A$.

Shared definitions used by this lesson's figures
# house kit: prepended to every essay figure after the std imports

# palette. AZURE and EMBER are the two actors, LEAF marks an answer, ROSE a problem
let PAPER = [1, 1, 1, 1]
let INK = [0.08, 0.09, 0.11, 1]
let SLATE = [0.40, 0.44, 0.50, 1]
let MIST = [0.85, 0.86, 0.88, 1]
let AZURE = [0.13, 0.42, 0.68, 1]
let EMBER = [0.89, 0.42, 0.12, 1]
let LEAF = [0.13, 0.55, 0.36, 1]
let ROSE = [0.78, 0.22, 0.24, 1]
let PLUM = [0.47, 0.30, 0.66, 1]
let GOLD = [0.85, 0.62, 0.08, 1]

background = PAPER

# an opaque pastel of col; use for fills so overlaps do not darken
let tint = |col, amount = 0.18| [
    1 - (1 - col[0]) * amount,
    1 - (1 - col[1]) * amount,
    1 - (1 - col[2]) * amount,
    1
]

# text: Cap for labels, Note for quiet labels, Math for typeset maths
let Cap = |txt, size = 0.75, col = INK| color{col} Text(txt, size, "Charter")
let Note = |txt, size = 0.65| color{SLATE} Text(txt, size, "Charter")
let Math = |tex, size = 0.9, col = INK| color{col} Tex(tex, size)

# a paper rectangle to sit behind text that crosses lines
let Plate = |pos, size| center{pos} fill{PAPER} stroke{alpha{0} PAPER, 0} Rect(size)

# shapes with fill and stroke both set, so no stray black appears
let Pt = |pos, col = INK, r = 0.075| center{pos} fill{col} stroke{col, 1} Circle(r, 48)
let Ring = |pos, col = INK, r = 0.1, w = 3| center{pos} fill{PAPER} stroke{col, w} Circle(r, 48)
let Seg = |a, b, col = INK, w = 3| stroke{col, w} Line(a, b)
let Path = |pts, col = INK, w = 3| stroke{col, w} Polyline(pts)
let Region = |pts, col| fill{col} stroke{alpha{0} col, 0} Polygon(pts)
let Outline = |pts, col = INK, w = 3| fill{alpha{0} PAPER} stroke{col, w} Polygon(pts)
let Shape = |pts, col, amount = 0.18, w = 3| fill{tint(col, amount)} stroke{col, w} Polygon(pts)
let Disc = |pos, r, col, amount = 0.18, w = 3| center{pos} fill{tint(col, amount)} stroke{col, w} Circle(r, 96)
let Box = |pos, size, col, amount = 0.18, w = 3| center{pos} fill{tint(col, amount)} stroke{col, w} Rect(size)
# a line from a to b ending in a small solid arrowhead
let Tip = |a, b, col = INK, w = 3, head = 0.17| {
    let d = normalize([b[0] - a[0], b[1] - a[1], 0])
    let n = [-d[1], d[0], 0]
    let base = [b[0] - d[0] * head, b[1] - d[1] * head, 0]
    return [
        stroke{col, w} Line(a, base),
        fill{col} stroke{col, 1} Polygon([
            b,
            [base[0] + n[0] * head * 0.42, base[1] + n[1] * head * 0.42, 0],
            [base[0] - n[0] * head * 0.42, base[1] - n[1] * head * 0.42, 0]
        ])
    ]
}

# plots. a frame is [x0, x1, y0, y1, centre, width, height]: a data window and the scene box it fills
let Frame = |x0, x1, y0, y1, c = [0, 0, 0], w = 6.4, h = 3.6| [x0, x1, y0, y1, c, w, h]
let fx = |fr, x| fr[4][0] + (x - (fr[0] + fr[1]) / 2) * fr[5] / (fr[1] - fr[0])
let fy = |fr, y| fr[4][1] + (y - (fr[2] + fr[3]) / 2) * fr[6] / (fr[3] - fr[2])
let at = |fr, x, y| [fx(fr, x), fy(fr, y), 0]

# axes through the data origin (or the nearest edge of the window), with optional names
let Axes = |fr, x_name = "", y_name = "", col = SLATE| {
    let ox = clamp(fr[0], 0, fr[1])
    let oy = clamp(fr[2], 0, fr[3])
    var parts = [
        Tip(at(fr, fr[0], oy), [fx(fr, fr[1]) + 0.25, fy(fr, oy), 0], col, 2.5, 0.15),
        Tip(at(fr, ox, fr[2]), [fx(fr, ox), fy(fr, fr[3]) + 0.25, 0], col, 2.5, 0.15)
    ]
    if (x_name != "") {
        parts .= center{[fx(fr, fr[1]) + 0.5, fy(fr, oy), 0]} Math(x_name, 0.8, col)
    }
    if (y_name != "") {
        parts .= center{[fx(fr, ox), fy(fr, fr[3]) + 0.5, 0]} Math(y_name, 0.8, col)
    }
    return parts
}

# tick marks with numbers at the listed data values
let XTicks = |fr, xs, col = SLATE, places = 0| block {
    let oy = fy(fr, clamp(fr[2], 0, fr[3]))
    for (x in xs) {
        . Seg([fx(fr, x), oy - 0.07, 0], [fx(fr, x), oy + 0.07, 0], col, 2)
        . center{[fx(fr, x), oy - 0.33, 0]} Math(to_string(x, places), 0.72, col)
    }
}
let YTicks = |fr, ys, col = SLATE, places = 0| block {
    let ox = fx(fr, clamp(fr[0], 0, fr[1]))
    for (y in ys) {
        . Seg([ox - 0.07, fy(fr, y), 0], [ox + 0.07, fy(fr, y), 0], col, 2)
        . center{[ox - 0.36, fy(fr, y), 0]} Math(to_string(y, places), 0.72, col)
    }
}

# faint grid lines at the listed data values
let GridLines = |fr, xs, ys, col = MIST| block {
    for (x in xs) {
        . Seg(at(fr, x, fr[2]), at(fr, x, fr[3]), col, 1.5)
    }
    for (y in ys) {
        . Seg(at(fr, fr[0], y), at(fr, fr[1], y), col, 1.5)
    }
}

# the graph of f over [a, b], drawn in the frame. values are clamped to the window
let Graph = |fr, f, a, b, col = AZURE, w = 4, n = 160|
    stroke{col, w} Polyline(map(sample(a, b, n), |x| at(fr, x, clamp(fr[2], f(x), fr[3]))))

# the region between the graph of f and the x axis over [a, b]
let AreaUnder = |fr, f, a, b, col = AZURE, amount = 0.2, n = 80| {
    var pts = [at(fr, a, 0)]
    for (x in sample(a, b, n)) {
        pts .= at(fr, x, clamp(fr[2], f(x), fr[3]))
    }
    pts .= at(fr, b, 0)
    return fill{tint(col, amount)} stroke{alpha{0} col, 0} Polygon(pts)
}

let FrameBox = |x0, x1, y0, y1, col, w = 4.5| Outline([[x0, y0, 0], [x1, y0, 0], [x1, y1, 0], [x0, y1, 0]], col, w)
let arcpts = |c, r, a0, a1, n = 30| map(sample(a0, a1, n), |t| [c[0] + r * cos(t), c[1] + r * sin(t), 0])

# the class: 30 students, 18 play football, 15 play tennis, k play both
let RF = 1.3
let RT = 0.95
let CXS = -0.6
let CYS = -0.25
let gap = |k| lerp(1.85, 0.35, (k - 3) / 12)
let StudentShapes = |k| {
    let d = gap(k)
    let cf = [CXS - d / 2, CYS, 0]
    let ct = [CXS + d / 2, CYS, 0]
    let x0 = (d * d + RF * RF - RT * RT) / (2 * d)
    let kk = to_int(round(k))
    var parts = [
        center{cf} fill{tint(AZURE, 0.3)} stroke{AZURE, 1} Circle(RF, 96),
        center{ct} fill{tint(EMBER, 0.3)} stroke{EMBER, 1} Circle(RT, 96)
    ]
    if (d <= RF - RT + 0.02) {
        parts .= center{ct} fill{tint(PLUM, 0.4)} stroke{PLUM, 1} Circle(RT, 96)
    } else {
        let h = sqrt(max(0, RF * RF - x0 * x0))
        let a = arctan2(h, x0)
        let b = arctan2(h, d - x0)
        parts .= Region(flatten([arcpts(cf, RF, 0 - a, a), arcpts(ct, RT, PI - b + 0.02, PI + b - 0.02)]), tint(PLUM, 0.4))
    }
    parts .= center{cf} fill{alpha{0} PAPER} stroke{AZURE, 4} Circle(RF, 96)
    parts .= center{ct} fill{alpha{0} PAPER} stroke{EMBER, 4} Circle(RT, 96)
    return parts
}
let StudentNums = |k, show| {
    let d = gap(k)
    let cf = [CXS - d / 2, CYS, 0]
    let ct = [CXS + d / 2, CYS, 0]
    let kk = to_int(round(k))
    var parts = []
    let lf = cf[0] - RF
    let lt = ct[0] - RT
    let rf = cf[0] + RF
    let rt = ct[0] + RT
    if (show[0]) {
        parts .= center{[(lf + lt) / 2, CYS, 0]} scale{1.3} color{INK} Number(18 - kk, 0)
    }
    if (show[1]) {
        parts .= center{[(lt + rf) / 2, CYS, 0]} scale{1.3} color{INK} Number(kk, 0)
    }
    if (show[2] and rt - rf > 0.3) {
        parts .= center{[(rf + rt) / 2, CYS, 0]} scale{1.3} color{INK} Number(15 - kk, 0)
    }
    if (show[3]) {
        parts .= center{[2.55, -0.3, 0]} scale{1.3} color{INK} Number(kk - 3, 0)
    }
    return parts
}
let StudentVenn = |k, show| [StudentShapes(k), StudentNums(k, show)]
let ClassBox = [
    Outline([[-3.6, -1.9, 0], [3.6, -1.9, 0], [3.6, 1.9, 0], [-3.6, 1.9, 0]], SLATE, 3),
    center{[-2.2, 1.5, 0]} Cap("football: 18", 0.8, AZURE),
    center{[0.75, 1.5, 0]} Cap("tennis: 15", 0.8, EMBER),
    center{[2.55, 1.5, 0]} Cap("30 students", 0.8, INK),
    center{[2.55, 0.2, 0]} Note("neither", 0.78)
]

# the numbers 1 to 12, with A the multiples of 2 and B the multiples of 3
let UNIV = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
let inA = |n| to_int(mod(n, 2) == 0)
let inB = |n| to_int(mod(n, 3) == 0)
let C1 = [-2.05, 0, 0]
let C2 = [-0.35, 0, 0]
let RAD = 1.3
let ANG = arccos(0.85 / RAD)
let lens = flatten([arcpts(C1, RAD, 0 - ANG, ANG, 24), arcpts(C2, RAD, PI - ANG + 0.03, PI + ANG - 0.03, 24)])
let NumVenn = |ff, po, qo, both| [
    center{[-1.2, 0, 0]} fill{ff} stroke{SLATE, 3} Rect([4.8, 3.7]),
    center{C1} fill{po} stroke{po, 1} Circle(RAD, 96),
    center{C2} fill{qo} stroke{qo, 1} Circle(RAD, 96),
    Region(lens, both),
    center{C1} fill{alpha{0} PAPER} stroke{AZURE, 4} Circle(RAD, 96),
    center{C2} fill{alpha{0} PAPER} stroke{EMBER, 4} Circle(RAD, 96)
]
let SPOTS = [
    [[-3.25, 1.5, 0], [0.85, 1.5, 0], [-3.25, -1.5, 0], [0.85, -1.5, 0]],
    [[0.3, 0.45, 0], [0.3, -0.45, 0]],
    [[-2.8, 0.45, 0], [-2.15, 0.55, 0], [-2.8, -0.45, 0], [-2.15, -0.55, 0]],
    [[-1.2, 0.42, 0], [-1.2, -0.42, 0]]
]
let place = |n| {
    let a = inA(n)
    let b = inB(n)
    let idx = len(filter(UNIV, |m| inA(m) == a and inB(m) == b and m < n))
    return SPOTS[2 * a + b][idx]
}
let Token = |n| [
    center{place(n)} fill{PAPER} stroke{SLATE, 2} Circle(0.23, 48),
    center{place(n)} Math(to_string(n), 0.74, INK)
]
let Tokens = [
    map(UNIV, |n| Token(n)),
    center{[-2.5, 1.52, 0]} Math("A", 0.95, AZURE),
    center{[0.1, 1.52, 0]} Math("B", 0.95, EMBER),
    center{[-1.2, -1.58, 0]} Math("U", 0.9, SLATE)
]
let ON = tint(LEAF, 0.35)