Discrete MathematicsModular Arithmetic and Cryptography4 of 6
Modular Arithmetic and Cryptography · 4

The Chinese Remainder Theorem

We know how to solve a single congruence, and now we want to find a single integer xx that satisfies several congruences with different moduli at the same time.

1

Sunzi's Remainder Puzzle

We want to find a mystery integer xx that leaves remainder 2\clueOne{2} when divided by 3\clueOne{3}, remainder 3\clueTwo{3} when divided by 5\clueTwo{5}, and remainder 2\clueThree{2} when divided by 7\clueThree{7}:

x≡2(mod3),x≡3(mod5),x≡2(mod7)\displaystyle x \equiv \clueOne{2 \pmod{3}}, \qquad x \equiv \clueTwo{3 \pmod{5}}, \qquad x \equiv \clueThree{2 \pmod{7}}
Sunzi's Remainder Puzzle
??
groups of 3\text{groups of } \htmlClass{lc-blue}{3}
2 left over\htmlClass{lc-blue}{2} \text{ left over}
groups of 5\text{groups of } \htmlClass{lc-pink}{5}
3 left over\htmlClass{lc-pink}{3} \text{ left over}
groups of 7\text{groups of } \htmlClass{lc-violet}{7}
2 left over\htmlClass{lc-violet}{2} \text{ left over}

Guessing and checking works when the moduli are tiny, but it stalls as soon as the moduli get large. To build xx directly out of the clues, we first need to see how a pair of remainders behaves as we count up from 00.

2

Remainder Cycles and Pairwise Coprime Moduli

Before we try to solve a system of congruences, we need to know when a system is guaranteed to hit every combination of remainders.

Remainder Cycles and Pairwise Coprime Moduli
x=0,1,2,…x = 0, 1, 2, \ldots
00
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
mod 3\htmlClass{lc-blue}{\text{mod } 3}
00
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
mod 5\htmlClass{lc-pink}{\text{mod } 5}
0\htmlClass{lc-blue}{0}
1\htmlClass{lc-blue}{1}
2\htmlClass{lc-blue}{2}
0\htmlClass{lc-pink}{0}
1\htmlClass{lc-pink}{1}
2\htmlClass{lc-pink}{2}
3\htmlClass{lc-pink}{3}
4\htmlClass{lc-pink}{4}

For mod 3\clueOne{\text{mod } 3} and mod 5\clueTwo{\text{mod } 5}, there are 3×5=15\clueOne{3} \times \clueTwo{5} = \cycle{15} possible remainder pairs, and counting from 00 to 1414 hits every pair once before both moduli reset to (0,0)(\clueOne{0}, \clueTwo{0}) at 15\cycle{15}. When two moduli share a factor, such as mod 4\clueOne{\text{mod } 4} and mod 6\clueTwo{\text{mod } 6} where gcd⁡(4,6)=2\gcd(\clueOne{4}, \clueTwo{6}) = \shared{2}, their cycles lock in step and reset together early at 12\cycle{12} instead of 4×6=24\clueOne{4} \times \clueTwo{6} = \wrong{24}, hitting half the remainder pairs twice and skipping the other half completely.

For a system with three or more moduli m1\clueOne{m_1}, m2\clueTwo{m_2}, …\dots, mkm_k, every pair of moduli must have no common factors:

gcd⁡(mi,mj)=1⏟pairwise coprimefor all i≠j\displaystyle \underbrace{\gcd(\clueOne{m_i}, \clueTwo{m_j}) = \coprime{1}}_{\text{pairwise coprime}} \quad \text{for all } i \neq j

Whenever m1\clueOne{m_1} and m2\clueTwo{m_2} are coprime, the wrapping diagonal path visits every remainder pair (x mod m1,x mod m2)(x \clueOne{\bmod m_1}, x \clueTwo{\bmod m_2}) in the grid once before resetting at m1×m2\clueOne{m_1} \times \clueTwo{m_2}; whenever m1\clueOne{m_1} and m2\clueTwo{m_2} share a factor, the path returns to (0,0)(\clueOne{0}, \clueTwo{0}) early and leaves whole diagonal stripes of remainder pairs unvisited.

Counting up steps diagonally across the remainder grid, visiting every pair when the moduli are coprime and looping back to the start early when they share a factor.
3

Building Unit Switches

Because 3\clueOne{3}, 5,\clueTwo{5}, and 7\clueThree{7} are pairwise coprime, Sunzi's puzzle is guaranteed to have a solution in the window 0≤x≤1040 \le x \le 104:

x≡2(mod3),x≡3(mod5),x≡2(mod7)\displaystyle x \equiv \clueOne{2 \pmod{3}}, \qquad x \equiv \clueTwo{3 \pmod{5}}, \qquad x \equiv \clueThree{2 \pmod{7}}

To adjust one remainder without disturbing the other two, we start with a multiple of the other two moduli and tune it so that it leaves remainder 11 in the current modulus. That tuning step uses a modular inverse: a multiplier yy satisfying a⋅y≡1(modn)a \cdot y \equiv 1 \pmod{n}, which is guaranteed to exist whenever gcd⁡(a,n)=1\gcd(a, n) = \coprime{1}.

Building Unit Switches
x≡2  (mod 3)x \equiv \htmlClass{lc-blue}{2 \; (\text{mod } 3)}
x≡3  (mod 5)x \equiv \htmlClass{lc-pink}{3 \; (\text{mod } 5)}
x≡2  (mod 7)x \equiv \htmlClass{lc-violet}{2 \; (\text{mod } 7)}
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
5\htmlClass{lc-mute}{5}
6\htmlClass{lc-mute}{6}
0≤x≤1040 \le x \le 104
??
Text solution
Build the (1,0,0)(\clueOne{1}, \clueTwo{0}, \clueThree{0}) switch for mod 3\clueOne{\text{mod } 3}Multiply the other two moduli so the number is 00 in mod 5\clueTwo{\text{mod } 5} and mod 7\clueThree{\text{mod } 7}, then multiply by the inverse of 35 mod 335 \clueOne{\bmod 3} to tune its mod 3\clueOne{\text{mod } 3} remainder to 11.
5×7\displaystyle \clueTwo{5} \times \clueThree{7} =35≡2(mod3),2⋅2⏟y1\displaystyle = 35 \equiv 2 \clueOne{\pmod{3}}, \qquad 2 \cdot \underbrace{2}_{y_1} =4≡1(mod3)  ⟹  35×2\displaystyle = 4 \equiv 1 \clueOne{\pmod{3}} \implies 35 \times 2 =70\displaystyle = 70
Build the (0,1,0)(\clueOne{0}, \clueTwo{1}, \clueThree{0}) and (0,0,1)(\clueOne{0}, \clueTwo{0}, \clueThree{1}) switches for mod 5\clueTwo{\text{mod } 5} and mod 7\clueThree{\text{mod } 7}Multiply the remaining pairs of moduli and check that both already leave remainder 11 in their own modulus with a tuning multiplier of 11.
3×7\displaystyle \clueOne{3} \times \clueThree{7} =21≡1(mod5),3×5\displaystyle = 21 \equiv 1 \clueTwo{\pmod{5}}, \qquad \clueOne{3} \times \clueTwo{5} =15≡1(mod7)\displaystyle = 15 \equiv 1 \clueThree{\pmod{7}}
Weight the switches by the target remainders and reduce modulo 105\cycle{105}Multiply each unit switch by the remainder we want for that clue, add the pieces together, and subtract multiples of 3×5×7\clueOne{3} \times \clueTwo{5} \times \clueThree{7} == 105\cycle{105}.
x=2(70)+3(21)+2(15)=140+63+30=233≡23(mod105)\displaystyle \begin{aligned} x &= \clueOne{2}(70) + \clueTwo{3}(21) + \clueThree{2}(15) \\ &= 140 + 63 + 30 \\ &= 233 \\ &\equiv 23 \cycle{\pmod{105}} \end{aligned}

So 2323 is the smallest positive integer that leaves remainders 2\clueOne{2}, 3,\clueTwo{3}, and 2\clueThree{2} when divided by 3\clueOne{3}, 5,\clueTwo{5}, and 7\clueThree{7}, and every other solution differs from 2323 by a multiple of 105\cycle{105}.

In this three-dial system for mod 3\clueOne{\text{mod } 3}, mod 5\clueTwo{\text{mod } 5}, and mod 7\clueThree{\text{mod } 7}, stepping any one remainder dial adds its unit switch (7070, 2121, or 1515), which spins the other two dials through exact full turns back to the remainders they already had.

Each unit switch is a multiple of the other two moduli, so turning one remainder dial spins the other two through full turns back to where they started.
4

Why the Solution Is Unique

The unit switches construct a valid solution, and now we want to prove that no second solution can fit inside the same window of M=105\cycle{M} = \cycle{105} numbers.

Why the Solution Is Unique
x≡x′(mod3)x≡x′(mod5)x≡x′(mod7)  ⟹  ?x≡x′(mod105)\displaystyle \begin{aligned} x &\equiv x' \clueOne{\pmod{3}} \\ x &\equiv x' \clueTwo{\pmod{5}} \\ x &\equiv x' \clueThree{\pmod{7}} \\ \overset{?}{\implies} x &\equiv x' \cycle{\pmod{105}} \end{aligned}

If two integers xx and x′x' leave the same remainder in mod 3\clueOne{\text{mod } 3}, mod 5\clueTwo{\text{mod } 5}, and mod 7\clueThree{\text{mod } 7}, their difference x−x′x - x' leaves remainder 00 when divided by 3\clueOne{3}, 5,\clueTwo{5}, and 7\clueThree{7}. Because 3\clueOne{3}, 5,\clueTwo{5}, and 7\clueThree{7} are different primes, each appears separately in the prime factorization of x−x′x - x', so their product 3×5×7\clueOne{3} \times \clueTwo{5} \times \clueThree{7} == 105\cycle{105} must divide x−x′x - x':

