Congruence Solver and the Chinese Remainder Theorem
After reading this you can solve any single linear congruence ax \equiv b \pmod{m}, count how many solutions it has, and stitch several congruences into one using the Chinese Remainder Theorem.
What a congruence solver does
A congruence ax \equiv b \pmod{m} asks: for which integers x does ax leave the same remainder as b when divided by m? It is an equation, but instead of one answer it usually has a whole family of answers spaced m apart, or a subfamily spaced more widely.
Here is a quick hook. Solve 6x \equiv 4 \pmod{10}. Try each residue from 0 to 9. You find 6 \cdot 4 = 24 \equiv 4 and 6 \cdot 9 = 54 \equiv 4. So x = 4 and x = 9 both work, and every integer congruent to those mod 10 works too. That is two solution classes, not one, and the solver reports both.
The second job is systems. Given several conditions like "remainder 2 mod 3, remainder 3 mod 5, remainder 2 mod 7", the Chinese Remainder Theorem (CRT) combines them into a single congruence with a single answer mod the product of the moduli.
When to use it, and when not
Use the linear mode when you have exactly one modular equation and want every x that satisfies it. Use the CRT mode when you have several remainder facts about the same unknown, one per modulus, and you want the unknown itself.
Two limits are worth stating up front. First, a linear congruence has a solution only when \gcd(a, m) divides b. If it does not, there is no x at all. Second, the clean version of CRT needs the moduli to be pairwise coprime. When they share factors, a solution may still exist, but only if the overlapping conditions agree.
If you are solving ax + by = c over the integers rather than a modular equation, that is a different but related problem. See the Linear Diophantine Equations solver, which uses the same extended Euclidean algorithm underneath.
The formula and the intuition
Let g = \gcd(a, m). The congruence ax \equiv b \pmod{m} behaves in exactly one of these ways.
Here g \nmid b means g does not divide b. When g does divide b, reduce the whole congruence by g:
Now a/g and m/g are coprime, so a/g has a modular inverse. Multiply by it to get one base solution x_0. The full set of classes mod m is then
The intuition: dividing by g shrinks the problem to a coprime one that always has a unique inverse. Lifting back up spreads that single answer into g equally spaced classes across the original modulus. In the hook example g = \gcd(6,10) = 2 divides 4, so you get 2 classes, 4 and 9, spaced m/g = 5 apart.
The Chinese Remainder Theorem
Suppose you want x with x \equiv r_i \pmod{m_i} for several moduli that are pairwise coprime. Let M = m_1 m_2 \cdots m_n. Then a unique solution exists mod M, and you can build it directly.
Read the pieces: M_i is the product of all moduli except m_i, so it is divisible by every other modulus and leaves the i-th condition free to be set. The inverse y_i tunes M_i so that M_i y_i \equiv 1 \pmod{m_i}. Each term contributes the correct remainder in its own modulus and contributes 0 in all the others.
Worked example: reproducing the demo
Solve the system 2 mod 3, 3 mod 5, 2 mod 7
Load the demo data (mode = crt with lines 2, 3, 3, 5, 2, 7) and follow the steps.
- The moduli
3,5,7are pairwise coprime, so a unique answer exists mod M = 3 \cdot 5 \cdot 7 = 105. - Compute the partial products: M_1 = 35, M_2 = 21, M_3 = 15.
- Find each inverse. 35 \equiv 2 \pmod 3 and 2 \cdot 2 = 4 \equiv 1, so y_1 = 2. 21 \equiv 1 \pmod 5, so y_2 = 1. 15 \equiv 1 \pmod 7, so y_3 = 1.
- Assemble: x = 2 \cdot 35 \cdot 2 + 3 \cdot 21 \cdot 1 + 2 \cdot 15 \cdot 1 = 140 + 63 + 30 = 233.
- Reduce mod
105: 233 - 2 \cdot 105 = 23. So x \equiv 23 \pmod{105}.
Verify all three conditions: 23 = 7 \cdot 3 + 2 gives remainder 2 mod 3; 23 = 4 \cdot 5 + 3 gives remainder 3 mod 5; 23 = 3 \cdot 7 + 2 gives remainder 2 mod 7. All match.
Reading and interpreting the results
For a linear congruence, the number that matters most is the count of solution classes, which equals \gcd(a, m) when a solution exists. The table below shows how the same right side can produce a different number of classes depending on a and m.
| Congruence | g = gcd(a, m) | g divides b? | Classes | Solutions |
|---|---|---|---|---|
| 5x ≡ 3 (mod 12) | 1 | yes | 1 | 3 |
| 4x ≡ 8 (mod 12) | 4 | yes | 4 | 2, 5, 8, 11 |
| 4x ≡ 6 (mod 12) | 4 | no | 0 | none |
| 6x ≡ 6 (mod 12) | 6 | yes | 6 | 1, 3, 5, 7, 9, 11 |
Notice the pattern. When g = 1, there is exactly one class, the tidy case. When g grows and still divides b, the classes multiply and sit m/g apart. When g does not divide b, the answer set is empty, as in the third row where 4 does not divide 6.
For a CRT system, the result is a single class x \equiv x_0 \pmod{M}. Read x_0 as the smallest nonnegative solution and M as the period at which solutions repeat. In the demo, x_0 = 23 and M = 105, so the full solution set is \{23, 128, 233, \dots\} and their negatives.
Common mistakes
The errors below account for most wrong answers.
- Reporting one solution when there are several
- If \gcd(a, m) = g \gt 1 and it divides
b, you getgclasses, not one. For 6x \equiv 4 \pmod{10} the answers are both4and9. - Forcing an inverse that does not exist
- You can only "divide by
a" when \gcd(a, m) = 1. When it is not1, reduce bygfirst, then invert. - Applying CRT to non-coprime moduli blindly
- The formula above assumes pairwise coprime moduli. For moduli like
6and4(shared factor2), a solution exists only if the two conditions agree mod \gcd(6,4) = 2. - Skipping verification
- Always substitute the reported
xback into every original congruence. A single arithmetic slip in an inverse is easy to catch this way.
A negative remainder is not a mistake in the input, but it is confusing to compare. Normalize each residue to the range 0 to m-1 before checking, since -2 \equiv 5 \pmod 7 and both name the same class.
Related tools
Congruence problems sit next to several other discrete topics on this site. The Prime Number Tools help you factor a modulus and confirm which moduli are coprime, which is the first thing CRT needs. The Base-N Arithmetic page is handy when a modulus is a power of a base and you want to read remainders straight off digits. For logic problems rather than number problems, the Truth Table Generator and the Set Calculator cover the neighboring ground.
Frequently asked questions
Why does my linear congruence have no solution?
Because \gcd(a, m) does not divide b. For 4x \equiv 6 \pmod{12}, the gcd is 4, and 4 does not divide 6, so no integer x works.
How many answers should I expect from a single congruence?
Exactly \gcd(a, m) distinct classes mod m, provided a solution exists. If the gcd is 1 you get one class; if it is 3 you get three, spaced m/3 apart.
Do the moduli in a CRT system have to be prime?
No, they only need to be pairwise coprime. The moduli 8, 9, 25 are all composite yet pairwise coprime, so CRT gives a unique answer mod 8 \cdot 9 \cdot 25 = 1800.
Can I combine two congruences with the same modulus?
Only if they agree. x \equiv 2 \pmod 5 and x \equiv 3 \pmod 5 contradict each other, so no x exists. Two identical conditions collapse to one.
What is the range of the CRT answer?
The unique solution is reported in the range 0 to M-1, where M is the product of the moduli. In the demo that range is 0 to 104, and the answer is 23.