Chinese Remainder Theorem
calculator.
Find the number that leaves the right remainder for every modulus at once.
- Repeats every
- 105
- Moduli
- Coprime
- Conditions
- 3
The moduli share no factor, so exactly one remainder out of every 105 works, and the classical formula applies.
Where the congruences line up
Each row marks the numbers from 0 to 59 that leave the right remainder for one modulus. Shaded columns have a mark in every row: 23.
Working, one congruence at a time
- 1Start with the first congruencex ≡ 2 (mod 3)
- 2Add x ≡ 3 (mod 5)Write x = 2 + 3t. Then 3t ≡ 1 (mod 5); the inverse of 3 mod 5 is 2, so t ≡ 2. x ≡ 8 (mod 15)
- 3Add x ≡ 2 (mod 7)Write x = 8 + 15t. Then 15t ≡ 1 (mod 7); the inverse of 1 mod 7 is 1, so t ≡ 1. x ≡ 23 (mod 105)
- 4Check: put the answer back into every congruence23 mod 3 = 2, 23 mod 5 = 3, 23 mod 7 = 2
Check any number
- 2026 mod 3 = 1, needs 2
- 2026 mod 5 = 1, needs 3
- 2026 mod 7 = 3, needs 2
Terms that add up to x, mod 105
| m | a | M ÷ m | Inverse | Term |
|---|---|---|---|---|
| 3a = 2 | 2 | 35inverse 2 | 2 | 35 |
| 5a = 3 | 3 | 21inverse 1 | 1 | 63 |
| 7a = 2 | 2 | 15inverse 1 | 1 | 30 |
Each term is a times M ÷ m times its inverse. It leaves a for its own modulus and 0 for the others, so the terms add up to x mod 105.
How it's worked out
A congruence such as 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.
Each term is a multiple of every modulus but its own, and it leaves for its own. So the sum leaves the right remainder everywhere. The inverse 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.
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.
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.
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 , 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
Sources
- Raji, Elementary Number Theory (LibreTexts), section 3.4, The Chinese Remainder Theorem
- Barrus and Clark, Elementary Number Theory (LibreTexts), section 1.23, Chinese Remainder Theorem
- University of Waterloo, Math 135, Lecture 31 notes
- Zhifang Zhang, Network Coding Based on Chinese Remainder Theorem (arXiv 1208.3966), Theorem 2 and Corollary 1