Academy
Maths›Number Theory›Gcd Lcm›GCD and Modular Arithmetic

GCD and Modular Arithmetic

GCD determines which linear congruences are solvable, how many solutions they have, and when inverses exist modulo m. Includes Euler's totient and the Chinese Remainder Theorem.

A linear Diophantine equation ax+by=cax + by = c and a linear congruence ax≡c(modb)ax \equiv c \pmod b are the same object, written differently.

In ax+by=cax + by = c, you're asking: does there exist an integer xx such that c−axc - ax is a multiple of bb? That's exactly ax≡c(modb)ax \equiv c \pmod b.

So everything from the Diophantine theory translates directly. But the modular language opens new questions — particularly about inverses — that are worth developing on their own.


When is aa invertible modulo mm?

Question. When does ax≡1(modm)ax \equiv 1 \pmod m have a solution?

This is asking: when does aa have a multiplicative inverse mod mm?

Theorem. aa has an inverse modulo mm if and only if gcd⁡(a,m)=1\gcd(a, m) = 1.

Proof. ax≡1(modm)ax \equiv 1 \pmod m means ax−1=myax - 1 = my for some integer yy, i.e., ax+m(−y)=1ax + m(-y) = 1. By Bezout, this is solvable iff gcd⁡(a,m)∣1\gcd(a, m) \mid 1, i.e., gcd⁡(a,m)=1\gcd(a, m) = 1. ■\blacksquare

When gcd⁡(a,m)=1\gcd(a, m) = 1, the Bezout coefficients give the inverse directly.

Example. Find 3−1(mod7)3^{-1} \pmod 7.

From Bezout: 3(5)+7(−2)=13(5) + 7(-2) = 1. So 3×5≡1(mod7)3 \times 5 \equiv 1 \pmod 7. Thus 3−1≡5(mod7)3^{-1} \equiv 5 \pmod 7.


Solving ax≡b(modm)ax \equiv b \pmod m

Theorem. The congruence ax≡b(modm)ax \equiv b \pmod m has a solution if and only if d=gcd⁡(a,m)d = \gcd(a, m) divides bb.

When solutions exist, there are exactly dd solutions modulo mm.

Method:

  1. Check d=gcd⁡(a,m)d = \gcd(a,m) divides bb. If not, no solution.
  2. Divide through by dd: adx≡bd(modmd)\frac{a}{d} x \equiv \frac{b}{d} \pmod{\frac{m}{d}}.
  3. Now gcd⁡(a/d,m/d)=1\gcd(a/d, m/d) = 1, so a/da/d is invertible mod m/dm/d. Find the inverse.
  4. One solution mod m/dm/d. Lift to dd solutions mod mm by adding multiples of m/dm/d.

Example. Solve 6x≡4(mod10)6x \equiv 4 \pmod{10}.

d=gcd⁡(6,10)=2d = \gcd(6,10) = 2 and 2∣42 \mid 4. ✓

Divide: 3x≡2(mod5)3x \equiv 2 \pmod 5.

Inverse of 3 mod 5: 3×2=6≡1(mod5)3 \times 2 = 6 \equiv 1 \pmod 5, so 3−1≡23^{-1} \equiv 2.

x≡2×2=4(mod5)x \equiv 2 \times 2 = 4 \pmod 5.

Lift to mod 10: x≡4x \equiv 4 or x≡9(mod10)x \equiv 9 \pmod{10}. Two solutions, matching d=2d = 2.


Euler's Totient — how many invertibles?

The number of integers in {1,2,…,m}\{1, 2, \ldots, m\} coprime to mm is denoted ϕ(m)\phi(m), Euler's totient function.

These are exactly the integers with inverses mod mm.

Formula. If m=p1a1p2a2⋯pkakm = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}, then:

ϕ(m)=m∏p∣m(1−1p)\phi(m) = m \prod_{p \mid m} \left(1 - \frac{1}{p}\right)

Examples:

  • ϕ(p)=p−1\phi(p) = p-1 for prime pp (all of 1,…,p−11, \ldots, p-1 are coprime to pp).
  • ϕ(pk)=pk−pk−1=pk−1(p−1)\phi(p^k) = p^k - p^{k-1} = p^{k-1}(p-1).
  • ϕ(1000)=1000×(1−12)(1−15)=400\phi(1000) = 1000 \times (1-\frac{1}{2})(1-\frac{1}{5}) = 400.

Euler's Theorem. If gcd⁡(a,m)=1\gcd(a, m) = 1, then aϕ(m)≡1(modm)a^{\phi(m)} \equiv 1 \pmod m.

Fermat's Little Theorem is the prime case: ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p for prime p∤ap \nmid a.


Chinese Remainder Theorem

When you have a system of congruences with different moduli, the Chinese Remainder Theorem (CRT) tells you when a simultaneous solution exists.

Theorem. The system: x≡a1(modm1),x≡a2(modm2)x \equiv a_1 \pmod{m_1}, \quad x \equiv a_2 \pmod{m_2}

has a unique solution modulo m1m2m_1 m_2 if and only if gcd⁡(m1,m2)=1\gcd(m_1, m_2) = 1.

