Lecture 10

Another way might be to work out all factors of one of the numbers (aa, for example) and work out which of them are factors of bb. That’s also a pretty terrible way, because factorising numbers is hard work: it seems like a lot of work to find all factors of 123456789123456789 still.

We will see a much better way soon, but, first, let’s spot some easy properties of greatest common divisors.

For all integers aa and bb, we have gcd(a,b)=gcd(b,a),\gcd(a,b) = \gcd(b,a), because the definition is symmetric in aa and bb.

Also, for all positive integers aa, we have gcd(a,a)=a,\gcd(a,a) = a, and gcd(a,1)=1,\gcd(a,1) = 1, and gcd(a,b)=gcd(a,b).\gcd(a,b) = \gcd(a,-b).

A slightly less obvious property is:


Let a,ba,b and kk be integers. Then gcd(a,b)=gcd(a+kb,b).\gcd(a,b) = \gcd(a+kb,b).


We’ll show that the common divisors of aa and bb are the same as the common divisors of a+kba+kb and bb.

Suppose first that dd is a common divisor of aa and bb; in other words, dad\mid a and dbd\mid b. Then we can write a=mda=md and b=ndb=nd for some integers mm and nn. But then a+kb=md+knd=(m+kn)d,a+kb=md+knd=(m+kn)d, so da+kbd\mid a+kb, so dd is a common divisor of a+kba+kb and bb.

Similarly, if dd is a common divisor of a+kba+kb and bb, then we can write a+kb=lda+kb = ld and b=ndb=nd. But then a=a+kbkb=ldknd=(lkn)d,a = a+kb-kb = ld-knd = (l-kn)d, so dad\mid a, so dd is a common divisor of aa and bb.

Since we’ve now proved that aa and bb have the same common divisors as a+kba+kb and bb, it follows that they have the same greatest common divisor.

We should also mention that the greatest common divisor has a close cousin:
Definition: Given two positive integers aa and bb, the least common multiple lcm(a,b)\operatorname{lcm}(a,b) is the smallest positive integer which is a multiple both of aa and bb.

Given that abab is a common multiple of aa and bb, the least common multiple always exists (and is at most abab: we could find it by counting up from 11 to abab, stopping on the first common multiple).

The last piece of terminology we might want is this:
Definition: Two integers aa and bb are said to be coprime, or relatively prime, if gcd(a,b)=1\gcd(a,b)=1.

Division with Remainder

The above Proposition looks slightly dry at first: so what if you can add multiples of one number to another number without changing their greatest common divisor?

It turns out this is the key step in a surprisingly efficient method for calculating greatest common divisors. We can use it to make the numbers smaller; the question is, how? It turns out that this is something familiar to you all:


[Division with Remainder] Let aa and bb be integers, with b>0b>0. One can write a=qb+ra = qb+r for integers qq (the quotient) and rr (the remainder) such that 0r<b0\leq r<b.

It is not too hard to prove this: one can do it with two inductions, for example, (one for the negative and one for the positive integers), but I won’t do so here.

It’s reasonable to ask why we had to take b>0b>0. It’s true for b<0b<0, too, we just have to say that the remainder rr satisfies 0r<b0\leq r<-b instead.

This observation gives us a really efficient way of computing greatest common divisors. Let’s illustrate it by an example.

Suppose we’re trying to compute gcd(126,70)\gcd(126,70). If we divide 126126 by 7070 we get 11 with remainder 5656; in other words 126=1×70+56126 = 1\times 70+56. That means that

gcd(126,70)=gcd(56+1×70,70)=gcd(56,70)=gcd(70,56).\begin{aligned} \gcd(126,70) &= \gcd(56+1\times 70,70)\\ &= \gcd(56,70)\\ &= \gcd(70,56).\end{aligned}

That made the problem much smaller, and we can do the same trick repeatedly: gcd(70,56)=gcd(14+1×56,56)=gcd(14,56)=gcd(56,14).\begin{aligned} \gcd(70,56) &= \gcd(14+1\times 56,56)\\ &= \gcd(14,56)\\ &= \gcd(56,14).\end{aligned} That’s smaller still. Let’s see what happens next: gcd(56,14)=gcd(0+14×4,14)=gcd(0,14)=14.\begin{aligned} \gcd(56,14) &= \gcd(0+14\times 4,14)\\ &= \gcd(0,14)\\ &= 14.\end{aligned} As 5656 is a multiple of 1414, of course we get remainder 00, so we stop here: the greatest common divisor is 1414.

