Sun-tzu's Theorem: Constructing and Checking Solutions of Simultaneous Congruences

The problem

An ancient Chinese text attributed to Sun-tzu (Sunzi) asks for a number that leaves remainder 2 when divided by 3, remainder 3 when divided by 5, and remainder 2 when divided by 7. In modern notation this is a system of simultaneous congruences:


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


Sun-tzu's theorem, also called the Chinese remainder theorem, tells us when such systems can be solved, how many solutions they have, and how to construct them.

Statement

Let n1, n2, …, nk be pairwise coprime positive integers (every pair has gcd 1), and let N = n1n2…nk. Then for any integers a1, …, ak, the system x ≡ ai (mod ni) has a solution, and any two solutions are congruent modulo N. In other words, the solutions form exactly one residue class modulo N.


Why uniqueness holds. If x and y both solve the system, each ni divides x − y. Pairwise coprime divisors of a number have a product that also divides it, so N divides x − y.


Why existence holds. The construction below always works when the moduli are pairwise coprime.

Method 1: the explicit construction

  1. Compute N and, for each i, Mi = N/ni. Because Mi is a product of the other moduli, gcd(Mi, ni) = 1.
  2. Find yi with Miyi ≡ 1 (mod ni), using the Euclidean algorithm or inspection.
  3. Set x = a1M1y1 + … + akMkyk and reduce modulo N.


Each term aiMiyi is ≡ ai modulo ni and ≡ 0 modulo every other modulus, because Mi contains those moduli as factors. So the sum has the right remainder for every modulus at once.


Worked example (Sun-tzu's problem). N = 3 × 5 × 7 = 105.


  • M1 = 35 ≡ 2 (mod 3); since 2 × 2 = 4 ≡ 1, y1 = 2.
  • M2 = 21 ≡ 1 (mod 5), so y2 = 1.
  • M3 = 15 ≡ 1 (mod 7), so y3 = 1.


x = 2 × 35 × 2 + 3 × 21 × 1 + 2 × 15 × 1 = 140 + 63 + 30 = 233 ≡ 23 (mod 105). Check: 23 = 7 × 3 + 2 = 4 × 5 + 3 = 3 × 7 + 2. Every solution is 23 + 105t.


A common error is to forget the inverse yi and use aiMi alone. For the ith congruence, that term has the required remainder exactly when ai(Mi − 1) ≡ 0 (mod ni).


The condition Mi ≡ 1 (mod ni) is sufficient but not necessary: ai = 0 also makes the term work.


Here a1 = 2 and M1 = 35 ≡ 2 (mod 3), so 2 × (35 − 1) = 68 is not divisible by 3; omitting y1 fails for the first congruence.

Method 2: successive substitution

Solve one congruence at a time. From x ≡ 2 (mod 3), write x = 2 + 3s. Substitute into x ≡ 3 (mod 5): 3s ≡ 1 (mod 5), so s ≡ 2 and x = 8 + 15t.


Substitute into x ≡ 2 (mod 7): 15t ≡ −6 (mod 7), that is t ≡ 1 (mod 7), so x = 23 + 105u. This method is often faster by hand, and it also handles moduli that are not coprime.

Moduli that are not coprime

If gcd(n1, n2) = d > 1, the two congruences both say something about x modulo d, and those statements must agree.


The pair x ≡ a1 (mod n1), x ≡ a2 (mod n2) is compatible if and only if d divides a1 − a2. When it is, the solutions form exactly one residue class modulo lcm(n1, n2), not modulo the product.


For more than two moduli, every pair must be compatible, and the solution is unique modulo the lcm of all the moduli.


  • Compatible example. x ≡ 5 (mod 6) and x ≡ 3 (mod 8): gcd = 2 divides 5 − 3 = 2. Substituting x = 5 + 6s gives 6s ≡ −2 (mod 8), so 3s ≡ −1 ≡ 3 (mod 4) and s ≡ 1 (mod 4). Thus x ≡ 11 (mod 24), where 24 = lcm(6, 8).
  • Incompatible example. x ≡ 1 (mod 4) and x ≡ 2 (mod 6): gcd = 2 does not divide 1 − 2 = −1. The first congruence makes x odd and the second makes it even, so there is no solution.


Pairwise coprimality is a sufficient condition for solvability, not a necessary one. A system with non-coprime moduli is not automatically unsolvable; check compatibility instead.

Splitting a modulus into independent parts

The theorem also works in reverse. If N = n1n2 with coprime factors, then choosing a residue modulo N is equivalent to choosing a residue modulo n1 and, independently, one modulo n2.


So a congruence modulo N can be solved separately modulo each factor and the answers combined: the number of solutions modulo N is the product of the numbers of solutions modulo the factors.


Example. Solve x2 ≡ x (mod 15). Modulo 3 the solutions are 0 and 1; modulo 5 they are also 0 and 1. The 2 × 2 = 4 combinations give x ≡ 0, 1, 6, 10 (mod 15). For instance, 6 ≡ 0 (mod 3) and 6 ≡ 1 (mod 5), and 62 = 36 ≡ 6 (mod 15).

Checklist

  • Check pairwise gcds. Coprime: a unique class modulo the product. Not coprime: test compatibility, and work modulo the lcm.
  • Use inverses in the construction, or substitute step by step.
  • Verify the final answer against every original congruence.
  • When asked for values in a range, list every member of the residue class in that range.


General residue arithmetic and the mechanics of the Euclidean algorithm are developed in the sibling concepts on modular arithmetic and the Euclidean algorithm.

Consider the system

x ≡ 7 (mod 10), x ≡ 2 (mod 15).

Which statement is correct?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Mark Yourself Question

  1. Grab a piece of paper and try this problem yourself.
  2. When you're done, check the "I have answered this question" box below.
  3. View the solution and report whether you got it right or wrong.

Use Sun-tzu's construction to solve

x ≡ 1 (mod 4), x ≡ 3 (mod 5), x ≡ 4 (mod 9).

Give the complete solution as a residue class, and state the least positive solution.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Mark Yourself Question

  1. Grab a piece of paper and try this problem yourself.
  2. When you're done, check the "I have answered this question" box below.
  3. View the solution and report whether you got it right or wrong.

A warehouse holds more than 500 identical boxes. When they are stacked in groups of 7, 6 boxes are left over; in groups of 8, 3 are left over; and in groups of 5, 2 are left over.

What is the smallest number of boxes consistent with this information?

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors