# Solution to Simultaneous Linear Congruences

## Theorem

Let:

\(\displaystyle a_1 x\) | \(\equiv\) | \(\displaystyle b_1\) | \(\displaystyle \pmod {n_1}\) | ||||||||||

\(\displaystyle a_2 x\) | \(\equiv\) | \(\displaystyle b_2\) | \(\displaystyle \pmod {n_2}\) | ||||||||||

\(\displaystyle \) | \(\ldots\) | \(\displaystyle \) | |||||||||||

\(\displaystyle a_r x\) | \(\equiv\) | \(\displaystyle b_r\) | \(\displaystyle \pmod {n_r}\) |

be a system of simultaneous linear congruences.

This system has a simultaneous solution if and only if:

- $\forall i, j: 1 \le i, j \le r: \gcd \set {n_i, n_j}$ divides $b_j - b_i$.

If a solution exists then it is unique modulo $\lcm \set {n_1, n_2, \ldots, n_r}$.

## Proof

We take the case where $r = 2$.

Suppose $x \in \Z$ satisfies both:

\(\displaystyle a_1 x\) | \(\equiv\) | \(\displaystyle b_1\) | \(\displaystyle \pmod {n_1}\) | ||||||||||

\(\displaystyle a_2 x\) | \(\equiv\) | \(\displaystyle b_2\) | \(\displaystyle \pmod {n_2}\) |

That is, $\exists r, s \in \Z$ such that:

\(\displaystyle x - b_1\) | \(=\) | \(\displaystyle n_1 r\) | |||||||||||

\(\displaystyle x - b_2\) | \(=\) | \(\displaystyle n_2 r\) |

Eliminating $x$, we get:

- $b_2 - b_1 = n_1 r - n_2 s$

The right hand side is an integer combination of $n_1$ and $n_2$ and so is a multiple of $\gcd \left\{{n_1, n_2}\right\}$.

Thus $\gcd \set {n_1, n_2}$ divides $b_2 - b_1$, so this is a necessary condition for the system to have a solution.

To show sufficiency, we reverse the argument.

Suppose $\exists k \in \Z: b_2 - b_1 = k \gcd \set {n_1, n_2}$.

We know that $\exists u, v \in \Z: \gcd \set {n_1, n_2} = u n_1 + v n_2$ from Bézout's Identity.

Eliminating $\gcd \set {n_1, n_2}$, we have:

- $b_1 + k u n_1 = b_2 - k v n_2$.

Then:

- $b_1 + k u n_1 = b_1 + \paren {k u} n_1 \equiv b_1 \pmod {n_1}$
- $b_1 + k u n_1 = b_2 + \paren {k v} n_2 \equiv b_2 \pmod {n_2}$

So $b_1 + k u n_1$ satisfies both congruences and so simultaneous solutions do exist.

Now to show uniqueness.

Suppose $x_1$ and $x_2$ are both solutions.

That is:

- $x_1 \equiv x_2 \equiv b_1 \pmod n_1$
- $x_1 \equiv x_2 \equiv b_2 \pmod n_2$

Then from Intersection of Congruence Classes the result follows.

$\blacksquare$

The result for $r > 2$ follows by a tedious induction proof.