The Euclidean Algorithm [00JU]
- August 31, 2026
The first algorithm we will consider is the famous Euclidean algorithm for finding the greatest common divisor of two integers. We name it after Euclid, who wrote it down in his classic work on geometry and number theory, The Elements; however, it has been reinvented in multiple times, places and cultures throughout history.
Review: mod and GCD [00JV]
- August 31, 2026
The material in this section is presented via a POGIL activity.
For a refresher on the \mathbin {\mathbf {mod}} operator, see these lecture notes from Discrete Math.
For a refresher on GCD, see these lecture notes.
The Euclidean Algorithm, recusively and iteratively [00K2]
- September 2, 2026
The material in this section is also presented via a POGIL activity.
We will consider two different implementations of the Euclidean Algorithm.
Algorithm Recursive Euclidean Algorithm [00K4]
- September 2, 2026
The first implementation of the Euclidean Algorithm is recursive:
Algorithm Iterative Euclidean Algorithm [00K5]
- September 2, 2026
The second implementation of the Euclidean Algorithm is iterative:
By trying examples, it seems like both these algorithms produce correct results. But how can we be sure? This is an important question, because it's easy to write versions of these algorithms which look very similar, and even work some of the time, but which sometimes crash, loop forever, or return an incorrect result!
Proving \mathsf {GCDR} correct [00K3]
- September 2, 2026
We will begin by formally proving the correctness of the recursive Euclidean Algorithm. But before we can even do that, we have to back up a bit: what does it even mean for an algorithm to be "correct"?
Specification vs implementation [00K6]
- September 2, 2026
Whenever we implement an algorithm, we have in mind a specification of what we would like the algorithm to achieve. This specification could be in the form of a mathematical definition (as in this case), or it could be a brute-force algorithm which "obviously" does what we want, or a table of inputs and outputs, or any other way of precisely defining a relationship between inputs and outputs. Our algorithm is then an implementation of this specification, and our job is to show that the specification and implementation match: that is, they produce identical outputs for all possible inputs.
In this case, the specification we have in mind is the mathematical definition of \gcd ; the implementation we have in mind is the \mathsf {GCDR} function. We are starting with \mathsf {GCDR} because it's usually much easier to prove things about recursive algorithms than about iterative ones!
In order to prove that \mathsf {GCDR} is correct, we first need two small lemmas about the mathematical \gcd function. The proofs are included only for completeness—feel free to skip them unless you are interested.
Lemma GCD is commutative [00F3]
- April 16, 2026
\gcd (a,b) = \gcd (b,a).
Proof.
- April 16, 2026
Theorem GCD reduction via mod [00F8]
- April 16, 2026
For all a,b \in \mathbb {N}, \gcd (a,b) = \gcd (a \mathbin {\mathbf {mod}} b, b).
Proof.
- April 16, 2026
By the division algorithm write a = bq + r. Then
Now we can prove that \mathsf {GCDR} matches its specification.
Theorem \mathsf {GCDR} is correct [00K8]
- September 3, 2026
For all a,b, \geq 0, \mathsf {GCDR}(a,b) = \gcd (a,b).
Proof.
- September 3, 2026
By (strong) induction on b.
- In the base case, when b = 0, \mathsf {GCDR}(a,0) = a, which is indeed the definition of \gcd (a,0).
- Now suppose b > 0 and that for all a, \mathsf {GCDR}(a,b') = \gcd (a,b') whenever b' < b. In this case \mathsf {GCDR}(a,b) = \mathsf {GCDR}(b, a \bmod b). We know a \bmod b < b, so the induction hypothesis applies (note in particular that the induction hypothesis holds for any a, and in particular we can pick b as our “a”). We concude that \mathsf {GCDR}(a,b) = \mathsf {GCDR}(b, a \bmod b) = \gcd (b, a \bmod b) = \gcd (a \bmod b, b) = \gcd (a,b), where the last two steps follow from the fact that \gcd is commutative and the previous lemma.
Proving \mathsf {GCDI} correct [00K9]
- September 3, 2026
Now let's prove that the iterative version of the Euclidean Algorithm is correct as well.
Theorem \mathsf {GCDI} is correct [00KB]
- September 3, 2026
\mathsf {GCDI}(a,b) = \gcd (a,b).
Proof.
- September 3, 2026
Suppose we initially call \mathsf {GCDI}(m,n). Then we claim the following loop invariant: each time right before doing the loop check again, \gcd (a,b) = \gcd (m,n). That is, the \gcd of the current values of a and b is always equal to the \gcd of the original arguments to \mathsf {GCDI}.
We prove this by induction on the number of loop executions.
- In the base case, before the loop has executed at all, a and b are equal to the original arguments to \mathsf {GCDI}, hence the invariant holds trivially.
- Otherwise, suppose the invariant held after n loop iterations; we must show it will still hold after one more loop iteration. But this follows from the theorem GCD reduction via mod.
Finally, after the loop ends, we will have either a = 0 or b = 0, and we can see that we will return wichever one is nonzero. This correctly returns the \gcd of a and b since \gcd (a,0) = a. But because of the loop invariant we know that \gcd (a,b) = \gcd (m,n), hence \mathsf {GCDI}(m,n) = \gcd (m,n).
It might seem like we are done, but actually we are not—we do not know that the loop will ever stop! All we have proved so far is that if it ever stops, it will deliver the correct value, but that is a big if. It's worth remarking that our proof of correctness for \mathsf {GCDR} neatly sidestepped this issue because it was by induction on the size of the arguments to \mathsf {GCDR}: the proof that the function returns the correct value, and the proof that the function always stops after a finite amount of time, were both wrapped up into a single argument. With \mathsf {GCDI}, we must argue these two things separately.
So, why does \mathsf {GCDI} always stop? Each time through the loop, either a gets smaller and b stays the same, or b gets smaller and a stays the same: if the loop starts out with a \leq b, then b gets smaller, since b \bmod a < a \leq b. Otherwise a gets smaller, since a \bmod b < b < a. Since neither ever gets bigger and one gets strictly smaller every loop iteration, one of them must eventually hit 0, which will stop the loop.
Technically, this argument that \mathsf {GCDI} always stops is a proof by well-founded induction on pairs \mathbb {N}^2 under the well-founded relation \{((a,b), (x,y)) \mid (a < x \land b = y) \lor (a = x \land b < y)\}.Remark. ★ Pedantic comment about well-founded induction, for those who like that sort of thing [00KC]
Euclidean Algorithm problems [00KF]
- September 4, 2026
Problems related to The Euclidean Algorithm, induction, and proving algorithm correctness.
[GCD1] GCD via factoring vs Euclidean algorithm (2 pts) [00JW]
- August 31, 2026
In class we considered the problem of finding the greatest common divisor of two positive integers. We explored two ways to compute the GCD:
- Factor the two numbers into their unique prime factorizations, and find the biggest subset of primes contained in both; this is the factorization of the GCD.
- Run the Euclidean Algorithm.
- Use your brain, a calculator, Wolfram Alpha, and/or some other appropriate computational system to factor 170520 into its prime factors.
- Now factor 522522, and use the results to find \gcd (170520, 522522) by Method 1.
- Trace the execution of the Euclidean Algorithm on (170520, 522522). Compare and contrast the two methods of computing the GCD of these two numbers.
- Now try to factor the number m shown below, using whatever methods you like (for example, try using Wolfram Alpha). What happens? m = 308903627938612635213051732991863520976852479109400884338832430641564115236537.
I would be willing to bet some money that you did not succeed in factoring m—though not a large amount, since there do exist algorithms that can factor numbers of this size in a matter of hours or even minutes; but in any case factoring a number with, say, four times as many digits as this would require more like centuries. Factoring is widely believed to be a rather difficult computational problem (the security of your bank account probably depends on it!).
- Now, suppose I tell you that there are in fact three prime numbers p, q, and r, such that \begin {align*} m &= p \cdot q = 308903627938612635213051732991863520976852479109400884338832430641564115236537 \\ n &= p \cdot r = 235051426595377535232357646935740625119338466495681619002059053022377525089111 \end {align*} (this is the same number m from part (d)). Find p, q, and r.
- What does this suggest about the relative efficiency of the two methods for computing the GCD?
[K4] Kattis: GCD (1 pt) [00JY]
- September 1, 2026
Solve GCD. You may not use any built-in GCD function; you must implement the Euclidean Algorithm yourself.
Runtime analysis of the Euclidean Algorithm [00KE]
- September 4, 2026
In [GCD1] GCD via factoring vs Euclidean algorithm (2 pts), you learned that the Euclidean Algorithm seems much more efficient than factoring; but how efficient is it, exactly? The problems in this section will walk you through a classic analysis of how long the Euclidean Algorithm takes to run on given inputs. However, the problems are independent; it is possible to do each problem without having done the previous ones.
Together, the problems in this section walk you through a proof of what is known as Lamé's Theorem, first proved by Gabriel Lamé in 1844. It was significant in being one of the first “practical” applications of the Fibonacci numbers, and one of the first results in what we would now call the theory of algorithms.
Throughout this section, when referring to the Euclidean
Algorithm, we will specifically work with the recursive
implementation \mathsf {GCDR} (GCDRecA from the POGIL activity).
[GCD2] GCD of consecutive Fibonacci numbers (2 pts) [00JZ]
- September 1, 2026
Recall the Fibonacci numbers, defined by F_0 = 0, F_1 = 1, and F_{n+2} = F_{n+1} + F_n. That is, the first two Fibonacci numbers are 0, 1, and then each subsequent Fibonacci number is the sum of the previous two, so the first few are 0, 1, 1, 2, 3, 5, 8, 13, \dots
- Compute F_9 and F_{10}, and trace the execution of the Euclidean Algorithm to compute \gcd (F_{10}, F_9). What happens?
- Prove by induction on n that \gcd (F_{n+1}, F_n) = 1 for all n \geq 0.
- Explain why your proof also shows that the Euclidean Algorithm requires n recursive steps to compute \gcd (F_{n+1}, F_n) = 1.
[GCD3] Worst-case GCD input (3 pts) [00K0]
- September 1, 2026
In fact, more is true: consecutive Fibonacci numbers (F_{n+1}, F_n) are in some sense a worst-case input for the Euclidean Algorithm: they are the smallest numbers for which the Euclidean Algorithm needs n steps.
Let S(n) be the following proposition:
S(n) = For all natural numbers a and b, if a > b and \mathsf {GCDR}(a,b) makes n recursive calls, then a \geq F_{n+1} and b \geq F_n.
That is, if R(a,b,n) represents the proposition "\mathsf {GCDR}(a,b) makes n recursive calls", then S(n) = \forall a,b : \mathbb {N}.\; ((a > b) \land R(a,b,n)) \to ((a \geq F_{n+1}) \land (b \geq F_n)).
- Prove \forall n : \mathbb {N}. S(n) by induction on n:
- What does the base case S(0) say? Prove it.
Hint.
- September 1, 2026
Look at the code for \mathsf {GCDR}. What can you conclude about a and b if it makes zero recursive calls?
- Let k be an arbitrary natural number, and prove S(k) \to S(k+1) by supposing S(k) to be true and proving S(k+1). You may want to write out S(k) and S(k+1) in full to help you keep track of what you know and what you are trying to prove.
Hint.
- September 1, 2026
Show that a \geq b + a \mathbin {\mathbf {mod}} b.
- What does the base case S(0) say? Prove it.
- Explain why the previous result shows that if a \leq F_{n+1} then the Euclidean Algorithm requires at most n steps.
[GCD4] Fibonacci number size (3 pts) [00K1]
- September 1, 2026
How big are Fibonacci numbers? Let’s find out:
- Solve x^2 = x + 1 for x and call the positive solution \varphi (this is often known as the golden ratio) and the negative solution \hat {\varphi }.
- Explain why we know that \varphi ^2 = \varphi + 1 and \hat {\varphi }^2 = \hat {\varphi } + 1.
Hint.
- September 1, 2026
Your answer should be one sentence!
- Show that \varphi ^{k+2} = \varphi ^{k+1} + \varphi ^k for all k \geq 0 (by starting with \varphi ^2 = \varphi + 1 and multiplying both sides by \varphi ^k). Note that the same is also true with \hat {\varphi } in place of \varphi .
- Now prove by strong induction on n that F_n = \frac {1}{\sqrt 5}(\varphi ^n -
\hat {\varphi }^n).
Hint.
- September 1, 2026
Don’t forget that you need two base cases; you will want to show that \varphi - \hat {\varphi } = \sqrt 5.
-
Conclude that F_n \approx \varphi ^n/\sqrt 5.
Hint.
- September 1, 2026
What can you say about \hat {\varphi }^n as n gets large?
[GCD5] Time complexity of the Euclidean Algorithm (logarithm review) (2 pts) [00KD]
- September 4, 2026
Suppose we want to run the Euclidean Algorithm to compute \gcd (a,b), assuming a > b. Let F_{n+1} be the smallest Fibonacci number which is greater than or equal to a. Then we know from the previous problem that the Euclidean Algorithm takes at most n steps to run, with the worst case being when a = F_{n+1}. We want to figure out how n relates to a.
Starting from a = F_{n+1} (since this is the worst case), and using the result from the previous problem, namely, that F_n \approx \varphi ^n/\sqrt 5, solve (approximately) for n in terms of a. Your final answer should be of the form n \approx k_1 \log _{10} a + k_2 for suitable constants k_1 and k_2; you should give k_1 and k_2 in approximate, decimal form. Conclude that the Euclidean Algorithm requires, in the worst case, a number of steps proportional to the number of digits in the base-ten representation of a.
[I2] Exponentiation (3 pts) [00KG]
- September 4, 2026
Write a recursive algorithm which, given a real number b and a natural number e, computes the power b^e. Prove that your algorithm is correct.
You may express your algorithm using appropriate pseudocode, or you may implement it in a real programming language; either one is fine. You may use addition, subtraction, multiplication, and division, but you may not use any kind of built-in exponentiation operation.
[I3] The Jellybean Game (3 pts) [00KI]
- September 8, 2026
The jellybean game is a game for two players. There is a row of n jars, numbered 1 to n from left to right, each of which starts out containing some number of jellybeans (possibly zero). We assume that these are magical jars which can hold an unlimited number of jellybeans, so we don’t have to worry about the jars getting too full. (Note, however, that the jars cannot hold infinitely many beans—each jar must always have some finite number of jellybeans in it, but that number can be as big as we want.) We also assume that there is an unlimited supply of extra jellybeans (such as a jellybean factory or a magical jellybean-pooping unicorn).
The players alternate turns. On a player’s turn, she must:
- Pick a nonempty jar, call it jar k.
- Remove one jellybean from jar k.
- Add as many jellybeans as she wants (possibly zero) to each of the jars 1 \dots (k-1) to the left of her chosen jar.
For example, suppose there are five jars which currently hold 1 \quad 3 \quad 0 \quad 2 \quad 6. One possible valid turn is to pick jar 4 and remove one jellybean from it, then add 6, 29, and one billion jellybeans to jars 1, 2, and 3 respectively, resulting in 7 \quad 32 \quad 10^9 \quad 1 \quad 6.
The winner is the player who removes the last jellybean. Put another way, the loser is the first player who is unable to move because all the jars are empty.
- Prove that the jellybean game always ends eventually. That is, even if the players conspire to try to make the game last forever, they cannot, as long as they follow the rules. (Of course, they certainly can make the game take a very, very long time...)
- Describe a winning strategy for the jellybean game. From which positions is the first player guaranteed to win, as long as they play perfectly? What strategy should the first player follow in order to guarantee a win, if a win is possible?