MatheramaMatherama
← Combinatorics
Combinatorics·Section 8·15 min read

Principle of Inclusion-Exclusion, Derangements, and Euler Totient Function

In Section 01, the Subtraction Principle handled problems with a single restriction: ∣A′∣=∣U∣−∣A∣|A'| = |U| - |A|. But what if a counting problem imposes two, three, or nn simultaneous restrictions C1,C2,…,CnC_1, C_2, \dots, C_n—and we want to count configurations that satisfy none of them (or at least one of them)?

Subtracting each restricted set separately over-subtracts the elements that belong to two or more sets at once. The Principle of Inclusion–Exclusion (PIE) is the exact generalization of the Subtraction Principle that corrects every overlap systematically.


Set-Theoretic Foundation and Introductory Example§

Recall three foundational laws from Set Theory (Chapter 01):

1. Inclusion–Exclusion Law for Unions: For two sets A1,A2A_1, A_2 and three sets A1,A2,A3A_1, A_2, A_3:

∣A1∪A2∣=∣A1∣+∣A2∣−∣A1∩A2∣|A_1 \cup A_2| = |A_1| + |A_2| - |A_1 \cap A_2| ∣A1∪A2∪A3∣=∣A1∣+∣A2∣+∣A3∣−∣A1∩A2∣−∣A2∩A3∣−∣A3∩A1∣+∣A1∩A2∩A3∣|A_1 \cup A_2 \cup A_3| = |A_1| + |A_2| + |A_3| - |A_1 \cap A_2| - |A_2 \cap A_3| - |A_3 \cap A_1| + |A_1 \cap A_2 \cap A_3|

Venn Diagram for Three Sets in the Principle of Inclusion-Exclusion

2. De Morgan's Laws:

(A∪B)′=A′∩B′,(A∩B)′=A′∪B′(A \cup B)' = A' \cap B', \qquad (A \cap B)' = A' \cup B'

3. Complement Law: For any subset AA of a universal set UU, ∣A′∣=∣U∣−∣A∣|A'| = |U| - |A|.

Before stating the general nn-set formula, watch how these laws work together on a concrete problem.

Example 1: Find the count of integers from 11 to 100100 that are divisible by neither 33 nor 55.

Solution: Let U={1,2,…,100}U = \{1, 2, \dots, 100\} be the universal set obtained by relaxing both conditions (∣U∣=100|U| = 100). Define:

  • A1A_1: integers in UU divisible by 33 (condition C1C_1).
  • A2A_2: integers in UU divisible by 55 (condition C2C_2).

Then A1′A_1' is the set of integers not divisible by 33, A2′A_2' is the set of integers not divisible by 55, and A1′∩A2′A_1' \cap A_2' is the set of integers divisible by neither 33 nor 55. Using De Morgan's Law and the union formula:

∣A1′∩A2′∣=∣(A1∪A2)′∣=∣U∣−∣A1∪A2∣=∣U∣−∣A1∣−∣A2∣+∣A1∩A2∣|A_1' \cap A_2'| = |(A_1 \cup A_2)'| = |U| - |A_1 \cup A_2| = |U| - |A_1| - |A_2| + |A_1 \cap A_2|

Now compute each term (note that A1∩A2A_1 \cap A_2 consists of multiples of lcm⁡(3,5)=15\operatorname{lcm}(3, 5) = 15):

∣A1∣=⌊1003⌋=33,∣A2∣=⌊1005⌋=20,∣A1∩A2∣=⌊10015⌋=6|A_1| = \left\lfloor \frac{100}{3} \right\rfloor = 33, \qquad |A_2| = \left\lfloor \frac{100}{5} \right\rfloor = 20, \qquad |A_1 \cap A_2| = \left\lfloor \frac{100}{15} \right\rfloor = 6

Substituting gives:

∣A1′∩A2′∣=100−33−20+6=53|A_1' \cap A_2'| = 100 - 33 - 20 + 6 = 53

