Chinese Remainder Theorem

Theorem (CRT).
If n1,n2,…,nk∈Nn_1, n_2, \dots, n_k \in \mathbb{N} are pairwise coprime, then for any a1,a2,…,ak∈Na_1, a_2, \dots, a_k \in \mathbb{N}, there exists an integer xx such that

x≡a1(modn1),x≡a2(modn2),    ⋮x≡ak(modnk). \begin{aligned} x &\equiv a_1 \pmod{n_1}, \\ x &\equiv a_2 \pmod{n_2}, \\ &\;\;\vdots \\ x &\equiv a_k \pmod{n_k}. \end{aligned}

Proof One

For each i∈[k]i \in [k], define

Ni=∏j=1knjni. N_i = \frac{\prod_{j=1}^k n_j}{n_i}.

Clearly, nj∣Nin_j \mid N_i for every j≠ij \neq i. Moreover, gcd⁡(Ni,ni)=1\gcd(N_i, n_i) = 1. Hence, there exists Mi∈[1,ni−1]M_i \in [1, n_i - 1] such that

MiNi≡1(modni). M_i N_i \equiv 1 \pmod{n_i}.

It follows that

aiMiNi≡ai(modni). a_i M_i N_i \equiv a_i \pmod{n_i}.

Therefore,

x=∑i=1kaiMiNi x = \sum_{i=1}^k a_i M_i N_i

is a solution. In fact, for any ℓ∈Z\ell \in \mathbb{Z},

x+ℓ⋅∏i=1kni x + \ell \cdot \prod_{i=1}^k n_i

is also a solution. ∎


Proof Two (More Intuitive)

Consider a simple example with moduli 22 and 33:

012345⋯mod  2010101⋯mod  3012012⋯ \begin{array}{c|cccccc} \hline & 0 & 1 & 2 & 3 & 4 & 5 & \cdots \\ \hline \mod 2 & 0 & 1 & 0 & 1 & 0 & 1 & \cdots \\ \mod 3 & 0 & 1 & 2 & 0 & 1 & 2 & \cdots \\ \hline \end{array}

There is a one-to-one correspondence between numbers in {0,1,2,3,4,5}\{0,1,2,3,4,5\} and pairs in {0,1}×{0,1,2}\{0,1\} \times \{0,1,2\}.

In general, if n1,n2,…,nkn_1, n_2, \dots, n_k are pairwise coprime, then for any x,y∈[0,…,−1+∏jnj]x,y \in [0, \ldots, -1 + \prod_j n_j], if

x≡y(modn1),x≡y(modn2),    ⋮x≡y(modnk), \begin{aligned} x &\equiv y \pmod{n_1}, \\ x &\equiv y \pmod{n_2}, \\ &\;\;\vdots \\ x &\equiv y \pmod{n_k}, \end{aligned}

then x=yx = y.

To see this, since nj∣(y−x)n_j \mid (y-x) for all jj. Therefore, y−xy - x is a common multiple of nj,j∈[k]n_j, j \in [k]. Consider the set

{ z∈N:z is a common multiple of nj,∀j∈k] }. \{ \, z \in \N : z \text{ is a common multiple of } n_j, \forall j \in k] \, \}.

It is easy to see each element in this set is a multiple of the minimum one in this set, which is the least common multiple of the njn_j, ∀j∈[k]\forall j \in [k]. Since the njn_j’s are coprime, it follows that the least common multiple is ∏jnj\prod_j n_j and

∏jnj∣(y−x). \prod_j n_j \mid (y-x).

But since x,y∈[0,…,−1+∏jnj]x,y \in [0, \ldots, -1 + \prod_j n_j], we must have x=yx=y.

Thus, there is a one-to-one correspondence between [0,…,−1+∏jnj][0, \ldots, -1 + \prod_j n_j] and the Cartesian product [n1]×[n2]×⋯×[nk][n_1] \times [n_2] \times \cdots \times [n_k].

□\square


Corollary

There is a unique solution in [0,…,−1+∏jnj][0, \ldots, -1 + \prod_j n_j].

Proof. Suppose xx and yy are two solutions. Then for every i∈[k]i \in [k],