105∣(x−x′)  ⟹  x≡x′(mod105)\displaystyle \cycle{105} \mid (x - x') \implies x \equiv x' \cycle{\pmod{105}}
−82-82
2323
128128
233233
+105\htmlClass{lc-green}{+105}
+105\htmlClass{lc-green}{+105}
+105\htmlClass{lc-green}{+105}
00
104104
M=105\htmlClass{lc-green}{M} = \htmlClass{lc-green}{105}

Because the step from one solution to the next is always a multiple of 105\cycle{105}, every window of 105\cycle{105} consecutive integers contains a unique solution.

5

Tuning All Three Switches

In Sunzi's puzzle, 2121 and 1515 already left a remainder of 11 without extra tuning. Now let's solve a system where all three switches need a tuning multiplier:

x≡1(mod4),x≡4(mod5),x≡2(mod7)\displaystyle x \equiv \clueOne{1 \pmod{4}}, \qquad x \equiv \clueTwo{4 \pmod{5}}, \qquad x \equiv \clueThree{2 \pmod{7}}
Tuning All Three Switches
x≡1  (mod 4)x \equiv \htmlClass{lc-blue}{1 \; (\text{mod } 4)}
x≡4  (mod 5)x \equiv \htmlClass{lc-pink}{4 \; (\text{mod } 5)}
x≡2  (mod 7)x \equiv \htmlClass{lc-violet}{2 \; (\text{mod } 7)}
??
(1,0,0)(\htmlClass{lc-blue}{1}, \htmlClass{lc-pink}{0}, \htmlClass{lc-violet}{0})
??
(0,1,0)(\htmlClass{lc-blue}{0}, \htmlClass{lc-pink}{1}, \htmlClass{lc-violet}{0})
??
(0,0,1)(\htmlClass{lc-blue}{0}, \htmlClass{lc-pink}{0}, \htmlClass{lc-violet}{1})
Text solution
Check the moduli and find the master cycleEvery pair among 4\clueOne{4}, 5,\clueTwo{5}, and 7\clueThree{7} has a GCD of 1\coprime{1}, so the pattern of remainders repeats every 140\cycle{140} numbers.
M\displaystyle \cycle{M} =4×5×7\displaystyle = \clueOne{4} \times \clueTwo{5} \times \clueThree{7} =140\displaystyle = \cycle{140}
Build and tune the three unit switchesFor each modulus, multiply the other two moduli, reduce that product in the current modulus, and multiply the original product by the modular inverse to get a remainder of 11.
mod 4:5×7=35≡3(mod4),3×3=9≡1(mod4)  ⟹  35×3=105mod 5:4×7=28≡3(mod5),3×2=6≡1(mod5)  ⟹  28×2=56mod 7:4×5=20≡6(mod7),6×6=36≡1(mod7)  ⟹  20×6=120\displaystyle \begin{aligned} & \clueOne{\text{mod } 4}: \\ \clueTwo{5} \times \clueThree{7} = 35 &\equiv 3 \clueOne{\pmod{4}}, \\ 3 \times 3 = 9 &\equiv 1 \clueOne{\pmod{4}} \\ \implies 35 \times 3 &= 105 \\[6pt] & \clueTwo{\text{mod } 5}: \\ \clueOne{4} \times \clueThree{7} = 28 &\equiv 3 \clueTwo{\pmod{5}}, \\ 3 \times 2 = 6 &\equiv 1 \clueTwo{\pmod{5}} \\ \implies 28 \times 2 &= 56 \\[6pt] & \clueThree{\text{mod } 7}: \\ \clueOne{4} \times \clueTwo{5} = 20 &\equiv 6 \clueThree{\pmod{7}}, \\ 6 \times 6 = 36 &\equiv 1 \clueThree{\pmod{7}} \\ \implies 20 \times 6 &= 120 \end{aligned}
Combine the switches with the target remainders and reduce modulo 140\cycle{140}Weight each switch by its target remainder, sum the terms, and subtract 4×140=5604 \times \cycle{140} = 560.
x=1(105)+4(56)+2(120)=105+224+240=569≡9(mod140)\displaystyle \begin{aligned} x &= \clueOne{1}(105) + \clueTwo{4}(56) + \clueThree{2}(120) \\ &= 105 + 224 + 240 \\ &= 569 \\ &\equiv 9 \cycle{\pmod{140}} \end{aligned}

So x≡9(mod140)x \equiv 9 \cycle{\pmod{140}}, meaning 99 is the unique solution between 00 and 139139.

When you apply the tuning multiplier yiy_i, make sure you multiply the unreduced product (35×3=10535 \times 3 = 105), not the reduced remainder (3×3=93 \times 3 = \wrong{9}). You only reduce 3535 to 3(mod4)3 \clueOne{\pmod{4}} to spot the multiplier y1=3y_1 = 3; the switch itself must stay a multiple of 3535 so it stays 00 in mod 5\clueTwo{\text{mod } 5} and mod 7\clueThree{\text{mod } 7}.

6

Large Moduli and the Extended Euclidean Algorithm

When the moduli are large, such as mod 101\clueOne{\text{mod } 101}, mod 257\clueTwo{\text{mod } 257}, and mod 499\clueThree{\text{mod } 499}, guessing the tuning multiplier by trial and error isn't practical. To build the (0,1,0)(\clueOne{0}, \clueTwo{1}, \clueThree{0}) switch for mod 257\clueTwo{\text{mod } 257}, we find the modular inverse using the Extended Euclidean Algorithm, which divides down to a remainder of 11 and substitutes back up to write 11 as a combination of the two numbers (Bézout's identity).

