- 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.

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*=*q**b*+*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.

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*c*∣*a*and*c*∣*b*then*g*(*a*,*b*) ≥*c*. We will in fact prove that*c*∣*g*(*a*,*b*) (which clearly can only happen if*c*≤*g*(*a*,*b*).

**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*, *a*∣*a*, 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* = *q**b* + *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* = *c**g*(*b*, *r*) and *r* = *d**g*(*b*, *r*). Plugging this into the formula *a* = *q**b* + *r* we see that *a* = *q**c**g*(*b*, *r*) + *d**g*(*b*, *r*) = (*q**c* + *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*).

**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 *c*∣*a* and *c*∣*b* then *c*∣*g*(*a*, *b*).

This is stronger because if *x*∣*y* then *x* ≤ *y*.

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

In the base case, *b* = 0, we have that *g*(*a*, *b*) = *g*(*a*, 0) = *a*. Since *c*∣*a*, we have *c*∣*g*(*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 *c*∣*a* and *c*∣*b* then *c*∣*g*(*a*, *b*). Choose *a* and *c* and assume *c*∣*a* and *c*∣*b*. We know *g*(*a*, *b*) = *g*(*b*, *r*) where *a* = *q**b* + *r* and 0 ≤ *r* < *b*. If we could prove that *c*∣*b* and *c*∣*r*, then we could apply *P*(*r*) to conclude that *c*∣*g*(*a*, *b*), and we would be done.

Well, we know *c*∣*b*, so all that's left is to show that *c*∣*r*. Since *c*∣*a* we have *a* = *n*_{a}*c* for some integer *n*_{a}. Similarly, *b* = *n*_{b}*c* for some *n*_{b}. Plugging these into *a* = *q**b* + *r*, we have *n*_{a}*c* = *q**n*_{b}*b* + *r*. Rearranging this, we have *r* = (*n*_{a} − *q**n*_{b})*c*. Thus *c*∣*r*, so we are done.