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 x that satisfies several congruences with different moduli at the same time.
1
Sunzi's Remainder Puzzle
We want to find a mystery integer x that leaves remainder 2 when divided by 3, remainder 3 when divided by 5, and remainder 2 when divided by 7:
x≡2(mod3),x≡3(mod5),x≡2(mod7)
Sunzi's Remainder Puzzle
Guessing and checking works when the moduli are tiny, but it stalls as soon as the moduli get large. To build x directly out of the clues, we first need to see how a pair of remainders behaves as we count up from 0.
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
For mod 3 and mod 5, there are 3×5=15 possible remainder pairs, and counting from 0 to 14 hits every pair once before both moduli reset to (0,0) at 15. When two moduli share a factor, such as mod 4 and mod 6 where gcd(4,6)=2, their cycles lock in step and reset together early at 12 instead of 4×6=24, hitting half the remainder pairs twice and skipping the other half completely.
For a system with three or more moduli m1,m2,…,mk, every pair of moduli must have no common factors:
pairwise coprimegcd(mi,mj)=1for all i=j
Whenever m1 and m2 are coprime, the wrapping diagonal path visits every remainder pair (xmodm1,xmodm2) in the grid once before resetting at m1×m2; whenever m1 and m2 share a factor, the path returns to (0,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,5, and 7 are pairwise coprime, Sunzi's puzzle is guaranteed to have a solution in the window 0≤x≤104:
x≡2(mod3),x≡3(mod5),x≡2(mod7)
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 1 in the current modulus. That tuning step uses a modular inverse: a multiplier y satisfying a⋅y≡1(modn), which is guaranteed to exist whenever gcd(a,n)=1.
Building Unit Switches
Text solution
Build the (1,0,0) switch for mod 3Multiply the other two moduli so the number is 0 in mod 5 and mod 7, then multiply by the inverse of 35mod3 to tune its mod 3 remainder to 1.
5×7=35≡2(mod3),2⋅y12=4≡1(mod3)⟹35×2=70
Build the (0,1,0) and (0,0,1) switches for mod 5 and mod 7Multiply the remaining pairs of moduli and check that both already leave remainder 1 in their own modulus with a tuning multiplier of 1.
3×7=21≡1(mod5),3×5=15≡1(mod7)
Weight the switches by the target remainders and reduce modulo 105Multiply each unit switch by the remainder we want for that clue, add the pieces together, and subtract multiples of 3×5×7=105.
x=2(70)+3(21)+2(15)=140+63+30=233≡23(mod105)
So 23 is the smallest positive integer that leaves remainders 2,3, and 2 when divided by 3,5, and 7, and every other solution differs from 23 by a multiple of 105.
In this three-dial system for mod 3,mod 5, and mod 7, stepping any one remainder dial adds its unit switch (70,21, or 15), 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 numbers.
Why the Solution Is Unique
xxx⟹?x≡x′(mod3)≡x′(mod5)≡x′(mod7)≡x′(mod105)
If two integers x and x′ leave the same remainder in mod 3,mod 5, and mod 7, their difference x−x′ leaves remainder 0 when divided by 3,5, and 7. Because 3,5, and 7 are different primes, each appears separately in the prime factorization of x−x′, so their product 3×5×7=105 must divide x−x′:
105∣(x−x′)⟹x≡x′(mod105)
Because the step from one solution to the next is always a multiple of 105, every window of 105 consecutive integers contains a unique solution.
5
Tuning All Three Switches
In Sunzi's puzzle, 21 and 15 already left a remainder of 1 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)
Tuning All Three Switches
Text solution
Check the moduli and find the master cycleEvery pair among 4,5, and 7 has a GCD of 1, so the pattern of remainders repeats every 140 numbers.
M=4×5×7=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 1.
Combine the switches with the target remainders and reduce modulo 140Weight each switch by its target remainder, sum the terms, and subtract 4×140=560.
x=1(105)+4(56)+2(120)=105+224+240=569≡9(mod140)
So x≡9(mod140), meaning 9 is the unique solution between 0 and 139.
When you apply the tuning multiplier yi, make sure you multiply the unreduced product (35×3=105), not the reduced remainder (3×3=9). You only reduce 35 to 3(mod4) to spot the multiplier y1=3; the switch itself must stay a multiple of 35 so it stays 0 in mod 5 and mod 7.
6
Large Moduli and the Extended Euclidean Algorithm
When the moduli are large, such as mod 101,mod 257, and mod 499, guessing the tuning multiplier by trial and error isn't practical. To build the (0,1,0) switch for mod 257, we find the modular inverse using the Extended Euclidean Algorithm, which divides down to a remainder of 1 and substitutes back up to write 1 as a combination of the two numbers (Bézout's identity).
Large Moduli and the Extended Euclidean Algorithm
Text solution
Multiply the other two moduli and reduce modulo 257Multiply 101 and 499 so the number leaves remainder 0 in both, then divide by 257 to find the remainder that needs to be tuned to 1.
101×499=50,399=196×257+27⟹50,399≡27(mod257)
Run the Euclidean Algorithm and work backwards for the inverse of 27mod257Divide 257 by 27 down to remainder 1, then substitute the remainders back up to express 1 in terms of 257 and 27.
Read off the tuning multiplier and build the switchModulo 257, the 2×257 term drops out to 0, leaving −19×27≡1(mod257), so we convert y2=−19 to 257−19=238 and multiply by the base product 50,399.
50,399×238=11,994,962
So 11,994,962 is the (0,1,0) unit switch that leaves remainder 0 when divided by 101 or 499, and remainder 1 when divided by 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)
The Substitution Method
x≡x≡x≡2(mod3)3(mod5)?(mod15)
Text solution
Rewrite the first congruence and substitute into the secondWrite x≡2(mod3) as x=3k+2 for some integer k, substitute it into the mod 5 congruence, and multiply by 2 (the inverse of 3mod5) to isolate k.
3k+23k2⋅3kk≡3(mod5)≡1(mod5)≡2⋅1(mod5)≡2(mod5)
Merge the first two clues and substitute into the thirdWrite k=5j+2 to get x=3(5j+2)+2=15j+8, then substitute this combined expression into the mod 7 congruence and reduce the coefficients modulo 7.
15j+81j+1j≡2(mod7)≡2(mod7)≡1(mod7)
Substitute back into xWrite j=7m+1 for some integer m and plug it back into x=15j+8.
x=15(7m+1)+8=105m+23≡23(mod105)
Merging the congruences one at a time gives the same unique solution, x≡23(mod105).
8
The General Formula and Why It Matters
We can package the unit-switch construction into a single formula for any system of k congruences with pairwise coprime moduli m1,m2,…,mk.
The General Formula and Why It Matters
x≡i=1∑kaiMiyi(modM)
M=?,Mi=?,yi=?
When every pair of moduli satisfies gcd(mi,mj)=1, the system x≡ai(modmi) has a unique solution modulo the master cycle M=m1m2⋯mk:
x≡i=1∑ktargetremainderaiunit switchfor mod miMiM/miyiMi−1modmi(modM)
where Mi=miM is the product of all the moduli except mi, and yi is the tuning multiplier satisfying Miyi≡1(modmi). Because each number modulo M matches a unique tuple of remainders modulo m1,m2,…,mk, large calculations modulo a composite number M can be split into small, parallel calculations modulo each mi and reassembled at the end.
Key concepts
Before solving a system of congruences, check that the moduli are pairwise coprime: gcd(mi,mj)=1 for every pair. If two moduli share a factor, such as x≡1(mod4) and x≡2(mod6) where gcd(4,6)=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 → Substitution
Write the first congruence as x=m1k+a1 and plug it into the second. For x≡2(mod3) and x≡3(mod5), substituting x=3k+2 gives 3k+2≡3(mod5)⟹k≡2(mod5), so k=5j+2 and x=3(5j+2)+2=15j+8≡8(mod15).
Three or more congruences (or fixed moduli with new remainders) → Unit switches
Set M=m1m2⋯mk. For each modulus mi, multiply the other moduli to get Mi=miM, find the tuning multiplier yi with Miyi≡1(modmi) (by inspection for small moduli, or the Extended Euclidean Algorithm for large moduli), and sum the weighted switches:
x≡i=1∑kaiMiyi(modM)
For mod 3,5,7(M=105), the mod 3 switch starts with M1=5×7=35≡2(mod3), tunes it with y1=2 since 2×2≡1(mod3), and uses the full product M1y1=35×2=70.
9
Practice
1
Solve the system x≡3(mod7) and x≡4(mod9) using the substitution method. Give your answer as a congruence modulo 63.
Watch out: k≡4(mod9) is the value for k, not x; stopping here gives x≡4(mod63).Watch out: forgetting the +3 when plugging back into x=7k+3 gives x≡28(mod63).
Rewrite the first congruence as x=7k+3 and substitute that expression for x into the modulo 9 congruence.
From the first congruence, write x=7k+3 for some integer k. Substituting into the second gives 7k+3≡4(mod9), so 7k≡1(mod9). Since 7×4=28≡1(mod9), multiplying both sides by 4 gives k≡4(mod9), so k=9j+4 for some integer j. (Watch out: k≡4(mod9) is the value for k, not x; stopping here gives x≡4(mod63), and forgetting the +3 when plugging back into x=7k+3 gives x≡28(mod63).) Substituting k=9j+4 back into x=7k+3 gives x=7(9j+4)+3=63j+31, so x≡31(mod63).
2
(a) Explain why the system x≡2(mod6) and x≡4(mod9) has no solution, while x≡2(mod6) and x≡5(mod9) does. (b) For the three moduli 6,10, and 35,gcd(6,10,35)=1. Does the Chinese Remainder Theorem guarantee a solution for every system with these three moduli?
Watch out: gcd(6,10,35)=1 only says no prime divides all three at once, which leads to the wrong answer "Yes" if you forget pairwise coprimality.
(a) Check what remainder x must leave when divided by gcd(6,9)=3 in each congruence. (b) Test gcd(mi,mj) for individual pairs of moduli rather than all three at once.
(a) Because gcd(6,9)=3, both moduli are multiples of 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), where 6 has no inverse modulo 9.) A number with x≡2(mod6) has the form 6k+2=3(2k)+2, so it leaves remainder 2 when divided by 3. A number with x≡4(mod9) has the form 9j+4=3(3j+1)+1, so it leaves remainder 1 when divided by 3, which is impossible for the same integer; by contrast, x≡5(mod9) leaves remainder 2 when divided by 3, which is consistent (for example, x=14 works, and the cycle repeats every 18). (b) (Watch out: gcd(6,10,35)=1 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 and gcd(10,35)=5, so the answer is no.
3
Build the three unit switches for the moduli 3,4, and 5, and use them to find the unique solution to x≡1(mod3),x≡3(mod4), and x≡2(mod5) in the range 0≤x≤59.
Watch out: multiply each tuning multiplier by the unreduced product 20,15, or 12, not by the reduced remainder 2,3, or 2; multiplying the reduced remainders gives switches 4,9, and 6 and the wrong answer x=43.
With M=3×4×5=60, start each switch with the product of the other two moduli (20,15, and 12) and find the tuning multiplier that makes each one congruent to 1 in its own modulus.
The moduli 3,4, and 5 are pairwise coprime with master cycle M=3×4×5=60. For mod 3, start with 4×5=20≡2(mod3); since 2×2=4≡1(mod3), the (1,0,0) switch is 20×2=40. For mod 4, start with 3×5=15≡3(mod4); since 3×3=9≡1(mod4), the (0,1,0) switch is 15×3=45. For mod 5, start with 3×4=12≡2(mod5); since 2×3=6≡1(mod5), the (0,0,1) switch is 12×3=36. (Watch out: multiply each tuning multiplier by the unreduced product 20,15, or 12, not by the reduced remainder 2,3, or 2; multiplying the reduced remainders gives switches 4,9, and 6 and the wrong answer x=43.) Combining the switches with the target remainders gives x=1(40)+3(45)+2(36)=40+135+72=247. Reducing modulo 60 gives 247−4(60)=7.
4
An orchard harvests between 400 and 500 apples. When the apples are packed in bags of 3,1 is left over; in bags of 5,4 are left over; in bags of 7,5 are left over. Using the unit switches 70,21, and 15 for mod 3,mod 5, and mod 7, find the exact number of apples harvested.
Watch out: stopping at the smallest positive solution gives 19.Watch out: adding 3×105 instead of 4×105 gives 334, which is still below the 400 to 500 window.
Weight the switches 70,21, and 15 by the new target remainders 1,4, and 5, then add multiples of M=105 to land in the interval from 400 to 500.
We need x≡1(mod3),x≡4(mod5), and x≡5(mod7) with 400≤x≤500. Using the unit switches 70,21, and 15 gives x=1(70)+4(21)+5(15)=70+84+75=229. Modulo 3×5×7=105, we have 229−2(105)=19, so the solutions are x≡19(mod105). (Watch out: stopping at the smallest positive solution gives 19, and adding 3×105 instead of 4×105 gives 334, which is still below the 400 to 500 window.) Adding 4 multiples of 105 to reach the window between 400 and 500 gives 19+4(105)=439.
5
In a system of congruences with pairwise coprime moduli 17,43, and 100, use the Extended Euclidean Algorithm to find the (0,1,0) unit switch for mod 43 (the integer between 0 and 73,099 that is a multiple of both 17 and 100 and leaves remainder 1 when divided by 43).
Watch out: take the coefficient 15 attached to 23, not the coefficient −8≡35(mod43) attached to 43, which would give 59,500.Watch out: don't stop at y2=15 without multiplying by 1,700.
Multiply the other two moduli (17×100=1,700), reduce modulo 43, and run the Extended Euclidean Algorithm on 43 and that remainder to find its inverse modulo 43.
First multiply the other two moduli: 17×100=1,700. Since 1,700=39×43+23, we have 1,700≡23(mod43), so we need y2 with 23y2≡1(mod43). Running the Euclidean Algorithm gives 43=1(23)+20,23=1(20)+3,20=6(3)+2, and 3=1(2)+1. Working backwards: 1=3−1(2)=3−(20−6×3)=7(3)−20=7(23−20)−20=7(23)−8(20)=7(23)−8(43−23)=15(23)−8(43). Modulo 43, this gives 15×23≡1(mod43), so y2=15. (Watch out: take the coefficient 15 attached to 23, not the coefficient −8≡35(mod43) attached to 43, which would give 59,500; and don't stop at y2=15 without multiplying by 1,700.) Multiplying the base product by y2=15 gives the (0,1,0) unit switch 1,700×15=25,500.
6
Suppose two integers x and x′ leave the same remainder when divided by 8, the same remainder when divided by 9, and the same remainder when divided by 25. Prove that x−x′ is a multiple of 1,800, and give a counterexample showing why x−x′ need not be a multiple of 8×12×25=2,400 if the moduli are 8,12, and 25 instead.
Check the remainder of the difference x−x′ when divided by each modulus, then compare the prime factorizations of 8,9,25 with those of 8,12,25.
Because x≡x′ in mod 8,mod 9, and mod 25, the difference x−x′ leaves remainder 0 when divided by 8,9, and 25, so 8,9, and 25 each divide x−x′. Since 8=23,9=32, and 25=52 share no prime factors, they are pairwise coprime, so the prime factorization of x−x′ must contain 23,32, and 52 separately, and x−x′ is divisible by their product 8×9×25=1,800. (Watch out: if you multiply the moduli without checking that they are pairwise coprime, you will falsely assume the cycle for 8,12, and 25 is 8×12×25=2,400.) If the moduli are 8,12, and 25, they are not pairwise coprime because gcd(8,12)=4; for example, x=600 and x′=0 both leave remainder 0 when divided by 8,12, and 25, yet x−x′=600.