Chinese Remainder Theorem

The Chinese remainder theorem says that if the moduli n1, n2, …, nk are pairwise relatively prime, then the system x ≡ a1 (mod n1), …, x ≡ ak (mod nk) has exactly one solution modulo N = n1n2⋯nk. You can find it by back substitution or with a formula that uses modular inverses.

Before this lesson: Linear Congruences and Modular Arithmetic

The theorem

A number that leaves remainder 2 when divided by 3 and remainder 3 when divided by 5 is pinned down mod 15: it must be 8, 23, 38, and so on. The moduli 3 and 5 share no factor, so knowing both remainders is the same as knowing the remainder mod 15. Integers are pairwise relatively prime when every pair of them has gcd 1.

\begin{cases} x \equiv a_1 \pmod{n_1} \\ \quad \vdots \\ x \equiv a_k \pmod{n_k} \end{cases} \text{ has exactly one solution mod } N = n_1 n_2 \cdots n_k

The theorem dates back to the Chinese mathematician Sunzi, around the 3rd to 5th century CE, who asked for a number with remainders 2, 3 and 2 when divided by 3, 5 and 7.

A table with 3 rows for x mod 3 = 0, 1, 2 and 5 columns for x mod 5 = 0 to 4. Each of the 15 cells holds the one number from 0 to 14 with that pair of remainders. Row 0 reads 0, 6, 12, 3, 9; row 1 reads 10, 1, 7, 13, 4; row 2 reads 5, 11, 2, 8, 14. The cell for remainders 2 and 3 holds 8 and is highlighted.
Every pair of remainders mod 3 and mod 5 occurs exactly once among 0 to 14, which is the Chinese remainder theorem for the moduli 3 and 5.

Why the solution is unique. If x and x′ both solve the system, then each ni divides x − x′. Since the ni share no prime factors, their product N divides x − x′, so x ≡ x′ (mod N).

The construction formula

  1. Compute N = n1n2⋯nk and, for each i, Ni = N / ni.
  2. Find yi, the inverse of Ni mod ni. It exists because gcd(Ni, ni) = 1.
  3. Add the products: x = a1N1y1 + a2N2y2 + ⋯ + akNkyk.
  4. Reduce x mod N.
x \equiv \sum_{i=1}^{k} a_i N_i y_i \pmod{N}, \qquad N_i y_i \equiv 1 \pmod{n_i}

It works because Nj is a multiple of ni for every j ≠ i, so mod ni every term vanishes except aiNiyi ≡ ai · 1. Back substitution, shown in Example 3, often uses smaller numbers.

Chinese remainder theorem examples

Example 1: Two congruences by listing

Solve x ≡ 2 (mod 3) and x ≡ 3 (mod 5).

\gcd(3, 5) = 1

The moduli are relatively prime, so there is one solution mod 15.

3, 8, 13

List the numbers below 15 that are ≡ 3 (mod 5).

3 \equiv 0,\ 8 \equiv 2,\ 13 \equiv 1 \pmod{3}

Reduce each one mod 3.

x \equiv 8 \pmod{15}

Only 8 also leaves remainder 2 mod 3.

Answer: x ≡ 8 (mod 15). Listing is quick when one modulus is small, and the table above shows 8 in the cell for remainders 2 and 3.

Example 2: Sunzi’s problem with the formula

Solve x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7).

N = 3 \cdot 5 \cdot 7 = 105

The moduli are pairwise relatively prime, so multiply them.

N_1 = 35,\ N_2 = 21,\ N_3 = 15

Divide N by each modulus.

35 \equiv 2 \pmod{3},\ 2 \cdot 2 = 4 \equiv 1

Find y1: reduce N1 mod 3, then 2 is its inverse, so y1 = 2.

21 \equiv 1 \pmod{5}

So y2 = 1.

15 \equiv 1 \pmod{7}

So y3 = 1.

x = 2 \cdot 35 \cdot 2 + 3 \cdot 21 \cdot 1 + 2 \cdot 15 \cdot 1

Substitute into the formula.

x = 140 + 63 + 30 = 233

Multiply each product, then add.

233 = 105 \cdot 2 + 23

Reduce mod 105.

Answer: x ≡ 23 (mod 105). Check: 23 = 3 · 7 + 2, 23 = 5 · 4 + 3 and 23 = 7 · 3 + 2.

Example 3: Back substitution with three moduli

Solve x ≡ 4 (mod 7), x ≡ 6 (mod 11) and x ≡ 1 (mod 4).

x = 4 + 7k

Write every solution of the first congruence, with k an integer.

4 + 7k \equiv 6 \pmod{11}

Substitute into the second congruence.

7k \equiv 2 \pmod{11}

Subtract 4 from both sides.

7 \cdot 8 = 56 \equiv 1 \pmod{11}

8 is the inverse of 7 mod 11 (see linear congruences).

k \equiv 8 \cdot 2 = 16 \equiv 5 \pmod{11}

Multiply both sides by 8, then reduce.

k = 5 + 11j

Write every such k, with j an integer.

x = 4 + 7(5 + 11j)

Substitute k = 5 + 11j into x = 4 + 7k.

x = 4 + 35 + 77j

Distribute the 7.

x = 39 + 77j

Add 4 + 35.

39 + 77j \equiv 1 \pmod{4}

Substitute into the third congruence.

3 + j \equiv 1 \pmod{4}

Reduce: 39 ≡ 3 and 77 ≡ 1.

j \equiv -2 \equiv 2 \pmod{4}

Subtract 3, then add 4.

x = 39 + 77 \cdot 2 = 193

Use j = 2.

Answer: x ≡ 193 (mod 308), since 7 · 11 · 4 = 308. Check: 193 = 7 · 27 + 4 = 11 · 17 + 6 = 4 · 48 + 1.

Example 4: Moduli that are not relatively prime

Decide whether x ≡ 3 (mod 6) and x ≡ 4 (mod 8) has a solution. Then solve x ≡ 3 (mod 6) and x ≡ 5 (mod 8).

x = 3 + 6k = 2(3k + 1) + 1

Every solution of the first congruence is odd.

x = 4 + 8j = 2(2 + 4j)

Every solution of x ≡ 4 (mod 8) is even.

\text{no solution}

No integer is both odd and even.

3 + 6k \equiv 5 \pmod{8}

For the second system, substitute x = 3 + 6k.

6k \equiv 2 \pmod{8}

Subtract 3. gcd(6, 8) = 2 divides 2, so this is solvable.

3k \equiv 1 \pmod{4}

Divide a, b and the modulus by 2.

k \equiv 3 \pmod{4}

3 · 3 = 9 ≡ 1 (mod 4), so multiply both sides by 3.

x = 3 + 6 \cdot 3 = 21

Use k = 3.

Answer: the first system has no solution. The second has x ≡ 21 (mod 24), unique mod lcm(6, 8) = 24, not mod 48. In general, x ≡ a (mod m) and x ≡ b (mod n) is solvable exactly when a ≡ b (mod gcd(m, n)), and then the solution is unique mod the least common multiple of m and n.

Example 5: Counting eggs (word problem)

A farmer has fewer than 100 eggs. Counting them by 3s leaves 1, by 4s leaves 2, and by 5s leaves 3. How many eggs are there?

x \equiv 1 \pmod{3},\ x \equiv 2 \pmod{4},\ x \equiv 3 \pmod{5}

Write the system. 3, 4 and 5 are pairwise relatively prime, so N = 60.

x = 3 + 5k

Write every solution of the third congruence.

3 + 5k \equiv 2 \pmod{4}

Substitute into the second congruence.

3 + k \equiv 2 \pmod{4}

Reduce 5 ≡ 1 (mod 4).

k \equiv -1 \equiv 3 \pmod{4}

Subtract 3, then add 4.

x = 3 + 5(3 + 4j)

Substitute k = 3 + 4j.

x = 3 + 15 + 20j

Distribute the 5.

x = 18 + 20j

Add 3 + 15.

18 + 20j \equiv 1 \pmod{3}

Substitute into the first congruence.

2j \equiv 1 \pmod{3}

Reduce: 18 ≡ 0 and 20 ≡ 2.

j \equiv 2 \pmod{3}

Multiply both sides by 2, the inverse of 2 mod 3, since 2 · 2 = 4 ≡ 1.

x = 18 + 20 \cdot 2 = 58

Use j = 2.

Answer: 58 eggs. The next solution, 58 + 60 = 118, is too many. A shortcut: each remainder is 2 less than the divisor, so x + 2 is a multiple of 3, 4 and 5, which makes x + 2 = 60.

Common mistakes

MistakeExample of the errorFix
Using the theorem when moduli share a factor“x ≡ 3 (mod 6) and x ≡ 5 (mod 8) has one solution mod 48.”Check gcds first. Here the solution is unique mod lcm(6, 8) = 24.
Leaving out the inverses in the formulax = 2 · 35 + 3 · 21 + 2 · 15 = 163Include yi: x = 2 · 35 · 2 + 3 · 21 · 1 + 2 · 15 · 1 = 233 ≡ 23.
Finding yi mod the wrong numberInverting 35 mod 105Invert Ni mod its own modulus ni: 35 mod 3.
Solving for k mod the wrong modulusFrom 7k ≡ 2 (mod 11), writing k ≡ … (mod 7)k is found mod the modulus of the congruence it appears in: mod 11.
Giving one number instead of a class“The solution is 23.”State the class: x ≡ 23 (mod 105), which includes 128, 233, …
Skipping the final checkAn arithmetic slip goes unnoticedReduce the answer mod each ni to confirm every remainder.

Related ideas

The theorem says that remainders mod 3 and mod 5 work exactly like remainders mod 15, so a big modulus can be split into small ones. RSA Encryption uses this to speed up decryption, and Fermat’s Little Theorem handles the powers that appear in each small modulus. Each step of back substitution is one linear congruence, solved with an inverse from the Euclidean algorithm.

Practice problems

  1. Solve x ≡ 1 (mod 4) and x ≡ 3 (mod 5).
  2. Solve x ≡ 2 (mod 5) and x ≡ 4 (mod 9).
  3. Use the formula to solve x ≡ 1 (mod 2), x ≡ 2 (mod 3) and x ≡ 3 (mod 5).
  4. Solve x ≡ 3 (mod 7), x ≡ 5 (mod 8) and x ≡ 0 (mod 3).
  5. Does x ≡ 1 (mod 4) and x ≡ 2 (mod 6) have a solution?
  6. Solve x ≡ 1 (mod 4) and x ≡ 3 (mod 6).
  7. Use the theorem to find 2100 mod 15.
  8. How many integers from 0 to 999 solve Sunzi’s system in Example 2?
  9. Explain why x ≡ a (mod 4) and x ≡ b (mod 9) has exactly one solution mod 36 for every a and b.
  10. A basket of eggs leaves 1 egg over when they are taken out 2, 3, 4, 5 or 6 at a time, and none over when they are taken out 7 at a time. What is the smallest possible number of eggs?
Show answers

1.

3, 8, 13, 18

List the numbers below 20 that are ≡ 3 (mod 5).

13 = 4 \cdot 3 + 1

Only 13 leaves remainder 1 mod 4 (3, 8 and 18 leave 3, 0 and 2).

Answer: x ≡ 13 (mod 20)

2.

x = 2 + 5k

Write every solution of the first congruence.

5k \equiv 2 \pmod{9}

Substitute into the second and subtract 2.

k \equiv 2 \cdot 2 = 4 \pmod{9}

5 · 2 = 10 ≡ 1, so multiply both sides by 2.

x = 2 + 5 \cdot 4 = 22

Use k = 4.

Answer: x ≡ 22 (mod 45)

3.

N = 30;\ N_1 = 15,\ N_2 = 10,\ N_3 = 6

Multiply the moduli, then divide by each one.

15 \equiv 1,\ 10 \equiv 1,\ 6 \equiv 1

Each Ni is 1 mod its own modulus, so every yi = 1.

x = 1 \cdot 15 + 2 \cdot 10 + 3 \cdot 6 = 53

Substitute into the formula.

53 = 30 + 23

Reduce mod 30.

Answer: x ≡ 23 (mod 30)

4.

x = 3 + 7k

Write every solution of the first congruence.

7k \equiv 2 \pmod{8}

Substitute into the second and subtract 3.

-k \equiv 2 \pmod{8}

Reduce 7 ≡ −1.

k \equiv -2 \equiv 6 \pmod{8}

Multiply by −1, then add 8.

x = 3 + 7(6 + 8j)

Substitute k = 6 + 8j.

x = 3 + 42 + 56j

Distribute the 7.

x = 45 + 56j

Add 3 + 42.

45 + 56j \equiv 2j \equiv 0 \pmod{3}

Substitute into the third: 45 ≡ 0 and 56 ≡ 2.

j \equiv 0 \pmod{3}

Multiply by 2, the inverse of 2 mod 3.

Answer: x ≡ 45 (mod 168)

5.

x = 1 + 4k \text{ is odd}

Every solution of the first congruence is odd.

x = 2 + 6j \text{ is even}

Every solution of the second is even.

Answer: no solution. Equivalently, 1 ≢ 2 (mod gcd(4, 6) = 2)

6.

1 \equiv 3 \pmod{2}

Check compatibility: gcd(4, 6) = 2, and 3 − 1 = 2 is even.

1 + 4k \equiv 3 \pmod{6}

Substitute x = 1 + 4k.

4k \equiv 2 \pmod{6}

Subtract 1.

2k \equiv 1 \pmod{3}

Divide a, b and the modulus by 2.

k \equiv 2 \pmod{3}

2 is its own inverse mod 3.

x = 1 + 4 \cdot 2 = 9

Use k = 2.

Answer: x ≡ 9 (mod 12)

7.

2^{100} \equiv (-1)^{100} = 1 \pmod{3}

Reduce 2 ≡ −1 mod 3.

2^4 = 16 \equiv 1 \pmod{5}

Find a power of 2 that is 1 mod 5.

2^{100} = (2^4)^{25} \equiv 1 \pmod{5}

Split the power.

x \equiv 1 \pmod{3},\ x \equiv 1 \pmod{5}

Combine: x = 1 solves both.

Answer: 2100 ≡ 1 (mod 15)

8.

x = 23 + 105t

Every solution, from Example 2.

23 + 105 \cdot 9 = 968 \le 999

t = 9 still fits.

23 + 105 \cdot 10 = 1073 \gt 999

t = 10 is too big.

Answer: 10 integers, for t = 0, 1, …, 9

9.

\gcd(4, 9) = 1

4 = 2² and 9 = 3² share no prime.

Answer: the moduli are relatively prime, so the Chinese remainder theorem gives exactly one solution mod 4 · 9 = 36

10.

x - 1 \equiv 0 \pmod{60}

x − 1 is a multiple of 2, 3, 4, 5 and 6, so of lcm = 60.

x = 1 + 60k

Write every such x.

60k \equiv -1 \pmod{7}

x must be a multiple of 7.

4k \equiv 6 \pmod{7}

Reduce: 60 ≡ 4 and −1 ≡ 6.

k \equiv 2 \cdot 6 = 12 \equiv 5 \pmod{7}

4 · 2 = 8 ≡ 1, so multiply by 2, then reduce.

x = 1 + 60 \cdot 5 = 301

Use k = 5.

Answer: 301 eggs (and 301 = 7 · 43)

Chinese remainder theorem FAQ

What does the Chinese remainder theorem say?

If the moduli are pairwise relatively prime, a system of congruences x ≡ ai (mod ni) has exactly one solution modulo the product of the moduli.

How do you solve a system of congruences?

Write the solutions of one congruence as x = a + nk, substitute into the next, solve for k, and repeat. Or use the formula x = Σ aiNiyi with Ni = N/ni and yi the inverse of Ni mod ni.

What if the moduli are not relatively prime?

Then the system may have no solution. Two congruences x ≡ a (mod m) and x ≡ b (mod n) are solvable exactly when a ≡ b (mod gcd(m, n)), and the solution is then unique mod lcm(m, n).

What is the Chinese remainder theorem used for?

It splits work with a large modulus into several small moduli. It speeds up RSA decryption, helps compute large powers mod n and appears in scheduling problems where several cycles must line up.

Summary

  • If n1, …, nk are pairwise relatively prime, the system x ≡ ai (mod ni) has one solution mod N = n1⋯nk.
  • Back substitution: write x = a + nk, substitute into the next congruence and solve for k.
  • Formula: x ≡ Σ aiNiyi (mod N), where yi inverts Ni = N/ni mod ni.
  • With moduli that share a factor, check a ≡ b (mod gcd) first; the answer is unique mod the lcm.
  • Always state the answer as a residue class and check it against every congruence.

Previous lesson: Linear Congruences

Next lesson: Fermat’s Little Theorem

Back to the Discrete Math Index