Inputs
Any integer.
Any integer.
Integer ≥ 1.
Result
- Solutions modulo n
- —
- gcd(a, n)
- —
- n′ = n/d
- —
Solution steps
Method
Consider
and let d = gcd(a, n). A solution exists exactly when d | b. If d ∤ b, there is no solution.
If d | b, divide the congruence by d:
Now gcd(a/d, n/d) = 1, so a/d has a modular inverse modulo n/d. This gives one solution x_0 modulo n/d. The complete set of incongruent solutions modulo n is
Why d solutions? The reduced congruence determines one residue class modulo n/d. Inside a complete residue system modulo n, that class occurs exactly d times.
Two cases are handled directly. Modulo 1 every integer is congruent to every other, so every integer is a solution and there is a single residue class. If a ≡ 0 (mod n) the congruence reads 0·x ≡ b (mod n): every residue class is a solution when b ≡ 0, and none otherwise.
Related: Modular Inverse — when gcd(a, n) = 1 the congruence has a unique solution, found directly with a modular inverse. Chinese Remainder Theorem — the natural next step, systems of congruences.