Most counting problems come down to two questions: how many choices are there, and does anything get counted more than once?
Cardinality
The cardinality of a finite set A, written ∣A∣, is the number of elements in A. For example,
A={2,5,7}⟹∣A∣=3.
The empty set has no elements, while a set containing the empty set has one element:
∣∅∣=0but∣{∅}∣=1.
The vertical bars in ∣A∣ mean cardinality here, not absolute value.
The product principle
Use the product principle, also called the multiplication principle, when you make several choices in order. If the first choice has m options and, after every possible first choice, the second choice has the same n options, there are
m⋅n
possible combinations. For more steps, multiply the number of options at each step when each count stays the same for every earlier valid choice. If the number of continuations depends on what was chosen earlier, split into cases or add the branch counts instead.
For finite sets A and B, this gives the cardinality of their Cartesian product:
∣A×B∣=∣A∣∣B∣.
If A={2,5,7} and B={1,2}, then
∣A×B∣=3⋅2=6.
Example: Canadian postal codes
Let L be the set of 26 uppercase letters and D the set of 10 digits:
L={A,B,…,Z},D={0,1,…,9}.
In a simplified format model, a Canadian postal code has the pattern
LDLDLD.
This model ignores the real restrictions on which letters may appear in particular positions. There are three letter positions and three digit positions. Each position can be chosen independently, and characters can repeat, so the number of strings matching the simplified pattern is
26⋅10⋅26⋅10⋅26⋅10=263⋅103=17,576,000.
For example, Santa's postal code H0H 0H0 follows this pattern. Each 0 is the digit zero, not the letter O. The calculation counts format-matching strings, not actual assignable postal codes.
Why a power set has size 2n
The power set P(A) contains every subset of A. Suppose
A={a1,a2,…,an}.
To choose a subset X⊆A, decide what to do with each element ai:
include ai in X; or
leave ai out of X.
There are two choices for each of the n elements, so the product principle gives
∣P(A)∣=2n=2∣A∣.
This also works for the empty set:
∣P(∅)∣=20=1,
because P(∅)={∅}.
The sum principle
Two sets are disjoint when they have no elements in common:
A∩B=∅.
For disjoint finite sets, the sum principle, also called the addition principle, says
∣A∪B∣=∣A∣+∣B∣.
Because the sets do not overlap, nothing is counted twice. The same rule works for any number of pairwise-disjoint finite sets.
Example: usernames of length 6, 7, or 8
Suppose a username may contain uppercase letters, lowercase letters, and digits. There are
26+26+10=62
allowed characters in total.
Let Sn be the set of valid usernames of length n. Since characters can repeat, the product principle gives
∣S6∣=626,∣S7∣=627,∣S8∣=628.
A string cannot have two different lengths, so S6, S7, and S8 are disjoint. The sum principle therefore gives
∣S6∪S7∪S8∣=626+627+628=221,918,520,426,688.
The complement principle
Suppose A⊆B and B is finite. The complement principle counts the elements that are in B but not in A:
∣B∖A∣=∣B∣−∣A∣.
This works because B splits into two parts that do not overlap:
B=A∪(B∖A).
The sets A and B∖A do not overlap, so the sum principle gives
∣B∣=∣A∣+∣B∖A∣.
Solving this equation for ∣B∖A∣ gives the complement principle.
Example: usernames containing at least one digit
Let B be the set of all valid usernames of length 6, 7, or 8, and let A⊆B contain the usernames with no digits. A username in B can use any of the 62 characters, while a username in A can use only the 52 letters. Therefore,
∣B∣∣A∣=626+627+628,=526+527+528.
The set B∖A contains exactly the usernames with at least one digit. By the complement principle,
A bit is either 0 or 1. How many bit strings of length 8 start with 1or end with 00?
Let
A={length-8 bit strings that start with 1}
and
B={length-8 bit strings that end with 00}.
For A, the first bit is fixed and the remaining seven bits are free, so
∣A∣=27.
For B, the last two bits are fixed and the remaining six are free, so
∣B∣=26.
A string in A∩B starts with 1 and ends with 00. Five positions remain free, so
∣A∩B∣=25.
By inclusion–exclusion,
∣A∪B∣=27+26−25=160.
Inclusion–exclusion for three or more sets
For three finite sets, inclusion–exclusion becomes
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.
Start by adding the sizes of the three sets, then subtract the three pairwise overlaps. A point in all three sets was added three times and then subtracted three times, so it is missing from the count. Add the triple intersection once to put it back.
The central region belongs to all three sets. It is added three times, removed three times with the pairwise intersections, then added back once.
For finite sets A1,A2,…,An, write out the pattern like this:
The signs alternate as the intersections get larger:
add the size of each set;
subtract every pairwise intersection;
add every triple intersection;
subtract every quadruple intersection;
add every quintuple intersection, and keep alternating.
For four sets, this becomes: add the four set sizes, subtract the six pairwise intersections, add the four triple intersections, then subtract the four-way intersection.
Example: integers with none of three properties
How many integers from 1 through 1000 have none of these properties: perfect square, perfect cube, or multiple of 7?
Let
U={1,2,…,1000},
and define
ABC={n∈U∣n=k2 for some integer k},={n∈U∣n=k3 for some integer k},={n∈U∣7 divides n}.
Here, A contains the perfect squares, B contains the perfect cubes, and C contains the multiples of 7.
The floor function⌊x⌋ is the greatest integer less than or equal to x. We use it here because the square and cube roots must be whole numbers:
∣A∣=⌊1000⌋=31,∣B∣=⌊31000⌋=10,
and
∣C∣=⌊71000⌋=142.
Now count the intersections:
A number that is both a square and a cube is a sixth power, so
∣A∩B∣=⌊61000⌋=3.
A square divisible by 7 has a square root divisible by 7. Among 1,2,…,31, there are four multiples of 7, so
∣A∩C∣=4.
A cube divisible by 7 has a cube root divisible by 7. Among 1,2,…,10, only 7 qualifies, so
∣B∩C∣=1.
A number in all three sets would be a sixth power whose base is divisible by 7. The smallest is 76=117,649>1000, so
∣A∩B∩C∣=0.
Inclusion–exclusion gives
∣A∪B∪C∣=31+10+142−3−4−1+0=175.
The question asks for the numbers outside all three sets, so subtract the union from U: