Number Theory

The Chinese Remainder Theorem and How to Apply It

Combine remainder conditions into one answer, with two solving methods, a rule for moduli that share factors and practical applications.

Key takeaways

  • For pairwise coprime moduli, any choice of remainders has exactly one solution modulo their product.
  • You can find the solution by substituting one condition into the next, or by building it from special basis numbers.
  • When moduli share factors, a solution exists only if the remainders agree modulo each common divisor.
  • Arithmetic can be carried out separately in each modulus and recombined afterward.
  • The theorem speeds up large computations, including a standard shortcut in RSA.

The Chinese remainder theorem says that if you know the remainders of an unknown number on division by several moduli, and no two moduli share a factor, then those remainders fix the number completely, up to multiples of the product of the moduli. There is always exactly one answer in that range.

It is both a puzzle solver and a working tool. It lets you split a hard calculation into small independent ones and assemble the result at the end.

The classic puzzle

An old Chinese text, the Sunzi Suanjing, written roughly 1,500 years or more ago, poses this problem in effect: a number leaves remainder 2 when divided by 3, remainder 3 when divided by 5 and remainder 2 when divided by 7. What is it?

The smallest positive answer is 23.

23 = 7 × 3 + 2
23 = 4 × 5 + 3
23 = 3 × 7 + 2

All other answers are 23 plus a multiple of 105 = 3 × 5 × 7: so 128, 233, 338 and so on. In congruence notation, x ≡ 23 (mod 105). If the notation is unfamiliar, read clocks and calendars first.

Why there is exactly one answer

Take moduli 3 and 5. Each number from 0 to 14 has a pair of remainders.

remainder mod 3 \ mod 5 0 1 2 3 4
0 0 6 12 3 9
1 10 1 7 13 4
2 5 11 2 8 14

Every cell holds exactly one number. The reason is that two numbers below 15 with the same pair differ by a multiple of both 3 and 5, and therefore of 15, so they are equal. We then have 15 numbers occupying 15 different cells, which fills the whole table.

The same counting works for any pairwise coprime moduli. It proves a solution exists without telling you how to find it quickly.

Method one: substitute step by step

Solve x ≡ 4 (mod 7) and x ≡ 3 (mod 11).

x = 4 + 7k                 from the first condition
4 + 7k ≡ 3 (mod 11)        insert into the second
7k ≡ −1 ≡ 10
inverse of 7 mod 11 is 8   since 56 ≡ 1
k ≡ 80 ≡ 3
x = 4 + 7 × 3 = 25

Check: 25 leaves remainder 4 on division by 7 and remainder 3 on division by 11. The full solution is x ≡ 25 (mod 77). With more conditions, repeat the step, using 25 + 77j as the new starting form. Each step is a linear congruence.

Method two: build basis numbers

Let M be the product of all the moduli. For each modulus m, set Mᵢ = M / m and find the inverse yᵢ of Mᵢ modulo m. The number Mᵢ × yᵢ is 1 modulo its own modulus and 0 modulo all the others. Add them with the desired remainders as weights.

For the classic puzzle, M = 105.

modulus 3:  M₁ = 35,  35 ≡ 2,  inverse 2     basis 70
modulus 5:  M₂ = 21,  21 ≡ 1,  inverse 1     basis 21
modulus 7:  M₃ = 15,  15 ≡ 1,  inverse 1     basis 15

x ≡ 2 × 70 + 3 × 21 + 2 × 15 = 233 ≡ 23 (mod 105)

The basis numbers 70, 21 and 15 can be reused for any other remainders with these moduli. That makes this method ideal when you solve many problems with the same moduli. The inverses come from the extended Euclidean algorithm.

Building a solution from basis numbers
1 Check coprimality No two moduli may share a factor 2 Multiply moduli Call the product M 3 Make basis numbers Each is 1 for its modulus, 0 for the rest 4 Weighted sum Multiply by remainders, add, reduce mod M Check coprimality No two moduli may share a factor 1 Multiply moduli Call the product M 2 Make basis numbers Each is 1 for its modulus, 0 for the rest 3 Weighted sum Multiply by remainders, add, reduce mod M 4

Which method to choose

Substitution is usually fastest by hand for two or three conditions, because you only invert one number at each step. It also adapts easily when moduli are not coprime, since the linear congruence at each step tells you whether a solution survives.

The basis method is better when the same moduli come back with new remainders, or when you want a formula to reason about. It also shows clearly that the solution depends linearly on the remainders: change one remainder by 1 and the answer shifts by that modulus's basis number.

For tiny problems there is a third approach. List the numbers satisfying the condition with the largest modulus and test each against the others. For the classic puzzle, the numbers with remainder 2 on division by 7 are 2, 9, 16, 23, and the fourth one already works.

A second worked problem

A box of tiles, fewer than 300 in all, leaves 1 over when stacked in fours, 4 over in fives and 3 over in sevens. How many tiles are there?

x ≡ 1 (mod 4),  x ≡ 4 (mod 5),  x ≡ 3 (mod 7)

