01 / 20
≡

Solving Linear
Congruences

Learn how to solve equations of the form \( ax \equiv b \pmod{m} \) using modular inverses and the Extended Euclidean Algorithm.

Discrete Math

What Is a Congruence?

We say \( a \equiv b \pmod{m} \) if \( m \) divides \( a - b \). In other words, \( a \) and \( b \) leave the same remainder when divided by \( m \).

0 1 2 3 4 5 6 7 8 9 10 11 difference = 5
\( 7 \equiv 2 \pmod{5} \)

What Is a Linear Congruence?

A linear congruence is an equation of the form:

$$ ax \equiv b \pmod{m} $$

where \( a, b, m \) are known integers and we want to find all values of \( x \) that satisfy this relation.

\( a \)
The coefficient: multiplied by the unknown \( x \)
\( b \)
The target remainder we want after dividing by \( m \)
\( m \)
The modulus: the "clock size" we work with

Why Do They Matter?

Linear congruences appear throughout computer science and mathematics. They are the foundation of many important algorithms and systems.

Cryptography
RSA encryption relies on solving congruences modulo large primes
Hash Functions
Modular arithmetic ensures hash values stay within table bounds
Scheduling
Cyclic schedules and calendars use modular equations

The Existence Theorem

Let \( d = \gcd(a, m) \). Then:

If \( d \mid b \)
There are exactly \( d \) distinct solutions modulo \( m \)
If \( d \nmid b \)
There are no solutions at all

Visualizing the \(\gcd\) Condition

\( 3x \equiv 6 \pmod{9} \)
\( \gcd(3, 9) = 3 \), and \( 3 \mid 6 \)
0 1 2 3 4 5 6 7 8
3 solutions: \( x \in \{2, 5, 8\} \)
\( 4x \equiv 3 \pmod{6} \)
\( \gcd(4, 6) = 2 \), but \( 2 \nmid 3 \)
0 1 2 3 4 5 ✕ ✕ ✕ ✕ ✕ ✕ No solutions! ✓ gcd divides b ✕ gcd does not divide b Solutions exist No solutions

Congruence vs Equality

Congruence Equality
Notation \( ax \equiv b \pmod{m} \) \( ax = b \)
Solutions Infinitely many (periodic) One value (typically)
Division by \( a \) Only when \(\gcd(a,m)=1\) Always when \( a \neq 0 \)
Answer form \( x \equiv c \pmod{m} \) \( x = c \)
Domain Integers only Integers, rationals, reals

The Modular Inverse

The modular inverse of \( a \) modulo \( m \) is the integer \( a^{-1} \) such that:

$$ a \cdot a^{-1} \equiv 1 \pmod{m} $$

It exists if and only if \( \gcd(a, m) = 1 \). Think of it as "division" in modular arithmetic.

Example
\( 3^{-1} \equiv 5 \pmod{7} \) because \( 3 \times 5 = 15 \equiv 1 \pmod{7} \)
Non-example
\( 4^{-1} \pmod{6} \) does not exist because \( \gcd(4,6) = 2 \neq 1 \)

Finding the Inverse: Two Methods

Trial (small moduli)
Test \( x = 0, 1, 2, \ldots, m-1 \) until \( ax \equiv 1 \pmod{m} \). Fast when \( m \) is small.
Extended Euclidean
Run the algorithm to find \( s, t \) with \( as + mt = 1 \). Then \( a^{-1} \equiv s \pmod{m} \).

How to Solve \( ax \equiv b \pmod{m} \)

