Leandro Junes

Linear Congruence Solver

Solve a linear congruence of the form ax ≡ b (mod n). The calculator determines whether solutions exist and finds all incongruent solutions modulo n.

Inputs

ax ≡ b (mod n)

Any integer.

Any integer.

Integer ≥ 1.

Try

Result

—

Solutions modulo n
—
gcd(a, n)
—
n′ = n/d
—

Solution steps

—

    Method

    Consider

    ax ≡ b (mod n)

    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:

    (a/d)x ≡ b/d (mod n/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

    x ≡ x0 + k(n/d) (mod n), k = 0, 1, …, d − 1.

    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.