x = 1 + 4k
1 + 4k ≡ 4 (mod 5)    →  4k ≡ 3  →  k ≡ 2
x = 9 + 20j
9 + 20j ≡ 3 (mod 7)   →  6j ≡ 1  →  j ≡ 6
x = 129 + 140t

Here the step 9 + 20j ≡ 3 uses 9 ≡ 2 and 20 ≡ 6 modulo 7, giving 6j ≡ 1, and 6 is its own inverse. The candidates below 300 are 129 and 269. The conditions alone cannot separate them, because they differ by 140, the product of the moduli. A fourth condition, such as divisibility by 3, would: 129 is a multiple of 3 and 269 is not.

Moduli that share factors

Suppose x ≡ 1 (mod 4) and x ≡ 2 (mod 6). The first says x is odd, the second says x is even. No solution exists.

The general rule for two conditions x ≡ a (mod m) and x ≡ b (mod n) is that a solution exists exactly when a ≡ b modulo gcd(m, n). If it does, it is unique modulo the least common multiple of m and n.

x ≡ 3 (mod 4),  x ≡ 5 (mod 6)
gcd = 2, and 3 and 5 are both odd, so solvable
list 3, 7, 11:  11 ≡ 5 (mod 6)
x ≡ 11 (mod 12)

Why is the condition necessary? If x ≡ a modulo m, then the same holds modulo any divisor of m, in particular modulo g = gcd(m, n). Likewise x ≡ b modulo g. Both statements describe the same x, so a and b must agree modulo g.

With many moduli, check every pair. If all pairs agree on their common divisors, the whole system is solvable. A practical route is to split each modulus into prime powers, then, for each prime, keep only the condition with the highest power after confirming that the lower ones are consistent with it.

For least common multiples, see where least common multiples show up.

Computing in pieces

Because the correspondence respects addition and multiplication, you can do arithmetic on the remainder lists. Using moduli 7, 11 and 13, multiply 23 by 19.

23 → (2, 1, 10)
19 → (5, 8, 6)
product, entry by entry:  (10, 8, 60) → (3, 8, 8)
check: 23 × 19 = 437 → (3, 8, 8)

The three small multiplications never interact. Since 437 is below 7 × 11 × 13 = 1,001, the list identifies it uniquely. Computer algebra systems exploit this to handle huge integers on many processors, with no carrying between pieces.

Solving equations modulo composites

To solve a polynomial congruence modulo 15, solve it modulo 3 and modulo 5 and combine every pair of solutions. For x² ≡ 1, each small modulus has two solutions, ±1, so there are four combinations.

(1, 1) → 1
(1, 4) → 4
(2, 1) → 11
(2, 4) → 14

So 1, 4, 11 and 14 all square to 1 modulo 15. This explains why composite moduli can have extra square roots, a fact that the Miller-Rabin test uses to expose them.

The theorem is also why the totient function is multiplicative for coprime numbers.

A shortcut in RSA

RSA decryption raises a number to a large power modulo n = pq. Someone who knows p and q can instead compute the power modulo each prime, with shorter exponents, and then combine the two results. Because the numbers are half as long, this is several times faster.

The shortcut needs care: if one of the two partial computations is corrupted, the faulty output can reveal a factor of n. Real libraries include protections. Treat this as an explanation, not as a recipe for secure code.

Calendars and cycles

Cycles of coprime length realign only after their product. If one task recurs every 3 days, another every 5 and a third every 7, then each combination of positions within the three cycles occurs exactly once every 105 days. Knowing where each cycle stands tells you the day number within that period.

The same idea underlies traditional calendar systems that combine two or more repeating counts to name days over a much longer span. When the cycle lengths share a factor, some combinations never occur.

The same idea for polynomials

Remainders make sense for polynomials too. Dividing a polynomial by x − r leaves its value at r, so asking for a polynomial with given values at several different points is a remainder problem with pairwise coprime moduli.

The basis method then becomes Lagrange interpolation. For each point you build a polynomial that equals 1 there and 0 at the other points, and you add these with the desired values as weights. This is the same construction as the numbers 70, 21 and 15 above, in a different setting.

For instance, the line through the points (1, 3) and (2, 5) is 3 × (2 − x) + 5 × (x − 1) = 2x + 1. The first bracket equals 1 at x = 1 and 0 at x = 2, and the second does the reverse. Schemes for sharing a secret among several people build on exactly this fact, and related schemes use integer remainders directly.

Try it yourself

Find the smallest positive number that leaves remainder 1 on division by 2, 2 on division by 3 and 3 on division by 5. Substitution gives 5 from the first two conditions, since 5 is odd and leaves remainder 2 on division by 3. Then test 5, 11, 17 and 23, stepping by 6, until the remainder on division by 5 is 3. The answer is 23, and every other solution differs from it by a multiple of 30.

Common mistakes

Forgetting to test coprimality. The product formula and the guarantee both depend on it.

Reporting one number as the only answer. The solution is a whole residue class.

Inverting the wrong quantity. In method two, invert Mᵢ modulo its own modulus m, not modulo M.

Skipping the check. Substituting the answer into every original condition takes seconds and catches almost every slip.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic