Here is a practical method for finding all solutions to
the equation ax = b mod n.
Algorithm
- Input: integers a, b and a positive integer n.
- Output: all x (mod n) satisfying the equation
ax = b mod n.
- Find (using the extended Euclidean
algorithm) integers p and q such that
d := gcd(a,n) = pa + qn.
- If d does not divide b,
then return that there are no solutions.
- If d divides b, then
return that the solutions modulo n are
x = (pb/d) + (kn/d), with
k = 0, 1, ..., d - 1.
Systems of two linear congruences can be handled by solving one of them and then
using the result as input in the second one.