x−y≡0(modni). x - y \equiv 0 \pmod{n_i}.

Thus, x−yx-y is divisible by ∏i=1kni\prod_{i=1}^k n_i. Therefore,

x−y=ℓ⋅∏i=1kni x - y = \ell \cdot \prod_{i=1}^k n_i

for some ℓ\ell, so the difference between two solutions is at least ∏i=1kni\prod_{i=1}^k n_i. Hence there is exactly one solution modulo ∏i=1kni\prod_{i=1}^k n_i. ∎

Euler's Totient Function

The Chinese Remainder Theorem has many interesting applications. Here are a few:


Theorem 1.
Let φ(n)\varphi(n) denote Euler’s totient function, i.e., the number of integers in [0,…,n−1][0, \ldots, n - 1] that are coprime to nn. If n=pqn = pq for primes pp and qq, then

φ(n)=(p−1)(q−1). \varphi(n) = (p-1)(q-1).

Proof. By the CRT, there is a one-to-one correspondence between [0,…,n−1][0, \ldots, n - 1] and [0,…,p−1]×[0,…,q−1][0, \ldots, p - 1] \times [0, \ldots, q - 1]. If x∈[0,…,n−1]x \in [0, \ldots, n - 1] is not coprime to nn, then either p∣xp \mid x or q∣xq \mid x. Thus, xx corresponds to a pair of the form (0,a)(0,a) or (b,0)(b,0) for some a∈[0,…,q−1]a \in [0, \ldots, q - 1], b∈[0,…,p−1]b \in [0, \ldots, p - 1]. The number of such pairs is

p+q−1. p + q - 1.

Hence,

φ(n)=pq−(p+q−1)=(p−1)(q−1). \varphi(n) = pq - (p+q-1) = (p-1)(q-1).

∎


Theorem 2.
If n=pqn = pq for coprime integers p,q>1p,q > 1, then

φ(n)=φ(p)φ(q). \varphi(n) = \varphi(p)\varphi(q).

Proof. By Chinese Reminder Theorem, there is a one-to-one correspondence between [0,…,n−1][0, \ldots, n - 1] and [0,…,p−1]×[0,…,q−1][0, \ldots, p - 1] \times [0, \ldots, q - 1]. An integer x∈[0,…,n−1]x \in [0, \ldots, n - 1] is coprime to nn if and only if gcd⁡(x,p)=1\gcd(x,p)=1 and gcd⁡(x,q)=1\gcd(x,q)=1. Thus, xx corresponds to a pair (x mod p,x mod q)(x \bmod p, x \bmod q) where each component is coprime to its modulus. There are φ(p)\varphi(p) possibilities for the first component and φ(q)\varphi(q) for the second, so

φ(n)=φ(p)φ(q). \varphi(n) = \varphi(p)\varphi(q).

∎


Theorem 3.
Let n=p1k1p2k2⋯pmkmn = p_1^{k_1} p_2^{k_2} \cdots p_m^{k_m} be the prime factorization of nn. Then

φ(n)=n∏i=1m(1−1pi). \varphi(n) = n \prod_{i=1}^m \left(1 - \frac{1}{p_i}\right).

Proof. By multiplicativity (Theorem 2),

φ(n)=∏i=1mφ(piki). \varphi(n) = \prod_{i=1}^m \varphi(p_i^{k_i}).

For pkp^k, the multiples of pp in [pk][p^k] are

p,  2p,  3p,  …,  pk−1p, p, \; 2p, \; 3p, \; \dots, \; p^{k-1}p,

a total of pk−1p^{k-1} numbers. Thus,

φ(pk)=pk−pk−1=pk(1−1p). \varphi(p^k) = p^k - p^{k-1} = p^k \left(1 - \frac{1}{p}\right).

Therefore,

φ(n)=∏i=1mpiki(1−1pi)=n∏i=1m(1−1pi). \begin{aligned} \varphi(n) &= \prod_{i=1}^m p_i^{k_i}\left(1 - \frac{1}{p_i}\right) = n \prod_{i=1}^m \left(1 - \frac{1}{p_i}\right). \end{aligned}

∎