Large Moduli and the Extended Euclidean Algorithm
mod 101\htmlClass{lc-blue}{\text{mod } 101}
mod 257\htmlClass{lc-pink}{\text{mod } 257}
mod 499\htmlClass{lc-violet}{\text{mod } 499}
??
(0,1,0)(\htmlClass{lc-blue}{0}, \htmlClass{lc-pink}{1}, \htmlClass{lc-violet}{0})
Text solution
Multiply the other two moduli and reduce modulo 257\clueTwo{257}Multiply 101\clueOne{101} and 499\clueThree{499} so the number leaves remainder 00 in both, then divide by 257\clueTwo{257} to find the remainder that needs to be tuned to 11.
101×499\displaystyle \clueOne{101} \times \clueThree{499} =50,399\displaystyle = 50{,}399 =196×257+27  ⟹  50,399≡27(mod257)\displaystyle = 196 \times \clueTwo{257} + 27 \implies 50{,}399 \equiv 27 \clueTwo{\pmod{257}}
Run the Euclidean Algorithm and work backwards for the inverse of 27 mod 25727 \clueTwo{\bmod 257}Divide 257\clueTwo{257} by 2727 down to remainder 11, then substitute the remainders back up to express 11 in terms of 257\clueTwo{257} and 2727.
257=9×27+1427=1×14+1314=1×13+1\displaystyle \begin{aligned} \clueTwo{257} &= 9 \times 27 + \leftover{14} \\ 27 &= 1 \times \leftover{14} + \leftover{13} \\ \leftover{14} &= 1 \times \leftover{13} + 1 \end{aligned}
1=14−1×13=14−(27−14)=2×14−27=2(257−9×27)−27=2×257−19×27\displaystyle \begin{aligned} 1 &= \leftover{14} - 1 \times \leftover{13} \\ &= \leftover{14} - (27 - \leftover{14}) \\ &= 2 \times \leftover{14} - 27 \\ &= 2(\clueTwo{257} - 9 \times 27) - 27 \\ &= 2 \times \clueTwo{257} - 19 \times 27 \end{aligned}
Read off the tuning multiplier and build the switchModulo 257\clueTwo{257}, the 2×2572 \times \clueTwo{257} term drops out to 00, leaving −19×27≡1(mod257)-19 \times 27 \equiv 1 \clueTwo{\pmod{257}}, so we convert y2=−19y_2 = -19 to 257−19=238\clueTwo{257} - 19 = 238 and multiply by the base product 50,39950{,}399.
50,399×238\displaystyle 50{,}399 \times 238 =11,994,962\displaystyle = 11{,}994{,}962

So 11,994,96211{,}994{,}962 is the (0,1,0)(\clueOne{0}, \clueTwo{1}, \clueThree{0}) unit switch that leaves remainder 00 when divided by 101\clueOne{101} or 499\clueThree{499}, and remainder 11 when divided by 257\clueTwo{257}.

7

The Substitution Method

Instead of building switches in parallel, we can solve a system of congruences one clue at a time by translating a congruence into an ordinary equation and substituting it into the next clue:

x≡2(mod3),x≡3(mod5),x≡2(mod7)\displaystyle x \equiv \clueOne{2 \pmod{3}}, \qquad x \equiv \clueTwo{3 \pmod{5}}, \qquad x \equiv \clueThree{2 \pmod{7}}
The Substitution Method
x≡2(mod3)x≡3(mod5)x≡?  (mod15)\displaystyle \begin{array}{rl} x \equiv & \clueOne{2 \pmod{3}} \\ x \equiv & \clueTwo{3 \pmod{5}} \\ \hline x \equiv & {?} \; \cycle{\pmod{15}} \end{array}
Text solution
Rewrite the first congruence and substitute into the secondWrite x≡2(mod3)x \equiv \clueOne{2 \pmod{3}} as x=3k+2x = \clueOne{3}k + \clueOne{2} for some integer kk, substitute it into the mod 5\clueTwo{\text{mod } 5} congruence, and multiply by 22 (the inverse of 3 mod 5\clueOne{3} \clueTwo{\bmod 5}) to isolate kk.
3k+2≡3(mod5)3k≡1(mod5)2⋅3k≡2⋅1(mod5)k≡2(mod5)\displaystyle \begin{aligned} \clueOne{3}k + \clueOne{2} &\equiv \clueTwo{3 \pmod{5}} \\ \clueOne{3}k &\equiv 1 \clueTwo{\pmod{5}} \\ 2 \cdot \clueOne{3}k &\equiv 2 \cdot 1 \clueTwo{\pmod{5}} \\ k &\equiv \clueTwo{2 \pmod{5}} \end{aligned}
Merge the first two clues and substitute into the thirdWrite k=5j+2k = \clueTwo{5}j + \clueTwo{2} to get xx == 3(5j+2)+2\clueOne{3}(\clueTwo{5}j + \clueTwo{2}) + \clueOne{2} == 15j+8\cycle{15}j + 8, then substitute this combined expression into the mod 7\clueThree{\text{mod } 7} congruence and reduce the coefficients modulo 7\clueThree{7}.
15j+8≡2(mod7)1j+1≡2(mod7)j≡1(mod7)\displaystyle \begin{aligned} \cycle{15}j + 8 &\equiv \clueThree{2 \pmod{7}} \\ 1j + 1 &\equiv \clueThree{2 \pmod{7}} \\ j &\equiv \clueThree{1 \pmod{7}} \end{aligned}
Substitute back into xxWrite j=7m+1j = \clueThree{7}m + \clueThree{1} for some integer mm and plug it back into x=15j+8x = \cycle{15}j + 8.
x=15(7m+1)+8=105m+23≡23(mod105)\displaystyle \begin{aligned} x &= \cycle{15}(\clueThree{7}m + \clueThree{1}) + 8 \\ &= \cycle{105}m + 23 \\ &\equiv 23 \cycle{\pmod{105}} \end{aligned}

