I tend to remember these algorithms by the question each one answers. Do two numbers share a factor? Can I undo a multiplication after taking a remainder? How do I put several remainders back together? My math is rusty, so these are the working notes and Python snippets I come back to.
Euclid: what divides both numbers?
Start with $a>b$. Divide $a$ by $b$ and keep the remainder. A number that divides both $a$ and $b$ must divide that remainder too. That’s why we can trade one GCD problem for a smaller one:
$$
\gcd(a,b)=\gcd(b,a\bmod b)
$$
Keep going until the remainder is zero. The last nonzero divisor is the GCD. For everyday Python code there’s already math.gcd(a, b). For positive integers, knowing the GCD also gives the least common multiple: divide their product by the GCD.
Bézout: can I write the GCD using a and b?
Bézout’s identity says there are integers $x$ and $y$ such that
$$
ax+by=\gcd(a,b).
$$
Those coefficients can be negative. For instance, $3\times 5-2\times 7=1$. That’s a surprisingly useful way to know that 5 and 7 are coprime: the same equation will shortly tell us how to undo a multiplication by 5 modulo 7.
Extended Euclid: where do the coefficients come from?
Euclid’s algorithm keeps replacing $(a,b)$ with $(b,a\bmod b)$. Run it recursively and, on the way back up, keep the coefficients as well as the GCD.
Suppose the smaller call has found $x_2,y_2$ such that
$$
b x_2+(a\bmod b)y_2=\gcd(a,b).
$$
Write the quotient as $q=\lfloor a/b\rfloor$. The remainder is $a-qb$. Substitute that into the left-hand side and collect the multiples of $a$ and $b$:
$$
b x_2+(a-qb)y_2 = a y_2 + b(x_2-qy_2).
$$
Now we can read off the pair for the original problem: $x=y_2$ and $y=x_2-qy_2$. At the bottom of the recursion we’re looking at $\gcd(r,0)=r$. The coefficients there are just 1 and 0. That’s what both returns in this snippet are doing:
1 | def extgcd(a, b): |
A modular inverse: how do I undo a multiplication?
An inverse of $a$ modulo $n$ is a number $x$ for which $ax\equiv1\pmod n$. In ordinary arithmetic that means $ax-kn=1$ for some integer $k$. If $a$ and $n$ are coprime, their GCD is 1, so this is precisely the equation extended Euclid just solved.
Take the coefficient of $a$ and reduce it modulo $n$ to get its nonnegative representative. In the earlier example, $3\times 5-2\times 7=1$ tells us that 3 is the inverse of 5 modulo 7.
1 | def extgcd(a, b): |
The Chinese remainder theorem: which number fits all the clues?
Count a pile of objects in threes and two are left over. Count in fives and three are left. Count in sevens and two are left. How many objects could there be?
That’s the classic puzzle. We’re looking for a number with remainders 2, 3, and 2 modulo 3, 5, and 7. Here’s the system written out:

The trick is to make a separate contribution for each clue. It should produce the right remainder for its own modulus and zero for all the others, so adding it won’t spoil anything we’ve already matched. This version assumes that the moduli $n_i$ are pairwise coprime.
- Multiply all the moduli together to get $N$.
- For clue $i$, take $m_i=N/n_i$. It contains all the other moduli as factors, so it’s already zero modulo each of them.
- Multiply $m_i$ by its inverse modulo $n_i$. Now this contribution is 1 modulo its own modulus and still zero modulo all the others.
- Multiply that contribution by the remainder $a_i$ you actually want. Add the contributions and take the result modulo $N$.
For the pile of objects, the smallest nonnegative answer is 23. Check it: 23 leaves 2 after division by 3, 3 after division by 5, and 2 after division by 7. Adding $3\times5\times7=105$ gives another answer with the same three remainders.
1 | def CRT(a: list, n: list): |