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?