Merging the congruences one at a time gives the same unique solution, x≡23(mod105)x \equiv 23 \cycle{\pmod{105}}.

8

The General Formula and Why It Matters

We can package the unit-switch construction into a single formula for any system of kk congruences with pairwise coprime moduli m1\clueOne{m_1}, m2\clueTwo{m_2}, …\dots, mkm_k.

The General Formula and Why It Matters
x≡∑i=1kaiMiyi(modM)\displaystyle x \equiv \sum_{i=1}^{k} \clueOne{a_i} M_i y_i \cycle{\pmod{M}}
M\displaystyle \cycle{M} =?,Mi\displaystyle = {}?, \quad M_i =?,yi\displaystyle = {}?, \quad y_i =?\displaystyle = {}?

When every pair of moduli satisfies gcd⁡(mi,mj)=1\gcd(\clueOne{m_i}, \clueTwo{m_j}) = \coprime{1}, the system x≡ai(modmi)x \equiv \clueOne{a_i \pmod{m_i}} has a unique solution modulo the master cycle M=m1m2⋯mk\cycle{M} = \clueOne{m_1} \clueTwo{m_2} \cdots m_k:

x≡∑i=1kai⏟targetremainderMi⏞M/miyi⏞Mi−1modmi⏟unit switchfor mod mi(modM)\displaystyle \boxed{x \equiv \sum_{i=1}^{k} \underbrace{\clueOne{a_i}}_{\substack{\text{target} \\ \text{remainder}}} \underbrace{\overbrace{M_i}^{\cycle{M} / \clueOne{m_i}} \overbrace{y_i}^{\begin{gathered}\scriptstyle M_i^{-1} \\[-3pt] \scriptstyle \clueOne{\bmod m_i}\end{gathered}}}_{\begin{gathered}\scriptstyle \text{unit switch} \\[-3pt] \scriptstyle \text{for mod } \clueOne{m_i}\end{gathered}} \cycle{\pmod{M}}}

where Mi=MmiM_i = \tfrac{\cycle{M}}{\clueOne{m_i}} is the product of all the moduli except mi\clueOne{m_i}, and yiy_i is the tuning multiplier satisfying Miyi≡1(modmi)M_i y_i \equiv 1 \clueOne{\pmod{m_i}}. Because each number modulo M\cycle{M} matches a unique tuple of remainders modulo m1\clueOne{m_1}, m2\clueTwo{m_2}, …\dots, mkm_k, large calculations modulo a composite number M\cycle{M} can be split into small, parallel calculations modulo each mi\clueOne{m_i} and reassembled at the end.

Key concepts

Before solving a system of congruences, check that the moduli are pairwise coprime: gcd⁡(mi,mj)=1\gcd(\clueOne{m_i}, \clueTwo{m_j}) = \coprime{1} for every pair. If two moduli share a factor, such as x≡1(mod4)x \equiv \clueOne{1 \pmod{4}} and x≡2(mod6)x \equiv \clueTwo{2 \pmod{6}} where gcd⁡(4,6)=2\gcd(\clueOne{4}, \clueTwo{6}) = \shared{2}, do not apply the Chinese Remainder Theorem formula; their cycles reset early together, so you must check whether the remainders agree on the shared factor first.

Once you know the moduli are pairwise coprime, pick the method that fits the size of the system:

Two congruences →\rightarrow Substitution

Write the first congruence as x=m1k+a1x = \clueOne{m_1} k + \clueOne{a_1} and plug it into the second. For x≡2(mod3)x \equiv \clueOne{2 \pmod{3}} and x≡3(mod5)x \equiv \clueTwo{3 \pmod{5}}, substituting x=3k+2x = \clueOne{3}k + \clueOne{2} gives 3k+2≡3(mod5)\clueOne{3}k + \clueOne{2} \equiv \clueTwo{3 \pmod{5}}   ⟹  \implies k≡2(mod5)k \equiv \clueTwo{2 \pmod{5}}, so k=5j+2k = \clueTwo{5}j + \clueTwo{2} and xx == 3(5j+2)+2\clueOne{3}(\clueTwo{5}j + \clueTwo{2}) + \clueOne{2} == 15j+8≡8(mod15)\cycle{15}j + 8 \equiv 8 \cycle{\pmod{15}}.

Three or more congruences (or fixed moduli with new remainders) →\rightarrow Unit switches

Set M=m1m2⋯mk\cycle{M} = \clueOne{m_1} \clueTwo{m_2} \cdots m_k. For each modulus mi\clueOne{m_i}, multiply the other moduli to get Mi=MmiM_i = \tfrac{\cycle{M}}{\clueOne{m_i}}, find the tuning multiplier yiy_i with Miyi≡1(modmi)M_i y_i \equiv 1 \clueOne{\pmod{m_i}} (by inspection for small moduli, or the Extended Euclidean Algorithm for large moduli), and sum the weighted switches:

