# Euclid's GCD algorithm

• statement of algorithm
• proof that algorithm yields common divisor
• proof that algorithm yields greatest common divisor

Note: we also started modular arithmetic, these will be in the notes for the next lecture.

## Euclid's algorithm to find the greatest common divisor

As a technical tool in the coming lectures, we will need to compute the greatest common divisor of two numbers. Here we write down the algorithm and the property that it needs to satisfy, we will prove that it does satisfy that property in the next lecture.

Given a ≥ 0 and b ≥ 0, Euclid's algorithm defines g(a, b) as follows:

• If b = 0, then g(a, b) is defined to be a.
• If b ≤ a, then g(a, b) is defined as follows: first, use Euclidean division to write a = qb + r. Then g(a, b) is defined to be g(b, r).
• If b > a, then g(a, b) is defined to be g(b, a).

Does this even define a function? It's not clear, because g is defined in terms of itself. For example, if I said "define f by f(x) = f(x), this doesn't actually define a function.

This is an example of an inductive (or recursive) definition. Notice that whenever we define g(a, b) in terms of g(x, y), y is always smaller than b. That means we can only possibly need to compute b other values of g before needing g(aʹ, 0) for some aʹ, and this is well defined.

## Properties of g

I claim that g computes the common divisor of a and b. What does this mean?

• g(a, b) must be a common divisor of a and b: g(a, b)∣a and g(a, b)∣b.
• g(a, b) must be bigger than all other common divisors: if ca and cb then g(a, b) ≥ c. We will in fact prove that cg(a, b) (which clearly can only happen if c ≤ g(a, b).

### g yields a common divisor

Claim: for all a and b, g(a, b) divides both a and b.

Proof: By (strong) induction on b. Let P(b) be the statement that for all a, g(a, b) divides a and g(a, b) divides b.

In the base case, we must prove P(0), i.e. g(a, 0)∣a and g(a, 0)∣0. By defintion of g, g(a, 0) = a. Since a ⋅ 1 = a, aa, and since a ⋅ 0 = 0, a∣0.

For the inductive step, assume P(k) for all 0 ≤ k ≤ b. We wish to show P(b), i.e. for all a, g(a, b)∣a and g(a, b)∣b.

By definition, g(a, b) = g(b, r) where a = qb + r and 0 ≤ r < b. Since r < b, we can apply P(r) to conclude that g(b, r)∣b and g(b, r)∣r. Expanding the definition of , we know that there exists c and d such that b = cg(b, r) and r = dg(b, r). Plugging this into the formula a = qb + r we see that a = qcg(b, r) + dg(b, r) = (qc + d)g(b, r). Therefore, a is an integer multiple of g(b, r), i.e. g(b, r)∣a. Note also that the fact that g(b, r) divides b comes directly from P(r).

### g yields the greatest common divisor

Claim: if c divides a and b (i.e. if c is any common divisor of a and b) then c ≤ g(a, b).

In fact, we will prove a slightly stronger claim:

Claim: if ca and cb then cg(a, b).

This is stronger because if xy then x ≤ y.

Proof: Again, we use strong induction on b. Let P(b) be the statement "for all a and c, if ca and cb then cg(a, b)."

In the base case, b = 0, we have that g(a, b) = g(a, 0) = a. Since ca, we have cg(a, 0).

For the inductive step, assume P(k) for k < b. We wish to show P(b), i.e. for all a and c, if ca and cb then cg(a, b). Choose a and c and assume ca and cb. We know g(a, b) = g(b, r) where a = qb + r and 0 ≤ r < b. If we could prove that cb and cr, then we could apply P(r) to conclude that cg(a, b), and we would be done.

Well, we know cb, so all that's left is to show that cr. Since ca we have a = nac for some integer na. Similarly, b = nbc for some nb. Plugging these into a = qb + r, we have nac = qnbb + r. Rearranging this, we have r = (na − qnb)c. Thus cr, so we are done.