MathsNumber Theory

Chinese Remainder Theorem
calculator.

Find the number that leaves the right remainder for every modulus at once.

How it's worked out

A congruence such as x≡2(mod3)x \equiv 2 \pmod 3 says that x leaves 2 over when you divide it by 3. The number on the right, 3 here, is the modulus. A system asks for one x that meets every condition.

The theorem. When every pair of moduli has no common factor, the system has exactly one solution between 0 and M − 1, where M is the product of the moduli. Raji's text/03%3A_Congruences/3.04%3A_The_Chinese_Remainder_Theorem) proves it by building x from one term per congruence.

Mi\displaystyle M_i=Mmi\displaystyle {}= \frac{M}{m_i}
Miyi\displaystyle M_i \, y_i≡1(modmi)\displaystyle {}\equiv 1 \pmod{m_i}
x\displaystyle x≡a1M1y1+⋯+akMkyk(modM)\displaystyle {}\equiv a_1 M_1 y_1 + \cdots + a_k M_k y_k \pmod M

Each term is a multiple of every modulus but its own, and it leaves aia_i for its own. So the sum leaves the right remainder everywhere. The inverse yiy_i comes from the extended Euclidean algorithm.

Merging, one congruence at a time, is how the working here runs, and it is the method the Waterloo Math 135 notes teach. Write the first as x = a + mt, put that into the next one, solve for t, and repeat. Their example x ≡ 5 (mod 6), 2 (mod 7), 3 (mod 11) comes out as x ≡ 443 (mod 462).

When moduli share a factor, merging still works, but a solution may not exist. Two conditions agree only if their remainders match modulo the GCD of their moduli. When every pair agrees, the answer repeats every LCM, not every product, as Zhang states in Theorem 2.

Worked examples

Sunzi counts the soldiers

The oldest known case is in the Sunzi Suanjing, written some time from the 3rd to the 5th century. A number leaves 2 over in threes, 3 over in fives and 2 over in sevens. The answer is x ≡ 23 (mod 105), so 23, 128, 233 and so on all work.

By the formula, M = 105 and the three terms are 35, 63, 30. They add to 128, which leaves 23 after dividing by 105. Merging gets there too: the first two give x ≡ 8 (mod 15), and adding 2 (mod 7) gives 23.

Open this example in the tool

A trader counts eggs in a basket

An old puzzle: eggs counted out by 2, 3, 4, 5 or 6 leave 1 over, but by 7 leave none. The moduli 2, 4 and 6 share factors, so the formula above does not apply. The remainders all agree, though, so merging works.

The first five conditions only say x ≡ 1 (mod 60), since 60 is the LCM of 2 to 6. Adding the sevens gives x ≡ 301 (mod 420). The smallest basket holds 301 eggs, and the next holds 721.

Open this example in the tool

Jo lines up two rotas

Jo's gym class falls on days that leave 3 over in fours, and her free days leave 5 over in sixes. GCD(4, 6) = 2, and both 3 and 5 are odd, so they agree. They meet on day 11, then every 12 days, which is the LCM, not the product 24.

If the class were on days that leave 1 over in fours, and the free days left 2 over in sixes, they would never meet. One rule makes the day odd and the other makes it even. The tool names the clash: m1 = 4 and m2 = 6 share the factor 2, but 1 and 2 leave different remainders mod 2.

Open this example in the tool

Reading your result

The answer is a family. x ≡ 23 (mod 105) means 23, 128, 233, and also -82 on the negative side. The tool gives the smallest one that is 0 or more, and M, the gap between solutions.

The sieve. Each row marks the numbers that meet one condition, so its marks sit m apart. The shaded columns have a mark in every row: those are the solutions, M apart. When M is small, the picture starts at 0. When it is large, the picture shows a window around the answer.

Check any number. Type a number into the test box to see what it leaves for each modulus. For Sunzi's system, 128 leaves 2, 3, 2 for 3, 5 and 7, which matches, so 128 is in the family.

Big moduli are exact. Three primes just over a million, with remainders 1, 2 and 3, give M = 1,000,073,001,431,003,663 and x = 911,341,040,519,919,516. Both are past what a double holds, so a calculator that uses doubles would get the last digits wrong.

Common mistakes

  • Using the product when moduli share a factor. For 3 (mod 4) and 5 (mod 6) the answer repeats every 12, not 24. The product is right only when no two moduli share a factor.
  • Leaving a remainder negative. x ≡ −1 (mod 5) is the same as x ≡ 4 (mod 5). The tool reduces it for you, but a hand answer of −1 may be marked wrong.
  • Giving up when an inverse does not exist. With shared factors there is no inverse of MiM_i, so the formula fails, but a solution may still exist. Merging handles it: the Waterloo example x ≡ 4 (mod 6), 2 (mod 8) gives x ≡ 10 (mod 24).
  • Skipping the check. A slip in the last line of hand working is easy to make. Put your answer back into every congruence before you write it down.

Questions people ask

First check each pair: their remainders must leave the same value when divided by the GCD of the two moduli. If any pair fails, there is no solution. If all pass, merge the congruences one at a time, and the answer repeats every LCM of the moduli.

Sources