Here is a practical method for finding all solutions to the equation ax = b mod n.

Algorithm

  1. Find (using the extended Euclidean algorithm) integers p and q such that d := gcd(a,n) = pa + qn.
  2. If d does not divide b, then return that there are no solutions.
  3. 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.