Notice what happened geometrically: UU splits into elements satisfying C1C_1 only, C2C_2 only, both C1C_1 and C2C_2, and neither. Subtracting ∣A1∣|A_1| and ∣A2∣|A_2| removed the 66 multiples of 1515 twice, so adding ∣A1∩A2∣|A_1 \cap A_2| back restored the exact count.


The General Principle of Inclusion-Exclusion§

Suppose a combinatorial universe UU has nn conditions C1,C2,…,CnC_1, C_2, \dots, C_n, and let Ai⊆UA_i \subseteq U be the subset of elements satisfying condition CiC_i (1≤i≤n1 \le i \le n).

Theorem (Principle of Inclusion–Exclusion):

1. Complement Form (None of C1,C2,…,CnC_1, C_2, \dots, C_n hold):

∣A1′∩A2′∩⋯∩An′∣=∣U∣−∑∣Ai∣+∑∣Ai∩Aj∣−∑∣Ai∩Aj∩Ak∣+⋯+(−1)n∣A1∩A2∩⋯∩An∣|A_1' \cap A_2' \cap \dots \cap A_n'| = |U| - \sum |A_i| + \sum |A_i \cap A_j| - \sum |A_i \cap A_j \cap A_k| + \dots + (-1)^n |A_1 \cap A_2 \cap \dots \cap A_n|

2. Union Form (At least one of C1,C2,…,CnC_1, C_2, \dots, C_n holds):

∣A1∪A2∪⋯∪An∣=∑∣Ai∣−∑∣Ai∩Aj∣+∑∣Ai∩Aj∩Ak∣−⋯+(−1)n−1∣A1∩A2∩⋯∩An∣|A_1 \cup A_2 \cup \dots \cup A_n| = \sum |A_i| - \sum |A_i \cap A_j| + \sum |A_i \cap A_j \cap A_k| - \dots + (-1)^{n-1} |A_1 \cap A_2 \cap \dots \cap A_n|

Understanding the Sigma Notation§

Make sure you know exactly what each summation represents and how many terms it contains:

  • ∑∣Ai∣=∣A1∣+∣A2∣+⋯+∣An∣\sum |A_i| = |A_1| + |A_2| + \dots + |A_n| — sum of all single sets ((n1)\binom{n}{1} terms).
  • ∑∣Ai∩Aj∣=∣A1∩A2∣+∣A1∩A3∣+⋯+∣An−1∩An∣\sum |A_i \cap A_j| = |A_1 \cap A_2| + |A_1 \cap A_3| + \dots + |A_{n-1} \cap A_n| — sum over all pairs of distinct sets ((n2)\binom{n}{2} terms).
  • In general, ∑∣Ai1∩Ai2∩⋯∩Aik∣\sum |A_{i_1} \cap A_{i_2} \cap \dots \cap A_{i_k}| is the sum over all possible kk-fold intersections (1≤i1<i2<⋯<ik≤n1 \le i_1 < i_2 < \dots < i_k \le n), containing exactly (nk)\binom{n}{k} terms.

Proof of the Complement Form: Take any element x∈Ux \in U, and suppose xx belongs to exact mm of the sets A1,A2,…,AnA_1, A_2, \dots, A_n (0≤m≤n0 \le m \le n). Let us count how many times xx is counted on both sides:

  • Case m=0m = 0 (xx lies in none of the AiA_i): On the LHS, x∈A1′∩⋯∩An′x \in A_1' \cap \dots \cap A_n', so xx contributes 11. On the RHS, xx is counted once in ∣U∣|U| and 00 times in every intersection sum, giving 11.
  • Case m≥1m \ge 1 (xx lies in mm of the sets): On the LHS, x∉A1′∩⋯∩An′x \notin A_1' \cap \dots \cap A_n', so xx contributes 00. On the RHS, xx is counted once in ∣U∣|U|, (m1)\binom{m}{1} times in ∑∣Ai∣\sum |A_i|, (m2)\binom{m}{2} times in ∑∣Ai∩Aj∣\sum |A_i \cap A_j|, ..., and (mm)\binom{m}{m} times in the mm-fold intersection sum (and 00 times in higher intersections). By the Binomial Theorem for (1+x)m(1 + x)^m evaluated at x=−1x = -1, its net contribution on the RHS is:
