2.5.7 · D1Number Theory (Intermediate)

Foundations — Euclidean algorithm — GCD computation

2,070 words9 min readBack to topic

This page builds every symbol the parent note Euclidean algorithm — GCD computation uses, starting from the idea of a whole number. If you have never seen , , , or before, start here at line one. Nothing below is assumed.


1. Whole numbers and "fitting exactly"

The picture is tiles laid in a row with no gap and no overhang.

Figure — Euclidean algorithm — GCD computation

Look at the top row: three orange tiles of length exactly fill a bar of length , so (read " divides "). In the bottom row, tiles of length overhang the bar of length — the last tile pokes out — so (read " does not divide ").

Prerequisite: 2.5.01-divisibility-and-division-algorithm.


2. Divisor and common divisor

Take and .

  • Divisors of : .
  • Divisors of : .
  • Living in both lists: .

Those shared numbers are the common divisors. The picture: a tile length that lays perfectly along both bars at once.


3. The GCD — the star of the show

From the list above, the common divisors of and were ; the biggest is , so .

Figure — Euclidean algorithm — GCD computation

The magenta tile of length is the widest tile that still covers both the -bar and the -bar with no overhang. A tile of would overhang both; a tile of fits but is not the widest. That "widest fitting tile" is exactly what means.

Contrast with the factorization route from 2.5.03-fundamental-theorem-of-arithmetic, which finds this same number by breaking each into primes.


4. Quotient and remainder — the division algorithm

When does not divide , tiles overhang. The overhang is the star that makes the whole algorithm run.

Figure — Euclidean algorithm — GCD computation

For , : only one tile of length fits (), and sticks out, so . Written out: . The rule says the leftover is always smaller than the tile — otherwise you could have fit another whole tile.


5. The mod symbol

Examples you can check on your fingers:

  • (fits exactly — no leftover)
  • (a small number mod a big one is itself, since zero big-tiles fit)

6. Putting the symbols together — why the swap works

Now every symbol in the parent's key line is defined. Read it aloud: In words: the biggest common tile of and equals the biggest common tile of and the leftover. That is precisely the "leftover trick" — any tile that fits and must also fit the leftover , because it fits and it fits every -tile.

Figure — Euclidean algorithm — GCD computation

The magenta tile that covers both bars in the figure also lines up perfectly along the violet leftover chunk. Nothing about the biggest tile is lost when we throw away the big bar and keep the small bar + leftover — so we may safely shrink the problem. Repeat until the leftover is ; at that final step the right-hand number is , and the base case from Section 3 hands back the answer — the last non-zero remainder.


7. One more symbol family: Fibonacci and

The parent's speed section mentions and .

Why the topic needs them: consecutive Fibonacci numbers are the worst case — every quotient , so the leftover shrinks as slowly as possible. Because grows fast, even the slowest case finishes in about -many steps.


Prerequisite map

Integers and the divides relation

Divisor of a

Common divisor of a and b

GCD greatest common divisor

Division algorithm a = bq + r

Quotient q and remainder r

mod leftover a mod b

Euclidean swap gcd equals gcd b r

Termination via base case and Fibonacci worst case


Equipment checklist

Cover the right side and test yourself.

What does mean in plain words (and what must be)?
There is an integer with is a whole number of copies of , no leftover — and must be non-zero.
Is true or false?
True — , so every non-zero number divides .
What is a common divisor of and ?
A number that divides both and exactly.
Define , including its sign convention.
The largest integer dividing both and , always reported as non-negative.
What is and why?
— every number divides , so the common divisors of and are just the divisors of , the biggest being .
In , what is and what is ?
= how many whole -tiles fit in ; = the leftover, with .
What is , and for which inputs did we define it?
The remainder ; defined here only for and .
Compute .
(a smaller number mod a bigger one is itself).
Why is true?
Any tile fitting and also fits the leftover , so the biggest common tile is unchanged.
What are the first six Fibonacci numbers ?
.
Why do consecutive Fibonacci inputs give the worst case?
Every quotient is , so the leftover shrinks as slowly as possible.

Next: with these symbols secured, revisit Euclidean algorithm — GCD computation, then extend to 2.5.08-extended-euclidean-algorithm and 2.5.10-bezouts-identity — the gateway to 3.2.04-rsa-encryption.