2.5.8 · D5Number Theory (Intermediate)

Question bank — Extended Euclidean algorithm

1,990 words9 min readBack to topic

This is a rapid-fire trap deck. Each line is a claim or question; read it, decide before you peek, then reveal. The goal is not arithmetic (that lives in the parent note's worked tables) — it's the thinking about signs, base cases, uniqueness, and edge inputs that people get wrong.

Setup — the notation these traps use (read this first)

Every trap below leans on the same handful of symbols. Let's pin them down and draw them, so nothing is used before it's earned.

The picture below is the whole vocabulary in one glance. Read it as: the left column is the remainder chain shrinking downward until it hits ; the row just above the (green) is the gcd; the right column is each remainder's receipt telling you how that remainder is built from and . Follow the white down-arrows to watch each remainder feed the next.

Figure — Extended Euclidean algorithm
Figure 1 — The remainder chain for descending to , with the Bézout receipt riding alongside each rung. The green rung is the last nonzero remainder (the gcd); the red rung is the terminating .

The second diagram shows why the arithmetic is forced. Read it as: each receipt is a point in "coefficient space". The new receipt (green) is literally the two-steps-ago receipt (yellow) minus times the one-step-ago receipt (blue) — the red dashed arrow is that term. Because remainders obey , their receipts must obey the identical subtraction.

Figure — Extended Euclidean algorithm
Figure 2 — The coefficient recurrence as vector subtraction: , shown for the step .

With , , , , and the recurrence all defined, the traps make sense.


True or false — justify

True or false: For any nonzero integers , integers with always exist.
True — this is Bézout's identity, and Extended Euclid constructs them explicitly, so existence is guaranteed, never assumed.
True or false: The pair from Bézout's identity is unique.
False — you can add to and subtract from (with ) and the equation still holds, so there are infinitely many pairs.
True or false: can equal any integer you like by choosing .
False — can only ever be a multiple of ; the smallest positive value it reaches is exactly the gcd itself.
True or false: The gcd is the last remainder produced by the algorithm.
False — the last remainder is ; the gcd is the last non-zero remainder, one row before the process halts.
True or false: If then both Bézout coefficients must be nonzero.
False in general, but true unless or is ; e.g. via , where .
True or false: In , at least one of is negative whenever .
True — if both were then or (too big to equal the smaller gcd), so one coefficient must subtract.
True or false: Swapping and just swaps and .
True — the equation is symmetric in labelling, so with solves the swapped problem.
True or false: Running Extended Euclid on and always yields the modular inverse of mod .
False — only when ; otherwise no inverse exists and the coefficient solves nothing modular.

Spot the error

"I start my coefficient table at the first division , so my first row is that step." — what's wrong?
The base cases come before any division: has and has . Skipping them corrupts every later recurrence.
"Using with , , I get ." — spot it.
The sign was dropped: , so . Always substitute the coefficient with its sign in parentheses.
"The gcd of and came out as in the last row, so the gcd is ." — error?
is the terminating remainder, not the gcd. The gcd is , the last nonzero remainder above it.
"To solve I just find Bézout coefficients for and scale." — error?
is not a multiple of , so no integer solution exists at all — scaling by produces non-integers. Check divisibility of by the gcd first (see Linear Diophantine Equations).
"For the recurrence I only track ; I'll recover at the end from ." — is this safe?
It works arithmetically as a shortcut only if , but it hides sign/rounding errors; tracking both columns via the twin recurrences is safer and standard.
": the algorithm needs two positive inputs, so this is undefined." — error?
is perfectly defined; the receipt is , i.e. . Zero is a legitimate input.

Why questions

Why does every remainder end up expressible as ?
Because and already are, and each new remainder is a combination of the previous two — the property propagates by induction.
Why does the recurrence subtract times the previous coefficient rather than add?
Because the division step itself is (a subtraction), and the coefficients simply inherit the exact same arithmetic.
Why do we need Extended Euclid at all for modular inverses instead of just trying values?
Trial takes up to attempts; Extended Euclid finds with in about steps, which is why RSA relies on it for large keys.
Why is the smallest positive value of exactly and never smaller, and how does Extended Euclid actually attain it?
Any is divisible by every common divisor of , hence by , so no positive value beats ; and the algorithm attains because it carries the receipt on every row — when the chain reaches the row holding , that row's is an explicit combination summing to .
Why does the Chinese Remainder Theorem care about Extended Euclid?
Its construction glues remainders using modular inverses of the moduli, and those inverses are computed by Extended Euclid.

Edge cases

What are the Bézout coefficients when ?
, and the algorithm's first step leaves remainder immediately, so the gcd sits in the row whose receipt is — the algorithm returns (i.e. ), the minimal-magnitude pair.
What happens if at the start?
Nothing breaks — the first division gives quotient and remainder , which simply swaps them; the base cases and still hold.
What does the algorithm return for ?
It's undefined (conventionally ): with both inputs zero there is no nonzero remainder to call a gcd and no meaningful receipt, so it's excluded from Bézout's identity.
If or is negative, are Bézout coefficients still valid, and why?
Yes — is defined via absolute values, so run Euclid on to get ; if was negative, then and substituting gives , so flipping that coefficient's sign restores a genuine solution of — Bézout's identity is preserved because the equality still holds term-for-term.
For , what is the receipt?
with , so — a degenerate case where one coefficient is zero.
Since infinitely many pairs work, which one does the algorithm hand back, and how do I recognize the "canonical" one?
Extended Euclid returns the pair with the smallest magnitudes: for they satisfy and , so it is the minimal-magnitude solution — no shifting needed.
Why do those bounds and hold?
All solutions form a lattice, spaced apart in and apart in (see next item); the algorithm's output lands in one spacing-window around , so its distance from is at most half a spacing, giving and correspondingly .
I have one solution ; how do I list all of them?
Every solution is for integer : adding to and subtracting from changes by , so the equation still holds.
Sweeping to minimise — why need I check only one or two integer values of ?
is a "V-shaped" function of the real variable (decreasing then increasing), so its integer minimum is at one of the two integers straddling the real minimiser; checking those one or two suffices, because moving further only increases (and typically too).
For the algorithm gives ; is there a smaller-looking pair?
Shift by : grows, and gives — both larger, so is already the minimal-magnitude canonical pair the algorithm returns.

Recall The five traps in one breath

Base cases come before division; the gcd is the second-to-last remainder; substitute negatives in parentheses; check divisible by before scaling; inverses need .

Connections