2.5.7 · D3Number Theory (Intermediate)

Worked examples — Euclidean algorithm — GCD computation

2,286 words10 min readBack to topic

Before anything, one word we lean on constantly: a divisor of a number is a whole number that divides with zero remainder. "" (read " divides ") is shorthand for " is a whole multiple of ". The division algorithm promises: for any integer and any positive , there is exactly one pair with Here is the quotient (how many whole copies of fit inside ) and is the remainder (the leftover, always smaller than ). Every example below is just this line, applied over and over.


The scenario matrix

Every input pair falls into one of these cells. Our examples together hit all of them.

Cell Situation What could go wrong Example
A Both positive, , standard nothing — the baseline Ex 1
B Both positive, do we need to swap first? Ex 2
C One divides the other exactly remainder on step one Ex 3
D Coprime pair (answer is ) mistaking for "failed" Ex 4
E A zero input, the degenerate edge Ex 5
F Negative input(s) signs — does even make sense? Ex 6
G Word problem (tiling a floor) translating words into a Ex 7
H Exam twist ( of three numbers) chaining the algorithm Ex 8

The greatest common divisor is the largest whole number that divides both and . Read the matrix rows top to bottom — each strips away one assumption the baseline quietly made.


Cell A — the baseline

Look at the figure: each step chops the longer bar into copies of the shorter bar, and the shrinking leftover is what we carry forward.

Figure — Euclidean algorithm — GCD computation

Cell B — smaller number first (do we swap?)


Cell C — one divides the other


Cell D — coprime pair (answer is 1)


Cell E — a zero input (the degenerate edge)


Cell F — negative inputs


Cell G — word problem (tiling)

Figure — Euclidean algorithm — GCD computation

Cell H — exam twist (three numbers)


Recall

Recall Why does

? Every integer divides (since ), so the common divisors of and are exactly the divisors of ; the greatest is itself. Why does swapping to smaller-first cost only one step? ::: The first division gives quotient and remainder , so becomes — one wasted round, then normal. Does a negative input change the answer? ::: No — divisors come in pairs, and is fixed as the positive representative, so . How do you three numbers with a two-input algorithm? ::: Nest it: .

Connections

  • 2.5.7-euclidean-algorithm-gcd-computation — the parent method these cases stress-test
  • 2.5.01-divisibility-and-division-algorithm — the line every example runs on
  • 2.5.03-fundamental-theorem-of-arithmetic — cross-checking via shared prime factors
  • 2.5.08-extended-euclidean-algorithm — next: recover with
  • 2.5.10-bezouts-identity — why coprime (Cell D) guarantees a solvable
  • 2.5.15-modular-arithmetic-basics — coprimality is what lets modular inverses exist
  • 3.2.04-rsa-encryption — the real-world payoff of the coprime and inverse machinery
  • 1.4.09-fibonacci-sequence — the worst-case inputs that make the algorithm slowest