Linear Congruence Calculator — Solve ax ≡ b (mod m)
Solve the linear congruence ax ≡ b (mod m): check whether gcd(a, m) divides b, reduce the congruence, find the modular inverse and list every solution modulo m.
How it works
ax ≡ b (mod m) has a solution ⇔ g = gcd(a, m) divides b · divide through: (a/g)x ≡ (b/g) (mod m/g) · x₀ = (b/g)·(a/g)⁻¹ mod (m/g) · all solutions mod m: x₀ + k·(m/g), k = 0, 1, …, g − 1
A linear congruence is the modular version of ax = b. It is solvable exactly when g = gcd(a, m) divides b, and then it has exactly g solutions modulo m. Dividing a, b and m by g leaves a congruence whose coefficient is invertible, so one multiplication by the modular inverse (found with the extended Euclidean algorithm) gives the unique solution modulo m/g. Adding multiples of m/g lists every solution modulo m (Rosen, Elementary Number Theory, §4.2). Negative a or b are first reduced modulo m.
Worked example
14x ≡ 30 (mod 100): gcd(14, 100) = 2, and 2 divides 30, so there are 2 solutions. Divide by 2: 7x ≡ 15 (mod 50). The inverse of 7 mod 50 is 43 because 7 × 43 = 301 ≡ 1. Then x ≡ 15 × 43 = 645 ≡ 45 (mod 50). Modulo 100 the solutions are 45 and 95. Check: 14 × 45 = 630 ≡ 30 (mod 100).
Sources: Wolfram MathWorld — Linear Congruence Equation · Wikipedia — Linear congruence theorem
Frequently asked questions
How do you solve a linear congruence?
Find g = gcd(a, m). If g does not divide b there is no solution. Otherwise divide everything by g, multiply both sides by the inverse of a/g modulo m/g, and add multiples of m/g to list all g solutions.
How many solutions does ax ≡ b (mod m) have?
Either none, or exactly gcd(a, m) solutions modulo m. When a and m are coprime there is exactly one.
What if a and m are coprime?
Then a has an inverse mod m and x ≡ a⁻¹·b (mod m) is the single solution.
How is this related to the linear Diophantine equation?
ax ≡ b (mod m) means ax − my = b for some integer y, so it is the Diophantine equation ax + my = b in disguise; the solvability condition is the same.
Related calculators
This calculator is for educational purposes. Double-check important results before acting on them.