## Chinese Remainder Theorem

**Theorem**: Suppose that m_{1}, m_{2}, ..., m_{r} are pairwise relatively prime positive integers, and let a_{1}, a_{2}, ..., a_{r} be integers. Then the system of congruences, x = a_{i} (mod m_{i}) for 1 <= i <= r, has a unique solution modulo M = m_{1} x m_{2} x ... x m_{r}, which is given by:

x = a_{1}M_{1}y_{1} + a_{2}M_{2}y_{2} + ... + a_{r}M_{r}y_{r} (mod M),
where M_{i} = M/m_{i} and y_{i} = (M_{i})^{-1} (mod m_{i}) for 1 <= i <= r.
*Pf*: Notice that gcd(M_{i}, m_{i}) = 1 for 1 <= i <= r. Therefore, the y_{i} all exist (and can be determined easily from the extended Euclidean Algorithm). Now, notice that since M_{i}y_{i} = 1 (mod m_{i}), we have a_{i}M_{i}y_{i} = a_{i} (mod m_{i}) for 1 <= i <= r. On the other hand, a_{i}M_{i}y_{i} = 0 (mod m_{j}) if j is not i (since m_{j} | M_{i} in this case). Thus, we see that x = a_{i} (mod m_{i}) for 1 <= i <= r.

If there were two solutions, say x_{0}, and x_{1}, then we would have x_{0} - x_{1} = 0 (mod m_{i}) for all i, so x_{0} - x_{1} = 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 M_{i}'s and y_{i}'s as follows:

M_{2} = 210/2 = 105; y_{2} = (105)^{-1} (mod 2) = 1

M_{3} = 210/3 = 70; y_{3} = (70)^{-1} (mod 3) = 1

M_{5} = 210/5 = 42; y_{5} = (42)^{-1} (mod 5) = 3 and

M_{7} = 210/7 = 30; y_{7} = (30)^{-1} (mod 7) = 4.

So, x = 0(M_{2}y_{2}) + 2(M_{3}y_{3}) + 0(M_{5}y_{5}) + 3(M_{7}y_{7}) = 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.