x≡∑i=1kaiMiyi(modM)\displaystyle x \equiv \sum_{i=1}^{k} \clueOne{a_i} M_i y_i \cycle{\pmod{M}}
For mod 3\text{mod } \clueOne{3}, 5\clueTwo{5}, 7\clueThree{7} (M=105\cycle{M} = \cycle{105}), the mod 3\clueOne{\text{mod } 3} switch starts with M1M_1 == 5×7\clueTwo{5} \times \clueThree{7} == 35≡2(mod3)35 \equiv 2 \clueOne{\pmod{3}}, tunes it with y1=2y_1 = 2 since 2×2≡1(mod3)2 \times 2 \equiv 1 \clueOne{\pmod{3}}, and uses the full product M1y1=35×2=70M_1 y_1 = 35 \times 2 = 70.

9

Practice

1

Solve the system x≡3(mod7)x \equiv \clueOne{3 \pmod{7}} and x≡4(mod9)x \equiv \clueTwo{4 \pmod{9}} using the substitution method. Give your answer as a congruence modulo 63\cycle{63}.

Rewrite the first congruence as x=7k+3x = \clueOne{7}k + \clueOne{3} and substitute that expression for xx into the modulo 9\clueTwo{9} congruence.

From the first congruence, write x=7k+3x = \clueOne{7}k + \clueOne{3} for some integer kk. Substituting into the second gives 7k+3≡4(mod9)\clueOne{7}k + \clueOne{3} \equiv \clueTwo{4 \pmod{9}}, so 7k≡1(mod9)\clueOne{7}k \equiv 1 \clueTwo{\pmod{9}}. Since 7×4=28≡1(mod9)\clueOne{7} \times 4 = 28 \equiv 1 \clueTwo{\pmod{9}}, multiplying both sides by 44 gives k≡4(mod9)k \equiv \clueTwo{4 \pmod{9}}, so k=9j+4k = \clueTwo{9}j + \clueTwo{4} for some integer jj. (Watch out: k≡4(mod9)k \equiv \clueTwo{4 \pmod{9}} is the value for kk, not xx; stopping here gives x≡4(mod63)x \equiv \wrong{4} \cycle{\pmod{63}}, and forgetting the +3+ \clueOne{3} when plugging back into x=7k+3x = \clueOne{7}k + \clueOne{3} gives x≡28(mod63)x \equiv \wrong{28} \cycle{\pmod{63}}.) Substituting k=9j+4k = \clueTwo{9}j + \clueTwo{4} back into x=7k+3x = \clueOne{7}k + \clueOne{3} gives xx == 7(9j+4)+3\clueOne{7}(\clueTwo{9}j + \clueTwo{4}) + \clueOne{3} == 63j+31\cycle{63}j + 31, so x≡31(mod63)x \equiv 31 \cycle{\pmod{63}}.
2

(a) Explain why the system x≡2(mod6)x \equiv \clueOne{2 \pmod{6}} and x≡4(mod9)x \equiv \clueTwo{4 \pmod{9}} has no solution, while x≡2(mod6)x \equiv \clueOne{2 \pmod{6}} and x≡5(mod9)x \equiv \clueTwo{5 \pmod{9}} does. (b) For the three moduli 6\clueOne{6}, 10,\clueTwo{10}, and 35\clueThree{35}, gcd⁡(6,10,35)\gcd(\clueOne{6}, \clueTwo{10}, \clueThree{35}) == 11. Does the Chinese Remainder Theorem guarantee a solution for every system with these three moduli?

(a) Check what remainder xx must leave when divided by gcd⁡(6,9)=3\gcd(\clueOne{6}, \clueTwo{9}) = 3 in each congruence. (b) Test gcd⁡(mi,mj)\gcd(\clueOne{m_i}, \clueTwo{m_j}) for individual pairs of moduli rather than all three at once.

(a) Because gcd⁡(6,9)=3\gcd(\clueOne{6}, \clueTwo{9}) = \shared{3}, both moduli are multiples of 3\shared{3}. (Watch out: if you try to run substitution or unit switches blindly without checking the GCD first, you end up trying to solve 6k≡2(mod9)\clueOne{6}k \equiv 2 \clueTwo{\pmod{9}}, where 6\clueOne{6} has no inverse modulo 9\clueTwo{9}.) A number with x≡2(mod6)x \equiv \clueOne{2 \pmod{6}} has the form 6k+2\clueOne{6}k + \clueOne{2} == 3(2k)+2\shared{3}(2k) + \clueOne{2}, so it leaves remainder 22 when divided by 3\shared{3}. A number with x≡4(mod9)x \equiv \clueTwo{4 \pmod{9}} has the form 9j+4\clueTwo{9}j + \clueTwo{4} == 3(3j+1)+1\shared{3}(3j + 1) + 1, so it leaves remainder 11 when divided by 3\shared{3}, which is impossible for the same integer; by contrast, x≡5(mod9)x \equiv \clueTwo{5 \pmod{9}} leaves remainder 22 when divided by 3\shared{3}, which is consistent (for example, x=14x = 14 works, and the cycle repeats every 18\cycle{18}). (b) (Watch out: gcd⁡(6,10,35)\gcd(\clueOne{6}, \clueTwo{10}, \clueThree{35}) == 11 only says no prime divides all three at once, which leads to the wrong answer "Yes" if you forget pairwise coprimality.) The theorem requires the moduli to be pairwise coprime, but gcd⁡(6,10)=2\gcd(\clueOne{6}, \clueTwo{10}) = \shared{2} and gcd⁡(10,35)=5\gcd(\clueTwo{10}, \clueThree{35}) = \shared{5}, so the answer is no.
3

