The Chinese Remainder Theorem (Sunzi's Theorem) says that if $\gcd(m,n) = 1$, then knowing $x \bmod m$ and $x \bmod n$ is the same as knowing $x \bmod mn$. Here two gears with $m$ and $n$ teeth are meshed together, and a big dial counts modulo $mn$. Every step adds $1$ to everything: each gear turns one tooth and the dial turns one notch. Where the gears mesh, you read off $x \bmod m$ and $x \bmod n$.
try:
Solve a CRT problem: find $x$ with $x \equiv$ (mod 3) and $x \equiv$ (mod 5)
The table
Rows are $x \bmod m$ and columns are $x \bmod n$. Each step moves one square down and one square to the right, wrapping around the edges, and writes $x$ in the square it lands on.
What to notice
- When $\gcd(m, n) = 1$, the gears first line up at $(0, 0)$ again after exactly $mn$ steps, just as the big dial comes back to $0$. Along the way every pair $(a, b)$ shows up exactly once, so every square of the table fills in: each system $x \equiv a \pmod m$, $x \equiv b \pmod n$ has exactly one solution modulo $mn$.
- When $d = \gcd(m, n) > 1$, the gears line up at $(0, 0)$ again after only lcm$(m, n) = mn/d$ steps. From then on everything repeats, so the dial teeth from lcm$(m,n)$ onwards (greyed out) never show a new pair, and only $1/d$ of the table ever fills in. A system $x \equiv a \pmod m$, $x \equiv b \pmod n$ then has a solution only when $a \equiv b \pmod d$.
- See the textbook, Section 3.4 (Sunzi's Theorem (Chinese Remainder Theorem)), for how to compute the solution without turning the gears, using the Extended Euclidean Algorithm (try the Euclidean Algorithm page).