2.5.7 · D4Number Theory (Intermediate)

Exercises — Euclidean algorithm — GCD computation

3,131 words14 min readBack to topic

Before we start, one reminder of every symbol we use, in plain words:


Level 1 — Recognition

Exercise 1.1

Perform one step of the Euclidean algorithm on . Write the division and state the new pair you would work with next.

Recall Solution 1.1

We ask: how many whole times does fit into ? Twice, since and . So . The next pair is . We now forget entirely and continue with .

Exercise 1.2

Which of these already tells you the algorithm has finished: , , ? State the GCD wherever it is finished.

Recall Solution 1.2

The stopping condition is when the second number is .

  • — not finished ().
  • finished. When the answer is , so this equals .
  • — not the stop rule yet, but one step gives , so it becomes . Answers: , and .

Level 2 — Application

Exercise 2.1

Compute using the full algorithm. Show every division line.

Recall Solution 2.1

Each line writes , then we slide : The last nonzero remainder is , so .

Exercise 2.2

Compute — note the smaller number is written first. Explain what the very first step does.

Recall Solution 2.2

With we compute . Since fits into zero times: So the pair — the algorithm swapped them for us. Continuing: Answer: . No pre-sorting was needed.

Exercise 2.3

Compute with the algorithm, then confirm your answer divides both the friendly pair and the two big numbers and from which they were shrunk down.

Recall Solution 2.3

. Check on the friendly pair: and , both whole numbers. ✓ Now the big inputs — run the algorithm honestly, no shortcuts. We must not guess; we apply the same divide-take-remainder loop to the actual numbers: The last nonzero remainder is , so . Does divide these big numbers? Test directly. For divisibility by : the last two digits of are , and is not whole, so — hence . So is the GCD of the small pair only; the big numbers were a decoy with a completely different GCD of . The lesson: a divisor of a shrunk-down pair need not divide the original numbers — you must run the algorithm on the actual inputs, exactly as we just did.


Level 3 — Analysis

Exercise 3.1

You run the algorithm on and the sequence of remainders is Two facts follow immediately about and about which remainder is the GCD. State the GCD and explain why the second-to-last remainder is always the answer.

Recall Solution 3.1

The GCD is the last nonzero remainder, which is . Why: the very last line is , meaning . Now use the Euclidean Lemma stated at the top of this page — — which says every consecutive pair in the chain has the same GCD (each pair shares exactly the same common divisors). Walking that equality backward: Because the last pair is (edge case: ), the value threads unchanged all the way back up to the original and . That is why the last nonzero remainder is always the answer.

Exercise 3.2

Suppose (coprime). Show that as well.

Recall Solution 3.2

Apply the Euclidean Lemma to the pair . Divide: So . Since does not care about order, too. Reading it aloud: any common divisor of and must divide their difference ; but nothing bigger than divides both and , so nothing bigger than survives.

Exercise 3.3

Using , find .

Recall Solution 3.3

is the least common multiple (see the notation refresher). From the parent note, . Then Sanity check: , a multiple of both. ✓


Level 4 — Synthesis

The next two problems are geometric. The Euclidean algorithm is literally the answer to: "what is the largest square tile that perfectly fills a rectangle?" Read the figure carefully — its labels drive the whole solution.

Figure — Euclidean algorithm — GCD computation

Figure s01 — walk-through. The yellow outline is the whole rectangle (blue width label along the bottom, height label up the left side). We first lay down the biggest squares that fit — side (blue squares, marked with their side length). Two of them fit, and a -wide strip is left over on the right. Into that strip we fit one square of side (green), leaving a -wide sliver. That sliver takes two squares of side (red), which fill it exactly — no scrap. Each colour is one row of the algorithm; the smallest square's side is the GCD.

Exercise 4.1

A rectangle is . Using the tiling picture above, cut the largest possible squares repeatedly. How many squares of each size appear, and what is the side of the smallest square? Connect the tile counts to the quotients of the algorithm.

Recall Solution 4.1

Run and record the quotients — each quotient counts how many squares of that size fit (match them to the coloured blocks in figure s01): So the tiling uses two -squares, one -square, and two -squares. The smallest square has side — which is exactly . The remainder-zero step is the moment the leftover strip is perfectly filled with no scrap.

Exercise 4.2

Look at the second figure: an rectangle (consecutive Fibonacci sides). Predict — before tiling — that every square will have quotient exactly except possibly the last, and that the smallest square has side . Verify with the algorithm.

Figure — Euclidean algorithm — GCD computation

Figure s02 — walk-through. The yellow outline is the rectangle (blue width along the bottom, height on the left). Because the sides are consecutive Fibonacci numbers, each cut peels off exactly one square (labelled with its side) and leaves a smaller rectangle whose sides are again consecutive Fibonacci numbers — . The squares spiral inward, getting smaller by one Fibonacci step each time, until the final square has side . This "one square per step" pattern is the slowest the algorithm can ever shrink — the worst case.

Recall Solution 4.2

Because and are consecutive Fibonacci numbers, each division peels off exactly one square then leaves the previous Fibonacci number (trace the inward spiral in figure s02): Every quotient is (the last is ), so we get a spiral of one-of-each squares — this is the slowest possible shrinkage, the worst case. The smallest square has side , confirming the numbers are coprime.


Level 5 — Mastery

Exercise 5.1

Find all integers with such that . (These are exactly the numbers that have a modular inverse mod , and hence the valid multiplier keys in a simple cipher.) How does this count relate to ?

Recall Solution 5.1

Recall is Euler's totient — the count of numbers coprime to (notation refresher above). Here . A number is coprime to iff it is divisible by neither nor . Remove even numbers and multiples of (). What remains from to : Counting them gives values. Now the totient formula, using : So the list length is : the count of coprime residues below equals Euler's totient of , and these values are exactly the numbers that possess a modular inverse mod .

Exercise 5.2

Two gears mesh: one has teeth, the other . After how many teeth of the small gear will a marked tooth on each gear return to its starting contact simultaneously? (Hint: this is an LCM, built from a GCD.)

Recall Solution 5.2

They re-align after teeth (least common multiple, see refresher). First the GCD: Then The marks realign after teeth pass the contact point.

Exercise 5.3

Here is the real reason this algorithm powers RSA. In RSA one must check that a public exponent is usable by confirming , where is Euler's totient of the key modulus (the count of numbers coprime to it — see the refresher). Take , . Is a valid RSA exponent? Show the algorithm.

Recall Solution 5.3

Test : The last nonzero remainder is , so . Yes, is valid — it is coprime to , so an inverse exists (found by the extended algorithm) via Bézout's identity .


Wrap-up recall

Recall The one sentence that solves every problem above

Replace the larger number by (larger smaller), repeat, and the last nonzero remainder is the GCD — which also unlocks LCM, coprimality tests, and RSA key checks, all without ever factoring.