Build the three unit switches for the moduli 3\clueOne{3}, 4,\clueTwo{4}, and 5\clueThree{5}, and use them to find the unique solution to x≡1(mod3)x \equiv \clueOne{1 \pmod{3}}, x≡3(mod4)x \equiv \clueTwo{3 \pmod{4}}, and x≡2(mod5)x \equiv \clueThree{2 \pmod{5}} in the range 0≤x≤590 \le x \le 59.

With M\cycle{M} == 3×4×5\clueOne{3} \times \clueTwo{4} \times \clueThree{5} == 60\cycle{60}, start each switch with the product of the other two moduli (20,15,20, 15, and 1212) and find the tuning multiplier that makes each one congruent to 11 in its own modulus.

The moduli 3\clueOne{3}, 4,\clueTwo{4}, and 5\clueThree{5} are pairwise coprime with master cycle M\cycle{M} == 3×4×5\clueOne{3} \times \clueTwo{4} \times \clueThree{5} == 60\cycle{60}. For mod 3\clueOne{\text{mod } 3}, start with 4×5\clueTwo{4} \times \clueThree{5} == 20≡2(mod3)20 \equiv 2 \clueOne{\pmod{3}}; since 2×2=4≡1(mod3)2 \times 2 = 4 \equiv 1 \clueOne{\pmod{3}}, the (1,0,0)(\clueOne{1}, \clueTwo{0}, \clueThree{0}) switch is 20×2=4020 \times 2 = 40. For mod 4\clueTwo{\text{mod } 4}, start with 3×5\clueOne{3} \times \clueThree{5} == 15≡3(mod4)15 \equiv 3 \clueTwo{\pmod{4}}; since 3×3=9≡1(mod4)3 \times 3 = 9 \equiv 1 \clueTwo{\pmod{4}}, the (0,1,0)(\clueOne{0}, \clueTwo{1}, \clueThree{0}) switch is 15×3=4515 \times 3 = 45. For mod 5\clueThree{\text{mod } 5}, start with 3×4\clueOne{3} \times \clueTwo{4} == 12≡2(mod5)12 \equiv 2 \clueThree{\pmod{5}}; since 2×3=6≡1(mod5)2 \times 3 = 6 \equiv 1 \clueThree{\pmod{5}}, the (0,0,1)(\clueOne{0}, \clueTwo{0}, \clueThree{1}) switch is 12×3=3612 \times 3 = 36. (Watch out: multiply each tuning multiplier by the unreduced product 20,15,20, 15, or 1212, not by the reduced remainder 2,3,2, 3, or 22; multiplying the reduced remainders gives switches 4,9,\wrong{4}, \wrong{9}, and 6\wrong{6} and the wrong answer x=43x = \wrong{43}.) Combining the switches with the target remainders gives xx == 1(40)+3(45)+2(36)\clueOne{1}(40) + \clueTwo{3}(45) + \clueThree{2}(36) == 40+135+7240 + 135 + 72 == 247247. Reducing modulo 60\cycle{60} gives 247−4(60)=7247 - 4(\cycle{60}) = 7.
4

An orchard harvests between 400400 and 500500 apples. When the apples are packed in bags of 3\clueOne{3}, 1\clueOne{1} is left over; in bags of 5\clueTwo{5}, 4\clueTwo{4} are left over; in bags of 7\clueThree{7}, 5\clueThree{5} are left over. Using the unit switches 70,21,70, 21, and 1515 for mod 3\clueOne{\text{mod } 3}, mod 5,\clueTwo{\text{mod } 5}, and mod 7\clueThree{\text{mod } 7}, find the exact number of apples harvested.

Weight the switches 70,21,70, 21, and 1515 by the new target remainders 1\clueOne{1}, 4,\clueTwo{4}, and 5\clueThree{5}, then add multiples of M=105\cycle{M} = \cycle{105} to land in the interval from 400400 to 500500.

We need x≡1(mod3)x \equiv \clueOne{1 \pmod{3}}, x≡4(mod5)x \equiv \clueTwo{4 \pmod{5}}, and x≡5(mod7)x \equiv \clueThree{5 \pmod{7}} with 400≤x≤500400 \le x \le 500. Using the unit switches 70,21,70, 21, and 1515 gives xx == 1(70)+4(21)+5(15)\clueOne{1}(70) + \clueTwo{4}(21) + \clueThree{5}(15) == 70+84+7570 + 84 + 75 == 229229. Modulo 3×5×7\clueOne{3} \times \clueTwo{5} \times \clueThree{7} == 105\cycle{105}, we have 229−2(105)=19229 - 2(\cycle{105}) = 19, so the solutions are x≡19(mod105)x \equiv 19 \cycle{\pmod{105}}. (Watch out: stopping at the smallest positive solution gives 19\wrong{19}, and adding 3×1053 \times \cycle{105} instead of 4×1054 \times \cycle{105} gives 334\wrong{334}, which is still below the 400400 to 500500 window.) Adding 44 multiples of 105\cycle{105} to reach the window between 400400 and 500500 gives 19+4(105)=43919 + 4(\cycle{105}) = 439.
5

In a system of congruences with pairwise coprime moduli 17\clueOne{17}, 43,\clueTwo{43}, and 100\clueThree{100}, use the Extended Euclidean Algorithm to find the (0,1,0)(\clueOne{0}, \clueTwo{1}, \clueThree{0}) unit switch for mod 43\clueTwo{\text{mod } 43} (the integer between 00 and 73,09973{,}099 that is a multiple of both 17\clueOne{17} and 100\clueThree{100} and leaves remainder 11 when divided by 43\clueTwo{43}).

