Chinese Remainder Theorem

Theorem: Suppose that m1, m2, ..., mr are pairwise relatively prime positive integers, and let a1, a2, ..., ar be integers. Then the system of congruences, x = ai (mod mi) for 1 <= i <= r, has a unique solution modulo M = m1 x m2 x ... x mr, which is given by:
x = a1M1y1 + a2M2y2 + ... + arMryr (mod M),
where Mi = M/mi and yi = (Mi)-1 (mod mi) for 1 <= i <= r.

Pf: Notice that gcd(Mi, mi) = 1 for 1 <= i <= r. Therefore, the yi all exist (and can be determined easily from the extended Euclidean Algorithm). Now, notice that since Miyi = 1 (mod mi), we have aiMiyi = ai (mod mi) for 1 <= i <= r. On the other hand, aiMiyi = 0 (mod mj) if j is not i (since mj | Mi in this case). Thus, we see that x = ai (mod mi) for 1 <= i <= r.

If there were two solutions, say x0, and x1, then we would have x0 - x1 = 0 (mod mi) for all i, so x0 - x1 = 0 (mod M), i.e., they are the same modulo M.

Example

Find the smallest multiple of 10 which has remainder 2 when divided by 3, and remainder 3 when divided by 7.

We are looking for a number which satisfies the congruences, x = 2 mod 3, x = 3 mod 7, x = 0 mod 2 and x = 0 mod 5. Since, 2, 3, 5 and 7 are all relatively prime in pairs, the Chinese Remainder Theorem tells us that there is a unique solution modulo 210 ( = 2x3x5x7). We calculate the Mi's and yi's as follows:
M2 = 210/2 = 105; y2 = (105)-1 (mod 2) = 1
M3 = 210/3 = 70; y3 = (70)-1 (mod 3) = 1
M5 = 210/5 = 42; y5 = (42)-1 (mod 5) = 3 and
M7 = 210/7 = 30; y7 = (30)-1 (mod 7) = 4.

So, x = 0(M2y2) + 2(M3y3) + 0(M5y5) + 3(M7y7) = 0 + 2(70)(1) + 0 + 3(30)(4) = 140 + 360 = 500 mod 210 = 80.

Remark 1: The theorem is valid in much more general situations than we have presented here.

Remark 2: The condition given is sufficient, but not necessary for a solution. Necessary and sufficient conditions exist but we are not presenting them.

Remark 3: It is purported that Sun Tsu was aware of this result in the first century A.D.