Why the GCD condition? The system requires xx to be in two arithmetic progressions simultaneously. These progressions intersect iff their "step" relationship is compatible — which happens iff gcd⁡(m1,m2)∣a1−a2\gcd(m_1, m_2) \mid a_1 - a_2.

When gcd⁡(m1,m2)=1\gcd(m_1, m_2) = 1, this is always satisfied.

Construction. Let M=m1m2M = m_1 m_2, M1=m2M_1 = m_2, M2=m1M_2 = m_1.

Find y1y_1 such that M1y1≡1(modm1)M_1 y_1 \equiv 1 \pmod{m_1} (i.e., m2y1≡1(modm1)m_2 y_1 \equiv 1 \pmod{m_1}).

Find y2y_2 such that M2y2≡1(modm2)M_2 y_2 \equiv 1 \pmod{m_2}.

Then x=a1M1y1+a2M2y2(modM)x = a_1 M_1 y_1 + a_2 M_2 y_2 \pmod M is the unique solution.

Example. Solve: x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5.

gcd⁡(3,5)=1\gcd(3,5) = 1. ✓ M=15M = 15, M1=5M_1 = 5, M2=3M_2 = 3.

5y1≡1(mod3)5y_1 \equiv 1 \pmod 3: 5≡2(mod3)5 \equiv 2 \pmod 3, so 2y1≡1(mod3)2y_1 \equiv 1 \pmod 3, y1=2y_1 = 2.

3y2≡1(mod5)3y_2 \equiv 1 \pmod 5: y2=2y_2 = 2.

x=2×5×2+3×3×2=20+18=38≡8(mod15)x = 2 \times 5 \times 2 + 3 \times 3 \times 2 = 20 + 18 = 38 \equiv 8 \pmod{15}.

Check: 8=2×3+28 = 2 \times 3 + 2 ✓, 8=1×5+38 = 1 \times 5 + 3 ✓.

General CRT. For pairwise coprime m1,…,mkm_1, \ldots, m_k, the system has a unique solution modulo m1m2⋯mkm_1 m_2 \cdots m_k. The pairwise coprime condition is essential — and it's a GCD condition.


Worked problems

Problem 1. Find xx such that x≡1(mod2)x \equiv 1 \pmod 2, x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5.

gcd⁡\gcd of all pairs is 1. ✓

First combine mod 2 and mod 3: need xx odd and x≡2(mod3)x \equiv 2 \pmod 3.

x=2k+1x = 2k+1 and 2k+1≡2(mod3)  ⟹  2k≡1(mod3)  ⟹  k≡2(mod3)2k+1 \equiv 2 \pmod 3 \implies 2k \equiv 1 \pmod 3 \implies k \equiv 2 \pmod 3.

So k=3j+2k = 3j+2, x=6j+5x = 6j+5. Combined: x≡5(mod6)x \equiv 5 \pmod 6.

Now combine with mod 5: x≡5(mod6)x \equiv 5 \pmod 6 and x≡3(mod5)x \equiv 3 \pmod 5.

x=6j+5x = 6j+5 and 6j+5≡3(mod5)  ⟹  6j≡−2≡3(mod5)  ⟹  j≡3(mod5)6j+5 \equiv 3 \pmod 5 \implies 6j \equiv -2 \equiv 3 \pmod 5 \implies j \equiv 3 \pmod 5.

So j=5n+3j = 5n+3, x=6(5n+3)+5=30n+23x = 6(5n+3)+5 = 30n+23.

Answer: x≡23(mod30)x \equiv 23 \pmod{30}.

Problem 2. Find all nn such that 7∣n2+17 \mid n^2 + 1.

n2≡−1≡6(mod7)n^2 \equiv -1 \equiv 6 \pmod 7.

Check n=1,…,6n = 1,\ldots,6: 1,4,2,2,4,11,4,2,2,4,1 (squares mod 7). None equal 6.

So 7∤n2+17 \nmid n^2+1 for any nn. No solutions.

Problem 3. How many integers in {1,…,1000}\{1, \ldots, 1000\} are coprime to 1000?

1000=23×531000 = 2^3 \times 5^3.

ϕ(1000)=1000(1−12)(1−15)=1000×12×45=400\phi(1000) = 1000\left(1-\frac{1}{2}\right)\left(1-\frac{1}{5}\right) = 1000 \times \frac{1}{2} \times \frac{4}{5} = 400.

Problem 4. Prove: if pp is an odd prime, ax2+bx+c≡0(modp)ax^2 + bx + c \equiv 0 \pmod p has at most 2 solutions mod pp.

Complete the square (valid since pp is odd, so 2 is invertible): reduce to y2≡d(modp)y^2 \equiv d \pmod p. This has 0 or 2 solutions (since Z/pZ\mathbb{Z}/p\mathbb{Z} is a field — every nonzero element is either a quadratic residue with 2 roots or a non-residue with 0 roots). ■\blacksquare


The connection to look for

When a problem involves "find all nn such that [some expression] is an integer," you're usually solving a Diophantine equation. When it involves "find all nn such that [some expression] is divisible by mm," you're solving a congruence. The GCD is the gatekeeper to both.

The full problem set — mixing these techniques — is in GCD and LCM Problems.