Simultaneous Congruences and the CRT
Simultaneous Congruences
Suppose that instead of one linear congruence, we now want to solve several of them at the same time; that is, given and , we want to find every 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 works for every modulus simultaneously.
The Substitution Method
The general method is repeated substitution:
- Solve the first congruence for , and write in terms of a parameter .
- Substitute this expression for into the second congruence.
- Solve that congruence for , and write in terms of a new parameter .
- Rewrite in terms of , and substitute it into the third congruence.
- Repeat until every congruence has been used. The final expression for in terms of , for all , describes every solution.
Basically, solving the first congruence narrows down to one family of integers; each later congruence then filters that family further, and the parameter 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 , , and simultaneously.
Starting with the first congruence; since , the inverse of modulo is ,
Substituting this into the second congruence,
Rewriting in terms of ,
and substituting this into the third congruence,
Finally,
Therefore, the solution is ; notice how the final modulus 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 is correct. This check takes seconds and catches basically every arithmetic slip, so it is well worth doing in an exam.
Systems with No Solution
Example. Solve , , and simultaneously.
For the first congruence, notice that ,
Substituting into the second congruence,
so . Substituting into the third congruence,
Here and , 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 and (multiplying by , since ) . Working modulo : any solution of the first congruence must be or modulo , while any solution of the third must be or modulo . There is no overlap between the two lists, so no can satisfy both. Even quicker: forces to be odd, while forces 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 Chinese Remainder Theorem
The failure above happened because and share a factor of . 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
all the moduli are pairwise coprime; that is, for all . Then the system of congruences has a solution, and the solution is unique modulo .
Basically, when the moduli have nothing in common, the remainders 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 .
Proof (sketch). Uniqueness falls out of the substitution method: in the final expression for , the parameter ends up multiplied by all of the moduli, so any two solutions differ by a multiple of . Existence is a counting argument; each of the elements of has some tuple of residues , no two elements can share the same tuple (by the uniqueness we just argued), and there are exactly 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 ; if you are given instead, solve each congruence individually first to reduce it to that form.
Example. Solve the system of congruences
The moduli are pairwise coprime (), so the CRT guarantees a unique solution modulo before we even start.
The first congruence gives for immediately. Substituting into the second,
so . Substituting into the third,
Finally,
Therefore, the solution is ; checking, , and , so all three congruences hold.
Example. (The original puzzle of Sun Tzu.) Find the smallest positive integer that leaves a remainder of when divided by , a remainder of when divided by , and a remainder of when divided by .
This is just the system , , . The moduli are pairwise coprime, so by the CRT there is a unique solution modulo .
From the first congruence, for . Substituting into the second,
so . Substituting into the third,
giving
Therefore, the smallest positive such integer is ; indeed .
The Generalised Chinese Remainder Theorem
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 , 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,
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 for all . Then the solution is unique modulo .
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, , 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 is a solution to the system of congruences
find all solutions to the system.
First check the hypotheses: , and we are told a solution exists, so the Generalised CRT applies; the solution is unique modulo . Prime factorising,
so taking the highest power of each prime,
Therefore, the complete solution is ; no substitution work needed at all. As a sanity check on the given solution,
so really does satisfy all four congruences. Note that the moduli here are not pairwise coprime ( and ), which is exactly why the answer is unique modulo and not modulo .
The Consistency Condition
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 , has a solution if and only if
and when a solution exists, it is unique modulo .
To see why: writing and substituting into the second congruence gives , which is a linear congruence in ; it is solvable precisely when .
Basically, both congruences are making a claim about modulo the shared factor , and those two claims had better agree; if they do, everything else is independent and the system behaves like a coprime one.
Example. Solve and simultaneously.
Checking consistency first: and , so a solution exists and will be unique modulo . Writing and substituting into the second congruence,
Therefore,
i.e. . Notice how the final modulus is and not ; the division by in the middle of the working is exactly where the difference comes from. Checking: and , so both congruences hold.
Example. Show that the system and has no solution.
Using the consistency condition: , but and ; so the system is inconsistent and we are done. To see the same thing through the substitution method, write and substitute,
and since with , there is no solution for . Therefore, the system has no solution; intuitively, the first congruence forces to be even while the second forces to be odd, and both claims are statements about modulo the common factor .
The CRT Algorithm (Extension)
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 for with for all :
- Set .
- For each , solve the congruence for .
- The general solution is
The trick making this work is that each term is congruent to modulo (since ), but is congruent to modulo every other (since ); 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 , , and .
Here , and , , . Solving the three auxiliary congruences:
Assembling the general solution,
Therefore, , agreeing with the substitution method; but notice how much heavier the arithmetic was for the same answer.
Classic Puzzles and Applications
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 or at a time, one egg is always left over; when they are removed at a time, none are left over. What is the smallest possible number of eggs in the basket?
Translating into congruences, we need
The first five moduli are certainly not pairwise coprime, so we cannot throw the CRT at this directly. Instead, notice that the five congruences all say the same thing: is divisible by each of , which happens exactly when is divisible by their lcm. Since , the whole first batch collapses into the single congruence
Now , so by the Chinese Remainder Theorem the reduced system has a unique solution modulo . Writing and substituting into ,
so
Therefore, the smallest possible number of eggs is ; indeed , and leaves remainder on division by each of and since .
Example. A general lines up her soldiers in rows of and finds left over; in rows of , are left over; in rows of , are left over. Given that she has between and soldiers, exactly how many does she have?
The system is , , , with pairwise coprime moduli; so the CRT promises a unique answer modulo , and the range restriction of width will then pin down a single number.
From the first congruence, for . Substituting into the second,
so . Substituting into the third,
giving
The solutions are , and the only one lying between and is .
Therefore, the general has exactly soldiers; checking, .