# Chinese Remainder Theorem

The Chinese remainder theorem is used to get a unique solution for an arbitrary finite number of congruences with coprime moduli, which states that:

If M = m1, m2, â€¦, mr is a set of pairwise relatively prime non-zero integers such that gcd(mi, mj) = 1 for i â‰  j, and a1, a2, â€¦, ar  Ð„ â„¤, then the system of congruences x â‰¡ a1 (mod m1), x  â‰¡ a2 (mod m2), â€¦, x â‰¡ ar (mod mr) has a unique solution in modulo M for all 1â‰¤ i â‰¤ r.

It is applied in all areas of mathematics, especially in computer science, including internet cryptography, where it is used to encode numbers based on it.

Now, let us first verify if the theorem is true for a pair of congruences.

## For Pair of Congruences

It states that if m and n are two coprime numbers, the pair of congruences x â‰¡ a (mod m) and x â‰¡ b (mod n) has a unique solution for x modulo mn, âˆ€ a, b Ð„ â„¤.

Here, we show that there is always a solution for x modulo mn, and then we verify that the solution is unique in modulo mn.

### Proof

Verifying the Existence of Solution

The first congruence is written as

x â‰¡ a (mod m) â‡’ x = a + mp, for some p Ð„ â„¤ â€¦..(i)

Using (i), we get the second congruence as x â‰¡ b (mod n)

â‡’ a + mp â‰¡ b (mod n)

On subtracting a from both sides, we get

â‡’ mp â‰¡ b – a (mod n) â€¦..(ii)

Since m and n are coprime numbers, gcd(m, n) = 1

Also, we know m (mod n) is invertible. Let mâ€™ be the inverse of m (mod n), which means mmâ€™ â‰¡ 1 (mod n)

On multiplying (ii) by mâ€™, we get

mmâ€™p â‰¡ mâ€™(b – a) (mod n) â‡’ p = mâ€™(b – a) + nq, for some q Ð„ â„¤

â‡’ x = a + mp = a + mmâ€™(b – a) + mnq

Thus, if x satisfies the given pair of congruences, it must hold the above form.

Now, let us verify this expression for every q Ð„ â„¤, which satisfies the given pair of congruences a + mmâ€™(b – a) + mnq â‰¡ a + 0 + 0 â‰¡ a (mod m) and a + mmâ€™(b – a) + mnq â‰¡ a + 1(b – a) + 0 â‰¡ b (mod n)

Verifying the Uniqueness of Solution

Assuming the solution is not unique, if x = c and x = câ€™ both satisfy x â‰¡ a (mod m) and x â‰¡ b (mod n), then we have c â‰¡ câ€™ (mod m) and c â‰¡ câ€™ (mod n).

Thus, m | (c âˆ’ câ€™) and n | (c âˆ’ câ€™).

Since gcd(m, n) = 1, the product mn divides (c âˆ’ câ€™), which means c â‰¡ câ€™ (mod mn).

This shows that all solutions to the given pair of congruences are the same in modulo mn, and thus, the solution is unique.

Now, let us verify the Chinese remainder theorem for a system of congruences.

## Proof

To prove the theorem, first, we verify that there is always a solution for x modulo mi, and then, if the solution is unique in modulo mi for all 1â‰¤ i â‰¤ r.

### Existence of Solution

Here, we prove the existence of the solution using the mathematical induction.

We have already proved the base step for r = 2

Now, let us assume all simultaneous congruences with r pairwise relatively prime moduli in the inductive part for which the statement is true.

Considering a system of simultaneous congruences with r + 1 pairwise relatively prime moduli, we get:

x â‰¡ a1 (mod m1), â€¦, x â‰¡ ar (mod mr), x â‰¡ ar+1 (mod mr+1), where (mi, mj) = 1 for all i â‰  j, and all the aiâ€™s are arbitrary.

By the inductive hypothesis, there is a solution b to the first r congruences, say b â‰¡ a1 (mod m1), b â‰¡ a2 (mod m2), â€¦, b â‰¡ ar (mod mr).

Now, from the system of two congruences, we get

x â‰¡ b (mod m1m2 â€¦ mr) and x â‰¡ ar + 1 (mod mr + 1

â‡’ x â‰¡ b (mod M) and x â‰¡ ar + 1 (mod mr + 1) â€¦..(iii)

Since (mr, mr + 1) = 1 for i = 1, 2, â€¦, r, we have gcd(m1m2 â€¦ mr, mr + 1) = 1, and the two moduli in (iii) are relatively prime.

As we know, in the case of two congruences, there is always a solution to (iii), say c.

Since c â‰¡ b (mod m1m2 â€¦ mr), we have c â‰¡ b (mod M) for i = 1, 2, . . . , r.

For any value of b, we have b â‰¡ ai (mod M) for i = 1, 2, â€¦, r.

Thus, c â‰¡ ai (mod M) for i = 1, 2, â€¦, r. Also, c â‰¡ ar + 1 (mod mr + 1).

For any value of c, we observe that c satisfies r + 1 given congruences. This concludes the inductive step, which indicates a solution exists for x modulo mi (1â‰¤ i â‰¤ r).

### Uniqueness of Solution

Assuming the solution is not unique, if x = c and x = câ€™ both satisfy x â‰¡ a1 (mod m1), x  â‰¡ a2 (mod m2), â€¦, x â‰¡ ar (mod mr), then we have c â‰¡ câ€™ (mod mi) for i = 1, 2, â€¦, r

Thus, mi | (c âˆ’ câ€™) for i = 1, 2, â€¦, r

Since all the miâ€™s are pairwise relatively prime, their product m1m2 â€¦mr divides (c âˆ’ câ€™), which means c â‰¡ câ€™ (mod m1m2 â€¦mr) â‡’ c â‰¡ câ€™ (mod M).

This shows all solutions to the given system of congruences are the same when determined by modulo m1m2 â€¦mr (that is, modulo M).

Thus, the Chinese remainder theorem is verified.

The solution to a system of congruences using the Chinese remainder theorem is found by the formula:

x â‰¡ ${\sum ^{r}_{i=1}a_{i}M_{i}x_{i}\left( modM\right)}$

Here,

• ${M_{i}=\dfrac{M}{m_{i}}}$
• xi is the modular inverse of Mi, modulo mi

## Solved Examples

Find the set of solutions for x â‰¡ 2 (mod 6) and x â‰¡ 1 (mod 7).

Solution:

Here, the given pair of congruences are x â‰¡ 2 (mod 6) and x â‰¡ 1 (mod 7).
Simplifying the given pair of congruences, we get x = 2 +6p = 1 + 7q, where p, q Ð„ â„¤.
It provides a clear solution of p and q, that is, p = 1 and q = 1.Â
Thus, one solution to the system is x = 8
Â â‡’ x â‰¡ 8 (mod 42) is the set of all solutions.

Use the Chinese Remainder Theorem to find the value of x such that:Â
x â‰¡ 3 (mod 5)Â
x â‰¡ 2 (mod 7)Â
x â‰¡ 5 (mod 9)

Solution:

Calculating M = 5 Ã— 7 Ã— 9 = 315
Calculating all the Mi, we getÂ
M1 = ${\dfrac{315}{5}}$ = 63Â
M2 = ${\dfrac{315}{7}}$ = 45
M3 = ${\dfrac{315}{9}}$ = 35
Now, we find modular inverses, as shown:
x1 â‰¡ 63-1 (mod 5)Â
Since 63 â‰¡ 3 (mod 5), thus, 3x1 â‰¡ 1 (mod 5) â‡’ x1 â‰¡ 2 (mod 5)
x2 â‰¡ 45-1 (mod 7)Â
Since 45 â‰¡ 3 (mod 7), thus 3x2 â‰¡ 1 (mod 7) â‡’ x2 â‰¡ 5 (mod 7)
x3 â‰¡ 35-1 (mod 9)
Since 35 â‰¡ 8 (mod 9), thus 8x3 â‰¡ 1 (mod 9) â‡’ x3 â‰¡ 8 (mod 9)
Now, substituting the values in the formula:
x â‰¡ (3 Ã— 63 Ã— 2 + 2 Ã— 45 Ã— 5 + 5 Ã— 35 Ã— 8) (mod 315)
â‡’ x â‰¡ (378 + 450 + 1400) (mod 315)
â‡’ x â‰¡ 2228 (mod 315)
To find the smallest positive solution, we consider x â‰¡ 2228 â‰¡ 23 (mod 315)
Thus, the solution to the system of congruences is x â‰¡ 23 (mod 315).