For another example, let’s suppose we want the greatest common divisor of 556556 and 296296. We write gcd(556,296)=gcd(1×296+260,296)=gcd(260,296)=gcd(296,260)=gcd(1×260+36,260)=gcd(36,260)=gcd(260,36)=gcd(7×36+8,36)=gcd(8,36)=gcd(36,8)=gcd(4×8+4,8)=gcd(4,8)=gcd(8,4)=gcd(2×4+0,4)=gcd(0,4)=4.\begin{aligned} &{}\gcd(556,296)\\ &{}=\gcd(1\times296+260,296) {}=\gcd(260,296) {}=\gcd(296,260)\\ &{}=\gcd(1\times 260+36,260) {}=\gcd(36,260) {}=\gcd(260,36)\\ &{}=\gcd(7\times 36+8,36) {}=\gcd(8,36) {}=\gcd(36,8)\\ &{}=\gcd(4\times 8+4,8) {}=\gcd(4,8) {}=\gcd(8,4)\\ &{}=\gcd(2\times 4+0,4) {}=\gcd(0,4) {}= 4.\end{aligned}

Here’s the general case:


[Euclid’s algorithm] Suppose we must calculate the greatest common divisor of two positive integers. Call them aa and bb with aba\geq b. If they’re not in the right order, we can swap them over earlier.

By division with remainder, we can write a=qb+ra = qb+r for some integers qq and rr with 0r<b0\leq r<b.

But then we have gcd(a,b)=gcd(qb+r,b)=gcd(r,b)=gcd(b,r),\gcd(a,b) {}= \gcd(qb+r,b) {}= \gcd(r,b) {}= \gcd(b,r), and since bab\leq a and r<br<b we’ve made our numbers smaller.

If we keep doing this repeatedly, we’ll end up making one of the numbers zero and can stop (since gcd(d,0)=d\gcd(d,0) = d).

One might reasonably wonder just how fast Euclid’s algorithm really is. One good answer (not very hard to prove) is that if you’re trying to work out gcd(a,b)\gcd(a,b) and bab\leq a, then the number of steps you need is always less than five times the number of digits of bb.

So working out gcd(123456789,987654321)\gcd(123456789,987654321) will take less than 5×9=455\times 9 = 45 divisions (actually, this one takes a lot less than 4545, if you try it). Compared with the other methods we discussed, this makes it seem really good.

Euclid’s algorithm is in fact even more useful than it looks: using Euclid’s algorithm, if we have gcd(a,b)=d\gcd(a,b) = d, that enables us to write dd in the form ma+nb=dma+nb=d for some integers mm and nn. (We say that we’re writing it as a linear combination of aa and bb). This will be really useful later: I promise!

Let’s see how this works with an example. We saw earlier that gcd(126,70)=14\gcd(126,70)=14, so we expect to be able to find integers mm and nn such that 126m+70n=14126m+70n=14.

Along the way we found that: 126=1×70+56,(1)70=1×56+14.(2)\begin{aligned} {}126 &= 1\times 70 + 56, &\qquad(1)\\ {}70 &= 1\times 56 + 14. &\qquad(2)\end{aligned} Working through that backwards, we get that 14=1×701×56(using (2))=1×701×(1×1261×70)(using (1))=2×701×126.\begin{aligned} {}14 &= 1\times 70 - 1\times 56 \qquad\text{(using (2))}\\ {}&= 1\times 70 - 1\times (1\times 126 - 1\times 70) \qquad\text{(using (1))}\\ {}&= 2\times 70 - 1\times 126.\end{aligned}

Similarly, when we calculated that gcd(556,296)=4\gcd(556,296)=4, we found that: 556=1×296+260,(3)296=1×260+36,(4)260=7×36+8,(5)36=4×8+4.(6)\begin{aligned} {}556 &= 1\times 296+260, &\qquad(3)\\ {}296 &= 1\times 260+ 36, &\qquad(4)\\ {}260 &= 7\times 36+ 8, &\qquad(5)\\ {}36 &= 4\times 8+ 4. &\qquad(6)\end{aligned} This means that 4=364×8(using (6))=364×(2607×36)(using (5))=29×364×260=29×(296260)4×260(using (4))=29×29633×260=29×29633×(556296)(using (3))=62×29633×556.\begin{aligned} {}4 &= 36-4\times 8\qquad\text{(using (6))}\\ {}&= 36-4\times(260-7\times 36)\qquad\text{(using (5))}\\ {}&= 29\times 36-4\times 260\\ {}&= 29\times(296-260)-4\times 260\qquad\text{(using (4))}\\ {}&= 29\times296-33\times260\\ {}&= 29\times296-33\times(556-296)\qquad\text{(using (3))}\\ {}&= 62\times296-33\times556.\end{aligned}