1
Compute \( d = \gcd(a, m) \). If \( d \nmid b \), stop: no solution exists.
2
Simplify. Divide everything by \( d \): solve \( \frac{a}{d} x \equiv \frac{b}{d} \pmod{\frac{m}{d}} \).
3
Find the inverse. Since \( \gcd\!\left(\frac{a}{d},\, \frac{m}{d}\right) = 1 \), find \( \left(\frac{a}{d}\right)^{-1} \pmod{\frac{m}{d}} \).
4
Multiply both sides by the inverse: \( x \equiv \frac{b}{d} \cdot \left(\frac{a}{d}\right)^{-1} \pmod{\frac{m}{d}} \).
5
List all solutions mod \( m \). If \( d > 1 \), there are \( d \) solutions: \( x_0,\; x_0 + \frac{m}{d},\; x_0 + 2\!\cdot\!\frac{m}{d},\; \ldots \)

Example 1: \( 3x \equiv 4 \pmod{7} \)

1
Check \( \gcd(3, 7) = 1 \). Since \( 1 \mid 4 \), a unique solution exists.
2
Find \( 3^{-1} \pmod{7} \). Test: \( 3 \times 1 = 3 \), \( 3 \times 2 = 6 \), \( 3 \times 3 = 9 \equiv 2 \), \( 3 \times 4 = 12 \equiv 5 \), \( 3 \times 5 = 15 \equiv 1 \). So \( 3^{-1} \equiv 5 \).
3
Multiply both sides by 5: \( x \equiv 5 \cdot 4 \equiv 20 \equiv 6 \pmod{7} \).

Example 2: \( 6x \equiv 4 \pmod{10} \)

1
Compute \( d = \gcd(6, 10) = 2 \). Check: \( 2 \mid 4 \). ✓ Solutions exist, and there will be \( d = 2 \) of them.
2
Divide by 2: \( 3x \equiv 2 \pmod{5} \).
3
Find \( 3^{-1} \pmod{5} \). Trial: \( 3 \times 2 = 6 \equiv 1 \pmod{5} \). So \( 3^{-1} \equiv 2 \).
4
Multiply: \( x \equiv 2 \times 2 \equiv 4 \pmod{5} \).
5
All solutions mod 10: \( x \equiv 4 \pmod{5} \), so \( x \in \{4, 9\} \pmod{10} \).

Example 3: \( 4x \equiv 5 \pmod{6} \)

1
Compute \( d = \gcd(4, 6) = 2 \).
2
Check divisibility: Does \( 2 \mid 5 \)? No. ✕
3
Conclusion: No solution exists.
Values of 4x (mod 6) for x = 0, 1, 2, 3, 4, 5: x=0 x=1 x=2 x=3 x=4 x=5 0 4 2 0 4 2

Common Mistakes

✕ Dividing Both Sides by \( a \)
In regular algebra you can divide by \( a \). In modular arithmetic, you must multiply by \( a^{-1} \), which only exists when \( \gcd(a,m) = 1 \).
✕ Forgetting the \(\gcd\) Check
Always verify \( \gcd(a, m) \mid b \) first. Skipping this leads to wasted work or phantom solutions.
✕ Reporting Only One Solution
When \( d = \gcd(a,m) > 1 \), there are \( d \) distinct solutions mod \( m \). Missing the extras costs marks.

Summary Cheat Sheet

Scenario \(\gcd(a,m)\) Solutions Method
\(\gcd(a,m) = 1\) 1 Exactly 1 Find \(a^{-1}\), multiply
\(\gcd(a,m) = d\), \(d \mid b\) \(d > 1\) Exactly \(d\) Divide by \(d\), then invert
\(\gcd(a,m) = d\), \(d \nmid b\) \(d > 1\) None Stop immediately

Question 1 of 5

What is the solution to \( 5x \equiv 3 \pmod{7} \)?

Question 2 of 5

Does the congruence \( 6x \equiv 5 \pmod{9} \) have a solution?

Question 3 of 5

How many solutions does \( 4x \equiv 8 \pmod{12} \) have modulo 12?

Question 4 of 5

What is \( 7^{-1} \pmod{11} \)?

Question 5 of 5

Solve \( 9x \equiv 6 \pmod{15} \). Which values are solutions?