1−(m1)+(m2)−(m3)+⋯+(−1)m(mm)=(1−1)m=01 - \binom{m}{1} + \binom{m}{2} - \binom{m}{3} + \dots + (-1)^m \binom{m}{m} = (1 - 1)^m = 0

Since every element x∈Ux \in U contributes identically to both sides, the two sides are equal. Subtracting from ∣U∣|U| immediately gives the Union Form. ■\blacksquare

Example 2 (Forbidden Substrings — DROPTHEMIC): Find the number of arrangements of the 1010-letter word DROPTHEMIC (all 1010 letters distinct) in which none of the blocks DROP, THE, or MIC appear as contiguous substrings.

Solution: Let UU be all 10!10! unrestricted arrangements. Define A1,A2,A3A_1, A_2, A_3 as the sets of arrangements containing DROP, THE, and MIC respectively. Because these three blocks share no common letters, we simply bundle each required block as a single unit:

  • ∣U∣=10!|U| = 10!.
  • ∣A1∣=7!|A_1| = 7! (tie DROP into 11 unit +6+ 6 remaining letters =7= 7 units).
  • ∣A2∣=8!|A_2| = 8! (tie THE into 11 unit +7+ 7 remaining letters =8= 8 units).
  • ∣A3∣=8!|A_3| = 8! (tie MIC into 11 unit +7+ 7 remaining letters =8= 8 units).
  • ∣A1∩A2∣=5!|A_1 \cap A_2| = 5! (DROP, THE, and M, I, C =5= 5 units).
  • ∣A2∩A3∣=6!|A_2 \cap A_3| = 6! (THE, MIC, and D, R, O, P =6= 6 units).
  • ∣A1∩A3∣=5!|A_1 \cap A_3| = 5! (DROP, MIC, and T, H, E =5= 5 units).
  • ∣A1∩A2∩A3∣=3!|A_1 \cap A_2 \cap A_3| = 3! (the 33 units DROP, THE, MIC).

Applying PIE:

∣A1′∩A2′∩A3′∣=∣U∣−∣A1∣−∣A2∣−∣A3∣+∣A1∩A2∣+∣A2∩A3∣+∣A1∩A3∣−∣A1∩A2∩A3∣|A_1' \cap A_2' \cap A_3'| = |U| - |A_1| - |A_2| - |A_3| + |A_1 \cap A_2| + |A_2 \cap A_3| + |A_1 \cap A_3| - |A_1 \cap A_2 \cap A_3| =10!−7!−2(8!)+6!+2(5!)−3!=3,628,800−5040−80,640+720+240−6=3,544,074= 10! - 7! - 2(8!) + 6! + 2(5!) - 3! = 3{,}628{,}800 - 5040 - 80{,}640 + 720 + 240 - 6 = 3{,}544{,}074

Example 3 (Three Divisors): How many integers from 11 to 10001000 are divisible by none of 22, 33, or 55?

Solution: Let A1,A2,A3A_1, A_2, A_3 be multiples of 2,3,52, 3, 5 in {1,…,1000}\{1, \dots, 1000\}. Then:

  • ∣A1∣=⌊1000/2⌋=500|A_1| = \lfloor 1000/2 \rfloor = 500, ∣A2∣=⌊1000/3⌋=333|A_2| = \lfloor 1000/3 \rfloor = 333, ∣A3∣=⌊1000/5⌋=200|A_3| = \lfloor 1000/5 \rfloor = 200.
  • ∣A1∩A2∣=⌊1000/6⌋=166|A_1 \cap A_2| = \lfloor 1000/6 \rfloor = 166, ∣A2∩A3∣=⌊1000/15⌋=66|A_2 \cap A_3| = \lfloor 1000/15 \rfloor = 66, ∣A1∩A3∣=⌊1000/10⌋=100|A_1 \cap A_3| = \lfloor 1000/10 \rfloor = 100.
  • ∣A1∩A2∩A3∣=⌊1000/30⌋=33|A_1 \cap A_2 \cap A_3| = \lfloor 1000/30 \rfloor = 33.