Multiply the other two moduli (17×100=1,700\clueOne{17} \times \clueThree{100} = 1{,}700), reduce modulo 43\clueTwo{43}, and run the Extended Euclidean Algorithm on 43\clueTwo{43} and that remainder to find its inverse modulo 43\clueTwo{43}.

First multiply the other two moduli: 17×100=1,700\clueOne{17} \times \clueThree{100} = 1{,}700. Since 1,700=39×43+231{,}700 = 39 \times \clueTwo{43} + 23, we have 1,700≡23(mod43)1{,}700 \equiv 23 \clueTwo{\pmod{43}}, so we need y2y_2 with 23y2≡1(mod43)23y_2 \equiv 1 \clueTwo{\pmod{43}}. Running the Euclidean Algorithm gives 43=1(23)+20\clueTwo{43} = 1(23) + \leftover{20}, 23=1(20)+323 = 1(\leftover{20}) + \leftover{3}, 20=6(3)+2\leftover{20} = 6(\leftover{3}) + \leftover{2}, and 3=1(2)+1\leftover{3} = 1(\leftover{2}) + 1. Working backwards: 11 == 3−1(2)\leftover{3} - 1(\leftover{2}) == 3−(20−6×3)\leftover{3} - (\leftover{20} - 6 \times \leftover{3}) == 7(3)−207(\leftover{3}) - \leftover{20} == 7(23−20)−207(23 - \leftover{20}) - \leftover{20} == 7(23)−8(20)7(23) - 8(\leftover{20}) == 7(23)−8(43−23)7(23) - 8(\clueTwo{43} - 23) == 15(23)−8(43)15(23) - 8(\clueTwo{43}). Modulo 43\clueTwo{43}, this gives 15×23≡1(mod43)15 \times 23 \equiv 1 \clueTwo{\pmod{43}}, so y2=15y_2 = 15. (Watch out: take the coefficient 1515 attached to 2323, not the coefficient −8≡35(mod43)\wrong{-8} \equiv \wrong{35} \clueTwo{\pmod{43}} attached to 43\clueTwo{43}, which would give 59,500\wrong{59{,}500}; and don't stop at y2=15y_2 = 15 without multiplying by 1,7001{,}700.) Multiplying the base product by y2=15y_2 = 15 gives the (0,1,0)(\clueOne{0}, \clueTwo{1}, \clueThree{0}) unit switch 1,700×15=25,5001{,}700 \times 15 = 25{,}500.
6

Suppose two integers xx and x′x' leave the same remainder when divided by 8\clueOne{8}, the same remainder when divided by 9\clueTwo{9}, and the same remainder when divided by 25\clueThree{25}. Prove that x−x′x - x' is a multiple of 1,800\cycle{1{,}800}, and give a counterexample showing why x−x′x - x' need not be a multiple of 8×12×25\clueOne{8} \times \clueTwo{12} \times \clueThree{25} == 2,4002{,}400 if the moduli are 8\clueOne{8}, 12,\clueTwo{12}, and 25\clueThree{25} instead.

Check the remainder of the difference x−x′x - x' when divided by each modulus, then compare the prime factorizations of 8\clueOne{8}, 9\clueTwo{9}, 25\clueThree{25} with those of 8\clueOne{8}, 12\clueTwo{12}, 25\clueThree{25}.

Because x≡x′x \equiv x' in mod 8\clueOne{\text{mod } 8}, mod 9\clueTwo{\text{mod } 9}, and mod 25\clueThree{\text{mod } 25}, the difference x−x′x - x' leaves remainder 00 when divided by 8\clueOne{8}, 9,\clueTwo{9}, and 25\clueThree{25}, so 8\clueOne{8}, 9,\clueTwo{9}, and 25\clueThree{25} each divide x−x′x - x'. Since 8=23\clueOne{8} = \clueOne{2^3}, 9=32\clueTwo{9} = \clueTwo{3^2}, and 25=52\clueThree{25} = \clueThree{5^2} share no prime factors, they are pairwise coprime, so the prime factorization of x−x′x - x' must contain 23\clueOne{2^3}, 32\clueTwo{3^2}, and 52\clueThree{5^2} separately, and x−x′x - x' is divisible by their product 8×9×25\clueOne{8} \times \clueTwo{9} \times \clueThree{25} == 1,800\cycle{1{,}800}. (Watch out: if you multiply the moduli without checking that they are pairwise coprime, you will falsely assume the cycle for 8\clueOne{8}, 12,\clueTwo{12}, and 25\clueThree{25} is 8×12×25\clueOne{8} \times \clueTwo{12} \times \clueThree{25} == 2,400\wrong{2{,}400}.) If the moduli are 8\clueOne{8}, 12,\clueTwo{12}, and 25\clueThree{25}, they are not pairwise coprime because gcd⁡(8,12)=4\gcd(\clueOne{8}, \clueTwo{12}) = \shared{4}; for example, x=600x = \cycle{600} and x′=0x' = 0 both leave remainder 00 when divided by 8\clueOne{8}, 12,\clueTwo{12}, and 25\clueThree{25}, yet x−x′=600x - x' = \cycle{600}.