Sub topic 14
Combining Remainder Conditions with the Chinese Remainder Theorem
This chapter assumes you can compute remainders with congruence notation (a ≡ b mod n), reduce sums and products remainder by remainder, and factorise small numbers into primes. You also need to know that two numbers are coprime when their only common factor is 1.
The shape of the problem
CAT questions often give several remainder conditions on one unknown number:
- A number leaves remainder 2 when divided by 3, remainder 3 when divided by 5, and remainder 2 when divided by 7. Find the smallest such number.
- A number leaves remainder 4 on division by 5, 5 on division by 6 and 6 on division by 7. Find the smallest such number.
- How many numbers below 1000 leave remainder 3 on division by 4 and remainder 5 on division by 7?
Each condition alone has infinitely many solutions. Together they usually pin the answer to one residue class modulo a larger number. The Chinese Remainder Theorem (CRT) says when this happens and how large that modulus is.
The theorem in plain terms
Suppose you have the system x ≡ r₁ (mod m₁), x ≡ r₂ (mod m₂), and so on up to mₖ. If the moduli are pairwise coprime, meaning every pair shares no factor other than 1, then:
- A solution exists, whatever the remainders are.
- The solution is unique modulo M = m₁ × m₂ × … × mₖ. Exactly one solution lies in 0 to M − 1, and every other solution differs from it by a multiple of M.
For moduli 3, 5 and 7, M = 105, so the solutions are x = x₀ + 105t. That is why the search in the code below can stop at 105.
x = next(x for x in range(105) if x % 3 == 2 and x % 5 == 3 and x % 7 == 2)
print(x) # 23Brute force is fine for checking an answer. In the exam you need a method that takes under two minutes.
Method 1: successive substitution
Combine the conditions one at a time. Each combination turns two conditions into one, with the modulus multiplied.
Take x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).
- Write the first condition as x = 3a + 2.
- Substitute into the second: 3a + 2 ≡ 3 (mod 5), so 3a ≡ 1 (mod 5). Since 3 × 2 = 6 ≡ 1, a ≡ 2 (mod 5), so a = 5b + 2.
- Then x = 3(5b + 2) + 2 = 15b + 8. Now x ≡ 8 (mod 15).
- Substitute into the third: 15b + 8 ≡ 2 (mod 7). Since 15 ≡ 1 and 8 ≡ 1 (mod 7), this becomes b + 1 ≡ 2, so b ≡ 1 (mod 7), so b = 7c + 1.
- Then x = 15(7c + 1) + 8 = 105c + 23.
The answer is x ≡ 23 (mod 105), and the smallest positive solution is 23.
On the exam you can skip the algebra for the first merge. List numbers of the form (larger remainder) + (larger modulus) × k, and stop when one fits the other condition. For x ≡ 3 (mod 4) and x ≡ 5 (mod 7), list 5, 12, 19, 26, … and test each against "remainder 3 on division by 4". 19 works (19 = 16 + 3), so x ≡ 19 (mod 28). Listing is fast when the larger modulus is small, because you need at most the smaller modulus many steps.
Method 2: a constant gap makes the answer immediate
Look at the second example: remainder 4 mod 5, 5 mod 6, 6 mod 7. In each case the remainder is the divisor minus 1, so x ≡ −1 for all three moduli. Then x + 1 is divisible by 5, 6 and 7, so x + 1 is a multiple of their LCM:
- LCM(5, 6, 7) = 210.
- x = 210k − 1, so the smallest positive number is 209.
Check: 209 = 205 + 4 = 204 + 5 = 203 + 6.
The same idea works for any constant gap. If x leaves remainder 1 mod 4, 2 mod 5 and 3 mod 6, then each remainder is the divisor minus 3, so x ≡ −3 for all three. Then x + 3 is a multiple of LCM(4, 5, 6) = 60, so x = 60k − 3 and the smallest positive value is 57.
Notice that this method does not need the moduli to be coprime. 4 and 6 share a factor, and the LCM handles it. Always check for a constant gap or a constant remainder before using a general method. If every remainder is the same value r, the number is simply LCM × k + r.
Method 3: moduli that are not coprime
If two moduli share a factor, CRT in its basic form does not apply, and a solution may not exist. The test is simple. For moduli m₁ and m₂ with g = gcd(m₁, m₂), the two conditions can hold together only if r₁ ≡ r₂ (mod g). When they can, the solution is unique modulo the LCM, not the product.
- x ≡ 2 (mod 6) and x ≡ 4 (mod 8). Here g = 2, and 2 ≡ 4 (mod 2), so a solution exists. List 2, 8, 14, 20, … and test each against "remainder 4 on division by 8". 20 = 16 + 4 works, so x ≡ 20 (mod 24), where 24 = LCM(6, 8).
- x ≡ 1 (mod 4) and x ≡ 2 (mod 6). Here g = 2, but 1 and 2 differ mod 2. Any number with remainder 1 mod 4 is odd, and any number with remainder 2 mod 6 is even, so no number satisfies both.
CAT questions about non-coprime moduli usually have a consistent system, so listing works. If the question asks "how many", watch for the possibility of zero.
A general solver for checking answers
The substitution method can be written as a short loop. At each step, the current solution is x (mod M), and you add a multiple of M so that the new condition also holds. This needs the modular inverse of M modulo the new modulus, which pow(M, -1, m) gives in Python 3.8 and later. It works only when the moduli are pairwise coprime.
def crt(remainders, moduli):
x, M = 0, 1
for r, m in zip(remainders, moduli):
t = ((r - x) * pow(M, -1, m)) % m
x += M * t
M *= m
return x, M
print(crt([2, 3, 2], [3, 5, 7])) # (23, 105)
print(crt([3, 5], [4, 7])) # (19, 28)Use this to verify your hand answers while practising. You cannot use it in the exam.
From a residue class to exam answers
Once you have x ≡ x₀ (mod M), the typical follow-up questions are easy:
- Smallest positive number: x₀ (or M if x₀ = 0).
- Remainder when divided by M: x₀. If the question divides by a factor of M, reduce x₀ by that factor. In the first example, x = 23 leaves remainder 8 when divided by 15.
- How many such numbers up to N: the solutions are x₀, x₀ + M, x₀ + 2M, …, so count them as ⌊(N − x₀) / M⌋ + 1 when N ≥ x₀. For the numbers below 1000 with x ≡ 19 (mod 28): the largest is 19 + 28 × 35 = 999, so there are 36 of them (k = 0 to 35).
- The largest number below N: subtract from N until the residue matches.
Choosing between methods
The diagram summarises the order in which to check.
| Situation | Method | Modulus of the answer |
|---|---|---|
| Same remainder for every divisor | LCM × k + r | LCM |
| Constant gap (x ≡ −g for all) | LCM × k − g | LCM |
| Pairwise coprime moduli | Substitution or listing | Product |
| Moduli share factors | Check gcd consistency, then list | LCM |
Common errors
- Forgetting that the answer is a residue class. Reporting 23 and then listing 128 as another solution is fine, but listing 23 + 15 = 38 is wrong because the modulus is 105, not 15.
- Using the product as the modulus when the moduli are not coprime. For 6 and 8, the period is 24, not 48.
- Missing the constant gap because the remainders look different. Always compare each remainder with its divisor.
- Mixing up "divided by" and "multiplied by" when substituting. Write x = mk + r each time so the roles stay clear.
Exercise
A number leaves remainder 1 when divided by 4, remainder 2 when divided by 5, and remainder 3 when divided by 7. What is the smallest positive such number, and what is the remainder when it is divided by 140?
Show answer
Merge the first two conditions. List numbers with remainder 2 on division by 5: 2, 7, 12, 17. The number 17 leaves remainder 1 on division by 4, so x ≡ 17 (mod 20). Write x = 20k + 17 and require remainder 3 on division by 7. Since 20 ≡ 6 ≡ −1 and 17 ≡ 3 (mod 7), this gives −k + 3 ≡ 3, so k ≡ 0 (mod 7). The smallest solution is x = 17, and x ≡ 17 (mod 140). The remainder when divided by 140 is 17.
Exercise
How many positive integers below 500 leave remainder 3 when divided by 6 and remainder 5 when divided by 8? If a number leaves remainder 3 mod 6 and remainder 4 mod 8, does one exist?
Show answer
For the first system, gcd(6, 8) = 2 and 3 ≡ 5 (mod 2), so a solution exists with period LCM = 24. List numbers with remainder 5 on division by 8: 5, 13, 21. Of these, 21 = 18 + 3 leaves remainder 3 on division by 6, so x ≡ 21 (mod 24). The numbers are 21, 45, …, and 21 + 24 × 19 = 477 is the largest below 500, so there are 20. For the second system, 3 and 4 differ mod 2, so no such number exists: a number with remainder 3 mod 6 is odd, and a number with remainder 4 mod 8 is even.