By PIE:

∣A1′∩A2′∩A3′∣=1000−(500+333+200)+(166+66+100)−33=1000−1033+332−33=266|A_1' \cap A_2' \cap A_3'| = 1000 - (500 + 333 + 200) + (166 + 66 + 100) - 33 = 1000 - 1033 + 332 - 33 = 266

Example 4 (Upper-Bounded Integral Equation — Extending Section 07): Find the number of integer solutions to x1+x2+x3=15x_1 + x_2 + x_3 = 15 subject to 0≤x1≤60 \le x_1 \le 6, 0≤x2≤70 \le x_2 \le 7, and 0≤x3≤80 \le x_3 \le 8.

Solution: Let UU be all non-negative integer solutions of x1+x2+x3=15x_1 + x_2 + x_3 = 15, so ∣U∣=(15+3−12)=(172)=136|U| = \binom{15 + 3 - 1}{2} = \binom{17}{2} = 136. Define the three violation sets where each upper bound fails:

  • A1A_1 (x1≥7x_1 \ge 7): Shift by 7  ⟹  7 \implies sum =8  ⟹  ∣A1∣=(8+22)=(102)=45= 8 \implies |A_1| = \binom{8 + 2}{2} = \binom{10}{2} = 45.
  • A2A_2 (x2≥8x_2 \ge 8): Shift by 8  ⟹  8 \implies sum =7  ⟹  ∣A2∣=(7+22)=(92)=36= 7 \implies |A_2| = \binom{7 + 2}{2} = \binom{9}{2} = 36.
  • A3A_3 (x3≥9x_3 \ge 9): Shift by 9  ⟹  9 \implies sum =6  ⟹  ∣A3∣=(6+22)=(82)=28= 6 \implies |A_3| = \binom{6 + 2}{2} = \binom{8}{2} = 28.

Check pairwise and triple violations:

  • A1∩A2A_1 \cap A_2 (x1≥7,  x2≥8x_1 \ge 7,\; x_2 \ge 8): Shift sum by 15  ⟹  15 \implies remaining sum =0  ⟹  ∣A1∩A2∣=(0+22)=1= 0 \implies |A_1 \cap A_2| = \binom{0 + 2}{2} = 1.
  • A1∩A3A_1 \cap A_3 (x1≥7,  x3≥9x_1 \ge 7,\; x_3 \ge 9): Minimum sum 16>15  ⟹  ∣A1∩A3∣=016 > 15 \implies |A_1 \cap A_3| = 0.
  • A2∩A3A_2 \cap A_3 (x2≥8,  x3≥9x_2 \ge 8,\; x_3 \ge 9): Minimum sum 17>15  ⟹  ∣A2∩A3∣=017 > 15 \implies |A_2 \cap A_3| = 0.
  • A1∩A2∩A3A_1 \cap A_2 \cap A_3: Clearly 00.

By the Principle of Inclusion–Exclusion:

∣A1′∩A2′∩A3′∣=136−(45+36+28)+(1+0+0)−0=28|A_1' \cap A_2' \cap A_3'| = 136 - (45 + 36 + 28) + (1 + 0 + 0) - 0 = 28

Derangements§

Imagine nn people sitting in a room, each wearing their own hat. They toss all nn hats into a pile and redistribute them so that no one receives their own hat—every person ends up wearing someone else's hat. In how many ways can this happen?

A permutation of nn distinct objects in which no object sits in its original designated position is called a derangement, and the total number of derangements of nn objects is denoted by D(n)D(n) (or DnD_n).

Theorem (Derangement Formula): For every positive integer n≥1n \ge 1:

D(n)=n!(1−11!+12!−13!+⋯+(−1)nn!)=n!∑k=0n(−1)kk!D(n) = n!\left(1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + \dots + \frac{(-1)^n}{n!}\right) = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}

(with the convention D(0)=1D(0) = 1, representing the single empty arrangement of 00 hats).

Proof (Via PIE): Let UU be all n!n! permutations of the nn hats (∣U∣=n!|U| = n!). For each k∈{1,2,…,n}k \in \{1, 2, \dots, n\}, let AkA_k be the set of permutations in which the kk-th person receives their own hat (we do not restrict what happens to the other n−1n - 1 people).

What does Ak′A_k' mean? That the kk-th person does not have their own hat. Hence A1′∩A2′∩⋯∩An′A_1' \cap A_2' \cap \dots \cap A_n' is precisely the set of arrangements in which no one has their own hat—so D(n)=∣A1′∩A2′∩⋯∩An′∣D(n) = |A_1' \cap A_2' \cap \dots \cap A_n'|.

Compute each intersection sum:

  • Single sets: Fixing the ii-th person's hat leaves the remaining n−1n - 1 hats to permute in (n−1)!(n - 1)! ways, so ∣Ai∣=(n−1)!|A_i| = (n - 1)!. Since there are (n1)\binom{n}{1} terms: ∑∣Ai∣=(n1)(n−1)!\sum |A_i| = \binom{n}{1}(n - 1)!.
  • Pairs: Fixing both the ii-th and jj-th persons' hats leaves (n−2)!(n - 2)! permutations of the rest. Since there are (n2)\binom{n}{2} pairs: ∑∣Ai∩Aj∣=(n2)(n−2)!\sum |A_i \cap A_j| = \binom{n}{2}(n - 2)!.
  • In general (kk-fold intersections): ∑∣Ai1∩⋯∩Aik∣=(nk)(n−k)!=n!k!\sum |A_{i_1} \cap \dots \cap A_{i_k}| = \binom{n}{k}(n - k)! = \frac{n!}{k!}.

Substituting into PIE:

D(n)=n!−(n1)(n−1)!+(n2)(n−2)!−(n3)(n−3)!+⋯+(−1)n(nn)(n−n)!D(n) = n! - \binom{n}{1}(n - 1)! + \binom{n}{2}(n - 2)! - \binom{n}{3}(n - 3)! + \dots + (-1)^n \binom{n}{n}(n - n)! =n!−n!1!+n!2!−n!3!+⋯+(−1)nn!n!=n!(1−11!+12!−13!+⋯+(−1)nn!)■= n! - \frac{n!}{1!} + \frac{n!}{2!} - \frac{n!}{3!} + \dots + (-1)^n \frac{n!}{n!} = n!\left(1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + \dots + \frac{(-1)^n}{n!}\right) \qquad \blacksquare

Computing the first several values of D(n)D(n) is worth memorizing:

nn 00 11 22 33 44 55 66
D(n)D(n) 11 00 11 22 99 4444 265265

(Remarkable connection to calculus: since e−1=1−11!+12!−13!+⋯≈0.3679e^{-1} = 1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + \dots \approx 0.3679, the fraction of permutations that are derangements rapidly approaches 1/e≈36.8%1 / e \approx 36.8\% as nn grows!)

Five Essential Properties of Derangements§

Let nn people each own a hat, and consider the following variations:

1. Exactly kk people receive their own hats: First choose which kk people get their own hats in (nk)\binom{n}{k} ways. The remaining n−kn - k people must all receive wrong hats, which can be done in D(n−k)D(n - k) ways. Total ways:

(nk)D(n−k)\binom{n}{k} D(n - k)

2. Mr. XX always takes Mr. YY's hat (X≠YX \neq Y): Suppose among nn people (n≥2n \ge 2), Mr. XX insists on keeping Mr. YY's hat, and all nn people must end up with wrong hats. Is the answer simply D(n−1)D(n - 1)? No! Let us see why.

Picture Mr. XX walking out of the room wearing Mr. YY's hat. Now n−1n - 1 people remain in the room along with n−1n - 1 hats (everyone's own hat except Mr. YY's—and Mr. XX's abandoned hat sitting on the table). Mr. YY reluctantly picks up Mr. XX's abandoned hat and faces two disjoint choices:

  • Case I (Mr. YY keeps Mr. XX's hat and steps out of the swap): Both XX and YY now have wrong hats! The remaining n−2n - 2 people simply derange their own n−2n - 2 hats in D(n−2)D(n - 2) ways.
  • Case II (Mr. YY refuses to keep Mr. XX's hat and joins the derangement): Treating Mr. XX's hat temporarily as "Mr. YY's forbidden hat," every one of the n−1n - 1 remaining people now has exactly one forbidden hat—which is precisely a derangement of n−1n - 1 people in D(n−1)D(n - 1) ways.

Adding these two disjoint cases gives the total number of valid derangements:

D(n−1)+D(n−2)D(n - 1) + D(n - 2)

Two Disjoint Cases When Mr X Takes Mr Ys Hat Proving the Derangement Recurrence

3. Recurrence Relation: For every n≥2n \ge 2:

D(n)=(n−1)(D(n−1)+D(n−2))D(n) = (n - 1)\big(D(n - 1) + D(n - 2)\big)

Proof: Person 11 cannot receive Hat 11, so Person 11 has n−1n - 1 choices of whose hat to take (Hat 22, Hat 33, ..., or Hat nn). By Property 2, for each of those n−1n - 1 choices, the remaining n−1n - 1 hats can be deranged in D(n−1)+D(n−2)D(n - 1) + D(n - 2) ways. Multiplying yields D(n)=(n−1)(D(n−1)+D(n−2))D(n) = (n - 1)\big(D(n - 1) + D(n - 2)\big). ■\blacksquare

(Algebraic simplification of the explicit formula also gives the one-step recurrence D(n)=n D(n−1)+(−1)nD(n) = n\,D(n - 1) + (-1)^n.)

4. Partitioning All n!n! Permutations by Fixed Points: Let SS be all n!n! permutations of nn objects, and partition SS into S0,S1,…,SnS_0, S_1, \dots, S_n, where SkS_k contains all permutations with exact kk objects in their original positions (∣Sk∣=(nk)D(n−k)|S_k| = \binom{n}{k} D(n - k)). By the Addition Principle:

n!=(n0)D(n)+(n1)D(n−1)+(n2)D(n−2)+⋯+(nn)D(0)n! = \binom{n}{0}D(n) + \binom{n}{1}D(n - 1) + \binom{n}{2}D(n - 2) + \dots + \binom{n}{n}D(0)

5. Partial Derangement (kk specific people do not get their own hats): Suppose we only require that kk specific people (P1,P2,…,PkP_1, P_2, \dots, P_k) do not receive their own hats, while we do not care whether the remaining n−kn - k people get their own hats or not. Applying PIE solely to the kk sets A1,A2,…,AkA_1, A_2, \dots, A_k gives:

∣A1′∩A2′∩⋯∩Ak′∣=n!−(k1)(n−1)!+(k2)(n−2)!−⋯+(−1)k(kk)(n−k)!|A_1' \cap A_2' \cap \dots \cap A_k'| = n! - \binom{k}{1}(n - 1)! + \binom{k}{2}(n - 2)! - \dots + (-1)^k \binom{k}{k}(n - k)!

Example 5: Five letters are addressed to 55 different recipients. In how many ways can the letters be placed into the 55 envelopes so that: (a) no letter goes into its correct envelope, (b) at least 22 letters go into their correct envelopes, (c) neither Letter 11 nor Letter 22 goes into its correct envelope (with no restriction on Letters 3,4,53, 4, 5)?

Solution:

(a) D(5)=44D(5) = 44.

(b) Partition by exact number k∈{2,3,4,5}k \in \{2, 3, 4, 5\} of letters placed correctly:

(52)D(3)+(53)D(2)+(54)D(1)+(55)D(0)=10(2)+10(1)+5(0)+1(1)=31\binom{5}{2}D(3) + \binom{5}{3}D(2) + \binom{5}{4}D(1) + \binom{5}{5}D(0) = 10(2) + 10(1) + 5(0) + 1(1) = 31

(c) Apply Property 5 with n=5n = 5 and k=2k = 2:

5!−(21)4!+(22)3!=120−48+6=785! - \binom{2}{1}4! + \binom{2}{2}3! = 120 - 48 + 6 = 78

Euler's Totient Function§

One of the most elegant applications of the Principle of Inclusion–Exclusion in number theory is Euler's Totient Function, denoted φ(n)\varphi(n).

Definition: For any positive integer nn, φ(n)\varphi(n) is the count of positive integers m∈{1,2,…,n}m \in \{1, 2, \dots, n\} that are coprime to nn—that is, gcd⁡(m,n)=1\gcd(m, n) = 1.

Checking small values directly:

  • φ(1)=1\varphi(1) = 1 (since gcd⁡(1,1)=1\gcd(1, 1) = 1).
  • φ(2)=1\varphi(2) = 1 (11).
  • φ(3)=2\varphi(3) = 2 (1,21, 2).
  • φ(4)=2\varphi(4) = 2 (1,31, 3).
  • φ(5)=4\varphi(5) = 4 (1,2,3,41, 2, 3, 4; in general, φ(p)=p−1\varphi(p) = p - 1 for any prime pp).

Theorem (Euler's Product Formula): If nn has the prime factorization n=p1m1p2m2⋯pkmkn = p_1^{m_1} p_2^{m_2} \cdots p_k^{m_k} (where p1,p2,…,pkp_1, p_2, \dots, p_k are distinct primes), then:

φ(n)=n(1−1p1)(1−1p2)⋯(1−1pk)=n∏r=1k(1−1pr)\varphi(n) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right) = n \prod_{r=1}^{k} \left(1 - \frac{1}{p_r}\right)

Proof (Via PIE): Let U={1,2,…,n}U = \{1, 2, \dots, n\} (∣U∣=n|U| = n). For each distinct prime factor prp_r of nn (1≤r≤k1 \le r \le k), let ArA_r be the set of integers in UU divisible by prp_r:

Ar={pr,  2pr,  3pr,  …,  ⌊npr⌋pr}  ⟹  ∣Ar∣=⌊npr⌋=nprA_r = \left\{p_r,\; 2p_r,\; 3p_r,\; \dots,\; \left\lfloor \frac{n}{p_r} \right\rfloor p_r\right\} \implies |A_r| = \left\lfloor \frac{n}{p_r} \right\rfloor = \frac{n}{p_r}

Why can we drop the greatest-integer brackets ⌊⋅⌋\lfloor \cdot \rfloor? Because prp_r is a prime factor of nn, so n/prn / p_r is already an exact integer! Similarly, for any two distinct primes pi,pjp_i, p_j dividing nn, their product pipjp_i p_j also divides nn, so:

∣Ai∩Aj∣=npipj,∣Ai∩Aj∩Al∣=npipjpl,…,∣A1∩⋯∩Ak∣=np1p2⋯pk|A_i \cap A_j| = \frac{n}{p_i p_j}, \qquad |A_i \cap A_j \cap A_l| = \frac{n}{p_i p_j p_l}, \qquad \dots, \qquad |A_1 \cap \dots \cap A_k| = \frac{n}{p_1 p_2 \cdots p_k}

An integer in UU is coprime to nn if and only if it shares none of the prime factors p1,p2,…,pkp_1, p_2, \dots, p_k—which is precisely the set A1′∩A2′∩⋯∩Ak′A_1' \cap A_2' \cap \dots \cap A_k'. Applying PIE:

φ(n)=n−(np1+np2+⋯+npk)+(np1p2+np1p3+… )−⋯+(−1)knp1p2⋯pk\varphi(n) = n - \left(\frac{n}{p_1} + \frac{n}{p_2} + \dots + \frac{n}{p_k}\right) + \left(\frac{n}{p_1 p_2} + \frac{n}{p_1 p_3} + \dots\right) - \dots + (-1)^k \frac{n}{p_1 p_2 \cdots p_k}

Factoring nn out of every term reveals the exact expansion of the product:

φ(n)=n(1−1p1)(1−1p2)⋯(1−1pk)■\varphi(n) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right) \qquad \blacksquare

Example 6: Compute φ(12)\varphi(12), φ(360)\varphi(360), and φ(pa)\varphi(p^a) for a prime pp and a≥1a \ge 1.

Solution:

  • n=12=22⋅3n = 12 = 2^2 \cdot 3: φ(12)=12(1−12)(1−13)=12⋅12⋅23=4\varphi(12) = 12\left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right) = 12 \cdot \frac{1}{2} \cdot \frac{2}{3} = 4 (namely 1,5,7,111, 5, 7, 11).
  • n=360=23⋅32⋅5n = 360 = 2^3 \cdot 3^2 \cdot 5: φ(360)=360(1−12)(1−13)(1−15)=360⋅12⋅23⋅45=96\varphi(360) = 360\left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right)\left(1 - \frac{1}{5}\right) = 360 \cdot \frac{1}{2} \cdot \frac{2}{3} \cdot \frac{4}{5} = 96.
  • n=pan = p^a: Only one distinct prime factor pp divides pap^a, so φ(pa)=pa(1−1p)=pa−pa−1\varphi(p^a) = p^a\left(1 - \frac{1}{p}\right) = p^a - p^{a - 1} (every integer from 11 to pap^a except the pa−1p^{a - 1} multiples of pp).

Summary Table§

Concept Formula Key Application
PIE (None of A1,…,AnA_1, \dots, A_n) ∣U∣−∑∣Ai∣+∑∣Ai∩Aj∣−⋯+(−1)n∣⋂Ai∣\lvert U \rvert - \sum \lvert A_i \rvert + \sum \lvert A_i \cap A_j \rvert - \dots + (-1)^n \lvert \bigcap A_i \rvert Counting configurations satisfying none of nn forbidden conditions
PIE (At least one of A1,…,AnA_1, \dots, A_n) ∑∣Ai∣−∑∣Ai∩Aj∣+⋯+(−1)n−1∣⋂Ai∣\sum \lvert A_i \rvert - \sum \lvert A_i \cap A_j \rvert + \dots + (-1)^{n-1} \lvert \bigcap A_i \rvert Counting unions of overlapping sets
Derangements D(n)D(n) n!∑k=0n(−1)kk!n! \sum_{k=0}^n \frac{(-1)^k}{k!} Permutations with zero objects in their own positions
Derangement Recurrence D(n)=(n−1)(D(n−1)+D(n−2))D(n) = (n - 1)\big(D(n - 1) + D(n - 2)\big) Fast recursive evaluation of D(n)D(n)
Exact kk Fixed Points (nk)D(n−k)\binom{n}{k} D(n - k) Partial matching problems
Euler's Totient φ(n)\varphi(n) n∏p∣n(1−1p)n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) Count of integers in {1,…,n}\{1, \dots, n\} coprime to nn
Section 8 of 11 · Combinatorics

Mark this section complete when you have worked through its proofs.