Suppose that instead of one linear congruence, we now want to solve several of them at the same time; that is, given ai,ci∈Z and ni∈Z+, we want to find every x satisfying the whole system of simultaneous congruences
Each individual congruence is solved exactly as in Linear Congruences and Diophantine Equations; the new part is stitching the answers together so that one value of x works for every modulus simultaneously.
Solve the first congruence for x, and write x in terms of a parameter k1∈Z.
Substitute this expression for x into the second congruence.
Solve that congruence for k1, and write k1 in terms of a new parameter k2∈Z.
Rewrite x in terms of k2, and substitute it into the third congruence.
Repeat until every congruence has been used. The final expression for x in terms of kt, for all kt∈Z, describes every solution.
Basically, solving the first congruence narrows x down to one family of integers; each later congruence then filters that family further, and the parameter ki just keeps track of whatever freedom is left. If the system has no solution at all, this is not a problem for the method; one of the intermediate congruences will simply turn out to be unsolvable, and we can stop there.
Example. Solve 3x≡4(mod5), 5x≡6(mod7), and 7x≡8(mod9) simultaneously.
Starting with the first congruence; since 3⋅2=6≡1(mod5), the inverse of 3 modulo 5 is 2,
3x6xxx≡4(mod5)≡8(mod5)≡3(mod5)=3+5k1,k1∈Z.(multiplying both sides by 2)
Substituting this into the second congruence,
5(3+5k1)15+25k11+4k14k18k1k1k1≡6(mod7)≡6(mod7)≡6(mod7)≡5(mod7)≡10(mod7)≡3(mod7)=3+7k2,k2∈Z.(15≡1 and 25≡4mod7)(multiplying by 2, the inverse of 4)
Rewriting x in terms of k2,
x=3+5(3+7k2)=18+35k2,
and substituting this into the third congruence,
7(18+35k2)126+245k20+2k2k2k2≡8(mod9)≡8(mod9)≡8(mod9)≡4(mod9)=4+9k3,k3∈Z.(126=14×9 and 245=27×9+2)(dividing by 2, valid as gcd(2,9)=1)
Finally,
x=18+35(4+9k3)=158+315k3.
Therefore, the solution is x≡158(mod315); notice how the final modulus 315=5×7×9 is the product of all three original moduli.
It is always worth checking the answer by substituting it back into each original congruence:
All three hold, so x≡158(mod315) is correct. This check takes seconds and catches basically every arithmetic slip, so it is well worth doing in an exam.
Here gcd(6,8)=2 and 2∤7, so by the existence theorem this congruence has no solution. Therefore, the system has no simultaneous solution.
Alternatively, we could have seen this coming without doing any substitution. Reducing the first and third congruences on their own gives x≡5(mod6) and (multiplying 3x≡2(mod8) by 3, since 3⋅3=9≡1(mod8)) x≡6(mod8). Working modulo lcm(6,8)=24: any solution of the first congruence must be 5,11,17 or 23 modulo 24, while any solution of the third must be 6,14 or 22 modulo 24. There is no overlap between the two lists, so no x can satisfy both. Even quicker: x≡5(mod6) forces x to be odd, while x≡6(mod8) forces x to be even.
It is important to remember that when the moduli share common factors, a simultaneous solution is not guaranteed to exist; the substitution method will always expose this by producing an unsolvable congruence part-way through.
The failure above happened because 6 and 8 share a factor of 2. If we forbid that from happening — i.e. if every pair of moduli is coprime — then it turns out the system can never be inconsistent.
Note
Chinese Remainder Theorem Suppose that for the system of congruences
xxx≡c1(modn1),≡c2(modn2),⋮≡ct(modnt),
all the moduli are pairwise coprime; that is, gcd(ni,nj)=1 for all i=j. Then the system of congruences has a solution, and the solution is unique modulo n1n2…nt.
Basically, when the moduli have nothing in common, the remainders (c1,c2,…,ct) act like independent coordinates; no congruence can interfere with another, so you can always find a number hitting all of the required remainders at once, and exactly one such number exists in each window of length n1n2…nt.
Proof (sketch). Uniqueness falls out of the substitution method: in the final expression for x, the parameter kt ends up multiplied by all of the moduli, so any two solutions differ by a multiple of n1n2…nt. Existence is a counting argument; each of the n1n2…nt elements of Zn1n2…nt has some tuple of residues (c1,…,ct), no two elements can share the same tuple (by the uniqueness we just argued), and there are exactly n1n2…nt possible tuples; so every tuple, including the one we want, is achieved by exactly one element.
It is important to note that the CRT only tells you a solution exists; to actually find it, you still run the substitution method. Also notice that the theorem is stated for congruences of the form x≡ci; if you are given aix≡ci instead, solve each congruence individually first to reduce it to that form.
Example. Solve the system of congruences
xxx≡3(mod7),≡2(mod8),≡1(mod9).
The moduli are pairwise coprime (gcd(7,8)=gcd(7,9)=gcd(8,9)=1), so the CRT guarantees a unique solution modulo 7×8×9=504 before we even start.
The first congruence gives x=3+7k1 for k1∈Z immediately. Substituting into the second,
so x=3+7(1+8k2)=10+56k2. Substituting into the third,
10+56k21+2k22k2k2k2≡1(mod9)≡1(mod9)≡0(mod9)≡0(mod9)=9k3,k3∈Z.(10≡1 and 56≡2mod9)(dividing by 2, valid as gcd(2,9)=1)
Finally,
x=10+56(9k3)=10+504k3.
Therefore, the solution is x≡10(mod504); checking, 10=7+3, 10=8+2 and 10=9+1, so all three congruences hold.
Example. (The original puzzle of Sun Tzu.) Find the smallest positive integer that leaves a remainder of 2 when divided by 3, a remainder of 3 when divided by 5, and a remainder of 2 when divided by 7.
This is just the system x≡2(mod3), x≡3(mod5), x≡2(mod7). The moduli 3,5,7 are pairwise coprime, so by the CRT there is a unique solution modulo 3×5×7=105.
From the first congruence, x=2+3k1 for k1∈Z. Substituting into the second,
2+3k13k16k1k1k1≡3(mod5)≡1(mod5)≡2(mod5)≡2(mod5)=2+5k2,k2∈Z,(multiplying by 2, the inverse of 3)
so x=2+3(2+5k2)=8+15k2. Substituting into the third,
8+15k21+k2k2k2≡2(mod7)≡2(mod7)≡1(mod7)=1+7k3,k3∈Z,(8≡1 and 15≡1mod7)
giving
x=8+15(1+7k3)=23+105k3.
Therefore, the smallest positive such integer is x=23; indeed 23=7(3)+2=4(5)+3=3(7)+2.
You already know what an lcm is, so I'm not gonna write much;
Note
Definition
The least common multiple of a set of integers, written lcm(), is the smallest natural number that is a multiple of every number in the set.
For two integers there is a handy formula linking it to the gcd,
lcm(a,b)=gcd(a,b)ab.
For more than two numbers, the quickest approach by hand is to prime factorise everything and take the highest power of each prime that appears.
Note
Generalised Chinese Remainder Theorem Suppose that the system of congruences
has a solution, and that gcd(ai,ni)=1 for all i. Then the solution is unique modulo lcm(n1,n2,…,nt).
Basically, this version drops the requirement that the moduli be pairwise coprime, but it charges a price twice over: it no longer promises that a solution exists (you have to be told that, or discover it yourself), and the uniqueness is only modulo the lcm of the moduli rather than their product. The reason the lcm appears is that during the solving process we divide congruences through by the relevant gcds, which shrinks the moduli; whatever overlap the moduli share only gets counted once. Notice that when the moduli are pairwise coprime, lcm(n1,…,nt)=n1n2…nt, so this genuinely generalises the ordinary CRT.
Proof (sketch). The argument is the same as for the ordinary CRT; running the substitution method, the final parameter is multiplied by the moduli with the shared gcd factors divided out, which is precisely the lcm.
Example. Given that x=100 is a solution to the system of congruences
5x6x7x8x≡2(mod6),≡5(mod7),≡4(mod8),≡8(mod9),
find all solutions to the system.
First check the hypotheses: gcd(5,6)=gcd(6,7)=gcd(7,8)=gcd(8,9)=1, and we are told a solution exists, so the Generalised CRT applies; the solution is unique modulo lcm(6,7,8,9). Prime factorising,
6=2×3,7=7,8=23,9=32,
so taking the highest power of each prime,
lcm(6,7,8,9)=23×32×7=504.
Therefore, the complete solution is x≡100(mod504); no substitution work needed at all. As a sanity check on the given solution,
so x=100 really does satisfy all four congruences. Note that the moduli here are not pairwise coprime (gcd(6,8)=2 and gcd(6,9)=3), which is exactly why the answer is unique modulo 504 and not modulo 6×7×8×9=3024.
When only two congruences with non-coprime moduli are involved, there is a quick test for whether a solution exists at all.
Note
Proposition The system x≡c1(modn1), x≡c2(modn2) has a solution if and only if
gcd(n1,n2)∣(c1−c2),
and when a solution exists, it is unique modulo lcm(n1,n2).
To see why: writing x=c1+n1k1 and substituting into the second congruence gives n1k1≡c2−c1(modn2), which is a linear congruence in k1; it is solvable precisely when gcd(n1,n2)∣(c2−c1).
Basically, both congruences are making a claim about x modulo the shared factor gcd(n1,n2), and those two claims had better agree; if they do, everything else is independent and the system behaves like a coprime one.
Example. Solve x≡1(mod4) and x≡5(mod6) simultaneously.
Checking consistency first: gcd(4,6)=2 and 2∣(5−1), so a solution exists and will be unique modulo lcm(4,6)=12. Writing x=1+4k1 and substituting into the second congruence,
1+4k14k12k1k1k1≡5(mod6)≡4(mod6)≡2(mod3)≡1(mod3)=1+3k2,k2∈Z.(dividing everything, modulus included, by 2)(dividing by 2, valid as gcd(2,3)=1)
Therefore,
x=1+4(1+3k2)=5+12k2,
i.e. x≡5(mod12). Notice how the final modulus is lcm(4,6)=12 and not 4×6=24; the division by 2 in the middle of the working is exactly where the difference comes from. Checking: 5=4+1 and 5=0(6)+5, so both congruences hold.
Example. Show that the system x≡2(mod4) and x≡3(mod6) has no solution.
Using the consistency condition: gcd(4,6)=2, but c1−c2=2−3=−1 and 2∤−1; so the system is inconsistent and we are done. To see the same thing through the substitution method, write x=2+4k1 and substitute,
2+4k14k1≡3(mod6)≡1(mod6),
and since gcd(4,6)=2 with 2∤1, there is no solution for k1. Therefore, the system has no solution; intuitively, the first congruence forces x to be even while the second forces x to be odd, and both claims are statements about x modulo the common factor 2.
There is also a direct formula-style algorithm for coprime systems, which builds the answer in one hit rather than by repeated substitution. Given the system x≡ci(modni) for 1≤i≤t with gcd(ni,nj)=1 for all i=j:
Set n=n1n2…nt.
For each i, solve the congruence ninxi≡1(modni) for xi.
The general solution is
x≡n1nx1c1+n2nx2c2+⋯+ntnxtct(modn).
The trick making this work is that each term ninxici is congruent to ci modulo ni (since ninxi≡1(modni)), but is congruent to 0 modulo every other nj (since nj∣nin); so the terms never interfere with each other, and the sum hits the right remainder for every modulus at once.
This algorithm is not recommended for solving by hand; the multiplications and reductions in the final step are time-consuming, and you still have to solve just as many linear congruences as the substitution method requires. It is mainly of interest because it gives an explicit construction for the CRT (and computers like it).
Example. Use the CRT Algorithm to solve the earlier system x≡3(mod7), x≡2(mod8), and x≡1(mod9).
Here n=7×8×9=504, and n1n=72, n2n=63, n3n=56. Solving the three auxiliary congruences:
CRT problems love to disguise themselves as word problems; the skill being tested is translating "remainder" language into a congruence system, deciding which theorem applies, and then grinding through the substitution method.
Example. (Brahmagupta's egg basket.) A basket contains some eggs. When the eggs are removed 2,3,4,5 or 6 at a time, one egg is always left over; when they are removed 7 at a time, none are left over. What is the smallest possible number of eggs in the basket?
Translating into congruences, we need
x≡1(mod2,3,4,5,6)andx≡0(mod7).
The first five moduli are certainly not pairwise coprime, so we cannot throw the CRT at this directly. Instead, notice that the five congruences x≡1 all say the same thing: x−1 is divisible by each of 2,3,4,5,6, which happens exactly when x−1 is divisible by their lcm. Since lcm(2,3,4,5,6)=60, the whole first batch collapses into the single congruence
x≡1(mod60).
Now gcd(60,7)=1, so by the Chinese Remainder Theorem the reduced system has a unique solution modulo 60×7=420. Writing x=1+60k and substituting into x≡0(mod7),
1+60k1+4k4k8kkk≡0(mod7)≡0(mod7)≡6(mod7)≡12(mod7)≡5(mod7)=5+7m,m∈Z,(60=8×7+4)(multiplying by 2, the inverse of 4)
so
x=1+60(5+7m)=301+420m.
Therefore, the smallest possible number of eggs is 301; indeed 301=43×7, and 301=300+1 leaves remainder 1 on division by each of 2,3,4,5 and 6 since 60∣300.
Example. A general lines up her soldiers in rows of 7 and finds 3 left over; in rows of 8, 5 are left over; in rows of 9, 2 are left over. Given that she has between 1000 and 1500 soldiers, exactly how many does she have?
The system is x≡3(mod7), x≡5(mod8), x≡2(mod9), with pairwise coprime moduli; so the CRT promises a unique answer modulo 504, and the range restriction of width 500<504 will then pin down a single number.
From the first congruence, x=3+7k1 for k1∈Z. Substituting into the second,
so x=3+7(6+8k2)=45+56k2. Substituting into the third,
45+56k20+2k2k2k2≡2(mod9)≡2(mod9)≡1(mod9)=1+9k3,k3∈Z,(45=5×9 and 56=6×9+2)(dividing by 2, valid as gcd(2,9)=1)
giving
x=45+56(1+9k3)=101+504k3.
The solutions are 101,605,1109,1613,…, and the only one lying between 1000 and 1500 is x=101+2(504)=1109.
Therefore, the general has exactly 1109 soldiers; checking, 1109=158(7)+3=138(8)+5=123(9)+2.