Skip to content

Linear Congruences and the Chinese Remainder Theorem

5.1 Solving ax≡b(modn)ax \equiv b \pmod n

Section titled “5.1 Solving ax≡b(modn)ax \equiv b \pmod nax≡b(modn)”

Theorem. The congruence ax≡b(modn)ax \equiv b \pmod n is solvable if and only if gcd⁡(a,n)∣b\gcd(a, n) \mid b. When solvable, there are exactly gcd⁡(a,n)\gcd(a, n) incongruent solutions modulo nn.

Method. Let d=gcd⁡(a,n)d = \gcd(a, n). Dividing through by dd reduces to

a′x≡b′(modn′),a′=a/d,  b′=b/d,  n′=n/d,gcd⁡(a′,n′)=1,a' x \equiv b' \pmod{n'}, \qquad a' = a/d,\; b' = b/d,\; n' = n/d, \qquad \gcd(a', n') = 1,

whose solution is x≡b′⋅(a′)−1(modn′)x \equiv b' \cdot (a')^{-1} \pmod{n'} with the inverse from the extended Euclidean algorithm. Modulo nn the full solution set is x≡x0+kn′(modn)x \equiv x_0 + k n' \pmod n, k=0,…,d−1k = 0, \ldots, d-1.

Worked example. Solve 6x≡15(mod21)6x \equiv 15 \pmod{21}.

d=gcd⁡(6,21)=3d = \gcd(6, 21) = 3 and 3∣153 \mid 15, so there are 3 solutions modulo 21. Divide: 2x≡5(mod7)2x \equiv 5 \pmod 7. Since 2⋅4=8≡1(mod7)2 \cdot 4 = 8 \equiv 1 \pmod 7, the inverse of 2 is 4: x≡20≡6(mod7)x \equiv 20 \equiv 6 \pmod 7. Modulo 21 the solutions are x≡6,13,20(mod21)x \equiv 6, 13, 20 \pmod{21}.

Theorem (CRT). Let m1,m2m_1, m_2 be coprime. Then the system

x≡a1(modm1),x≡a2(modm2)x \equiv a_1 \pmod{m_1}, \qquad x \equiv a_2 \pmod{m_2}

has a unique solution modulo m1m2m_1 m_2.

Proof. Existence: set M=m1m2M = m_1 m_2, M1=m2M_1 = m_2, M2=m1M_2 = m_1. Since gcd⁡(M1,m1)=1\gcd(M_1, m_1) = 1, let y1y_1 be the inverse of M1M_1 modulo m1m_1 (extended Euclid). Then

x0=a1M1y1+a2M2y2,M2y2≡1(modm2)x_0 = a_1 M_1 y_1 + a_2 M_2 y_2, \qquad M_2 y_2 \equiv 1 \pmod{m_2}

satisfies both congruences: modulo m1m_1, every term containing M2=m1M_2 = m_1 vanishes and M1y1≡1M_1 y_1 \equiv 1; modulo m2m_2 symmetrically.

Uniqueness: if x1≡x2x_1 \equiv x_2 modulo both m1m_1 and m2m_2 then m1m2∣x1−x2m_1 m_2 \mid x_1 - x_2 (coprimality). ■\blacksquare

Worked example. Solve x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5, x≡2(mod7)x \equiv 2 \pmod 7 (Sunzi’s original problem, ~4th century CE).

M=105M = 105. M1=35M_1 = 35, M2=21M_2 = 21, M3=15M_3 = 15. Inverses: 35≡2(mod3)35 \equiv 2 \pmod 3, 2−1=22^{-1} = 2; 21≡1(mod5)21 \equiv 1 \pmod 5, inverse 1; 15≡1(mod7)15 \equiv 1 \pmod 7, inverse 1.

x≡2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=140+63+30=233≡23(mod105).x \equiv 2 \cdot 35 \cdot 2 + 3 \cdot 21 \cdot 1 + 2 \cdot 15 \cdot 1 = 140 + 63 + 30 = 233 \equiv 23 \pmod{105}.

For non-coprime moduli the CRT condition becomes: the system x≡a1(modm1)x \equiv a_1 \pmod{m_1}, x≡a2(modm2)x \equiv a_2 \pmod{m_2} is solvable iff a1≡a2(modgcd⁡(m1,m2))a_1 \equiv a_2 \pmod{\gcd(m_1, m_2)}, and solutions are unique modulo lcm(m1,m2)\mathrm{lcm}(m_1, m_2). Combine pairwise, replacing a pair by its single lcm-form congruence.

  • Calendar cycles (Sunzi’s original use), and the Zeller-type day-of-week congruences.
  • Residue number systems: represent a large integer by its residues modulo pairwise coprime moduli; addition and multiplication act componentwise with no carries; the hardware basis of RNS arithmetic.
  • Exponent reduction on composite moduli: to compute 71000(mod55)7^{1000} \pmod{55}, split into mod 5 and mod 11 (Fermat on each prime factor), solve by CRT. This “split through the prime factors” workflow is the standard exam technique.
  • The CRT uniqueness is modulo the product; infinitely many integer solutions exist (all congruent modulo MM).
  • The reconstruction formula requires the inverses of MiM_i modulo mim_i; a common slip is inverting mim_i instead.
  • Non-coprime moduli: check a1≡a2(modgcd⁡(m1,m2))a_1 \equiv a_2 \pmod{\gcd(m_1, m_2)} first, otherwise the system has no solution at all (students often “solve” impossible systems).

Solve x≡1(mod6)x \equiv 1 \pmod 6, x≡7(mod10)x \equiv 7 \pmod{10}: gcd⁡(6,10)=2\gcd(6, 10) = 2 and 1≡7≡1(mod2)1 \equiv 7 \equiv 1 \pmod 2, so solutions exist. Combine into x≡7(modlcm(6,10)=30)x \equiv 7 \pmod{\mathrm{lcm}(6,10) = 30}, i.e. x=7,37,…x = 7, 37, \ldots

x=a1M1y1+a2M2y2,Mi=M/mi,M=m1m2x = a_1 M_1 y_1 + a_2 M_2 y_2, \qquad M_i = M/m_i, \quad M = m_1 m_2

The reconstruction formula is worth memorising as a pattern, not as symbols: take the product of the other moduli, multiply by its inverse modulo your modulus, multiply by your residue, and sum over congruences. A worked three-modulus example with M=105M = 105 appears on this page; recompute it end-to-end as revision. Always finish with the residue class statement: solutions are classes modulo the product, and writing x≡x \equiv the found value makes the answer unambiguous for the marker.

Alternatively solve by substitution: from x=2+3kx = 2 + 3k into x≡3(mod5)x \equiv 3 \pmod 5 gives 3k≡1(mod5)3k \equiv 1 \pmod 5, k≡2k \equiv 2, so x=8+15tx = 8 + 15t; substituting into x≡2(mod7)x \equiv 2 \pmod 7 gives t≡4(mod7)t \equiv 4 \pmod 7 and x=68+105tx = 68 + 105t; the same class as the CRT formula.