 So this lecture is part of Berkeley Math 115, which is an undergraduate course on elementary number theory. So in the last lecture, we were looking at Euclid's algorithm, and we're going to continue looking at in more detail at this. So we saw that Euclid's algorithm could be used to find the highest greatest common divisor of two numbers A and B. And what we're going to do is notice that we sort of get a bonus. So suppose that greatest common divisor is D, then the problem we want is to solve the equation AX plus BY equals D, where we're given A and B and D is the greatest common divisor. And we can solve this using Euclid's algorithm and the best way to explain this is just to work out an example. So suppose you want to solve 71X plus 17Y equals 1. So first of all, we use Euclid's algorithm to find the greatest common divisor, even if we already know the greatest common divisor. So we write 71 equals 4 times 17 plus the remainder of 3, and then we have 17 equals 5 times this number 3 plus a remainder of 2, and then we divide again with 3 equals 1 times 2 plus a remainder of 1, and then we get 2 equals 2 times 1 plus a remainder of 0, and since we've got a remainder of 0, we stop here. And you notice all these numbers are coming through like this, and here we have a number 2 and so on. And now what we do is we know this number here is the greatest common divisor, which is also this number here. Now the key idea is to work backwards. So we start at the bottom, and we've got the greatest common divisor, it's just 1, and then we can look here and we can write 1 in terms of 2 and 3. So we write 1 equals 3 minus 1 times 2. So here this is the greatest common divisor, and we've now written it in terms of these numbers 3 and 2. Now we're going to write it in terms of 3 and 17, and for that we notice we can write 2 in terms of 3 and 17. So what we do is we write 1 is equal to 3 minus 1 times, well 2 is now 17 minus 5 times 3. And now we can rearrange this and we find that 1 is equal to 6 times 3 minus 1 times 17. So we've written 1 in terms of 3 and 17. Now we go back up another step. So we can now write 3 in terms of 71 and 17 using this equation. So we write 1 is equal to 6 times 3 minus 1 times 17, which is, we can write this as 6 times 71 minus 4 times 17, because 3 is equal to this stuff here, and then we have minus 1 times 17. And this is just equal to 6 times 71 minus 25 times 17. And now you see we've managed to write 1 is equal to 6 times 71 minus 25 times 17, which means we've now solved this equation. So x is equal to 6 and y is equal to minus 25. So of course we could solve this by trial and error, but we notice that solving it by trial and error is going to be very much longer. So if we take trial and error, trying to solve ax plus by equals d, this might take approximately a steps because we have to try out y being all the numbers from 0 to a. However, if we're using Euclid's algorithm, we saw earlier that Euclid's algorithm sort of takes the number of steps as at most sort of a logarithm of the number numbers we're working with. So this will take approximately log of a steps in a time some constant or whatever. And if the numbers a and b are really large, you know, with 100 digits, then this number of steps will be far too large to do, even if you've got a big computer, whereas this one is very efficient. So Euclid's algorithm gives us a very fast way of solving these linear equations. So in general, I suppose we want to solve the linear equation ax plus by equals c for any number c. So let's, for example, try and solve 72x plus 26y equals 1. Well, if you try and solve this, this is a bit stupid because it's obviously not solvable because this number is even and this number is even and this number is not even. So you can't solve it. And the problem is that we've got a number dividing a and b but not dividing c. So if this is solvable and d divides a, d divides b, then d divides c. So in particular, c is divisible by the greatest common divisor of a and b. And conversely, if the greatest common divisor of a and b divides c, then the equation is solvable. And we can see this from Euclid's algorithm. So Euclid's algorithm shows that we can solve ax plus by equals d, where d is equal to the greatest common divisor. But if the greatest common divisor divides c, this means that the greatest common divisor sum number z is equal to c. So we can just multiply this by z and we find ax plus by is equal to dz which is equal to c. So if we can solve ax plus by is the greatest common divisor, which we can do using Euclid, then we can solve this whenever this number c is divisible by the greatest common divisor. So to summarize, we see that ax plus by equals c is solvable if and only if the greatest common divisor of a and b divides c. Notice, by the way, that there are many solutions. If you've got one solution, x and y, then we can just add x. We can just add b to x and we can subtract a from y because adding b to x will add a, b to this and subtracting a from y will just subtract a, b from that. So we can add as many multiples of b as we like to x and subtract as many multiples of a as we like from y. If there's one solution, there are lots of solutions. The same works for polynomials in one variable over, say, the real numbers. If we want to solve a, p plus bq equals c, where a, b, c, p, q are now polynomials in x, I'm using p for a polynomial because it's going to be a polynomial in x, so I can't really use x for this variable here. We can see that this is solvable if and only if the greatest common factor, greatest common divisor of a and b has polynomials divides the polynomial c. I'm not going to give an example of this because this isn't really a course on algebra. However, you should notice that this fails for polynomials in two variables over the reals. For instance, if we try and solve x, p, plus y, q equals one, so here I'm looking at polynomials in two variables, x and y, and here this is a bit confusing because I'm taking x and y as being fixed and I'm trying to solve for p and q. So can we find polynomials p in x, y and q in x, y such that x, p plus y times q is equal to one? And certainly the greatest common divisor of x and y is one because there's no polynomial degree greater than one that divides both of these, but we can't solve this because the constant term of this is always zero, whereas here the constant term is not zero. So Euclid's algorithm works for polynomials in one variable, but it actually fails for polynomials in two variables. We can't always solve this equation for polynomials in two variables, even if A and B are co-prime polynomials. Well, that sort of deals with polynomials in equations in two variables. What about equations in three variables? So we want to do linear equations in three variables. So let's do an example. Let's try to solve 6x plus 10y plus 15z equals zero. Now you notice if two of these numbers were co-prime, then we could solve it, sorry, not equal to zero, equal one. Then we could solve it just ignoring the third variable. We notice that these have a highest common factor of two, these have a highest common factor of three, and these have a greatest common factor of five. So we can't solve this by ignoring one of the variables. And the idea is as follows, we first solve 6x plus 10y equals two. Well, why two? Well, obviously because two is the greatest common divisor of 6 and 10. And we can solve this by Euclid. And I'm not going to write this out because I've already shown you how to use Euclid's algorithm. So I'll just say well, one solution is 6 times 2 plus 10 times minus 1 is equal to 2. Next we solve 2w plus 15z is equal to 1. And now 2 is the greatest common divisor of 6 and 10 again. And now we notice that 215 is equal to 1, greatest common divisor of this is equal to 1. So we can solve this using Euclid. And again, I'm not going to write this out in detail. We can just write down a solution, say 2 times minus 7 plus 15 times 1 is equal to 1. So that gives a solution. Now what we can do is we can combine these two solutions because here I've got 2 times something plus 15 times something is equal to 1. But now I can substitute our value that we got for 2 in here. So we find 6 times 2 plus 10 times minus 1 times minus 7 plus 15 times 1 is equal to 1. So it's just substituting the solution to this equation into a solution to this equation. Now if I expand that out, we find 6 times minus 14 plus 10 times 7 plus 15 times 1 is equal to 1. And now we've solved our original equation by sort of using Euclid twice. And you can see it's not too difficult to see that AX plus EY plus CZ equals N has a solution if and only if the greatest common divisor of A, B and C divides N. So when we write several numbers in parentheses like this, this just means the greatest common divisor of A, B and C. And in fact you can work this out just using greatest common divisors of two variables. You first take the greatest common divisor of A and B and then take the greatest common divisor of that and the greatest common and C. And obviously we've done this with three variables but the same works for equations and N variables. So if you've got A1X plus A2X2 plus plus ANXN equals some number M, this has a solution if and only if the greatest common divisor of A1 up to AN divides M. So we can always solve, we've got a very efficient way of solving linear equations and checking whether they have solutions or not. I should say we can do similar things for several linear equations. So if we've got several linear equations A11X1 plus A12X2 equals N1, A211X1 plus A22X2 plus and so on equals N2 and so on. So if you've got a bunch of several linear equations and several unknowns then it's not too difficult to extend the methods we've been doing and show how you can find solutions of this very efficiently but since we don't need this I'm not going to go through some detail. So we seem to have a very satisfactory solution to solving linear equations over the integers. We've got a fast algorithm for doing it however there is a bit of a problem with Euclid. So there's a problem with Euclid's algorithm. It uses long division and trouble with long division is it's kind of a bit slow and complicated. Well you might say this isn't a problem because all computers these days have long division built in. Well they don't if your numbers have hundreds of digits. So if your numbers have hundreds of digits in them you need to program long division in software and this is really rather slow and complicated and you want to avoid it if possible. In fact multiple precision long division is hard even in hardware there was a sort of notorious case some time ago when Intel's Pentium processor turned out to a very subtle bug in its long division double precision algorithm and would sometimes give a very slightly wrong answer and everybody got very excited about this at the time. In fact it even has a Wikipedia article if you want to see about it you can look up Pentium bug or something and I don't know everybody who's involved with trying to program long division was actually rather sympathetic about the poor guys at Intel who had got it wrong because it really is very very tricky to program long division in a way that is both efficient and correct. If you haven't actually tried to do it yourself you shouldn't sneer at people who didn't get it right. It's not easy. So unfortunately there's although Hewlett's algorithm is fast and it's the oldest algorithm still around it's actually probably not the best algorithm so there isn't factors slightly better algorithm and this is an algorithm for finding the greatest common divisor and for that matter for solving linear equations which avoids use of long division and it works as follows so step one is take out all factors of two from A and B and that's very easy because dividing by two is a trivial operation on a computer computer store things in binary so dividing by two just means you're sort of shifting things so we can assume A and B both odd. I should have said this is a we're having an algorithm to find the greatest common divisor of two numbers A and B and we may as well swap them so we can assume that A is greater than or equal to B and now the step two is we change A to A minus B so we instead of doing a division we've got a subtraction and and this is now even and now we see if the result is zero we stop wait when I say the result is zero if A minus B is zero we stop and now what we do is we again take out factors of two you see A is even so so it now has at least one factor of two in it but it may actually have several factors of two and now we just go back to here so instead of doing long division we're just sort of doing subtraction and division by two and in fact somehow one method of doing long division is to repeatedly do this subtraction division by two so in some sense we're sort of doing long division by hand in here but and now you can see that this is still fast this is because every step you must divide one of these numbers by two or possibly four or eight or something so so so each step some number is divided by two so the number of steps is going to be some sort constant times log of these numbers so it takes about the same number of steps as Euclid's algorithm they're both some constant times the times the logarithm of these numbers now it's actually a little bit tricky to compare the speed of this algorithm with the speed of Euclid's algorithm you notice this algorithm will in general take slightly more steps than Euclid's algorithm because Euclid's algorithm you're doing a division whereas here you're only doing a subtraction and then taking out factors of two and a long division can sometimes reduce these numbers much more and however it turns out that most of the time in Euclid's algorithm when you're doing division you're not actually gaining very much more from it and the other key point is that this subtraction step is is a lot easier than the long division step so this is sort of replacing a small number of very long difficult steps or the slightly large number of much easier steps so which of these is better tends to depend on subtle details about how big a and b are and what your computer hardware looks like and what sort of parallel processing it can do and so on so let's just do an example of this so let's try and find the greatest common divisor of say 68 and 142 and what you do is you you start by taking out factors of two so we have we can take out a factor of two we get to 34 then we get to 17 and here we get to 142 so we get to 71 so I've got 17 and 71 again and now these are both odd and you need to keep track of the fact that there's a sort of factor of two dividing both of these you take the minimum number of copies of two you've found and now we subtract one of these from the other so I'm going to take 71 minus 17 so I get down to 54 which is 71 minus 17 and now this again has a factor of two in it so I take out the factor of two and now get to 27 and now again these are both odd so I subtract one from the other and since 17 is smaller I'm going to take this now goes to 10 which is 27 minus 17 and again I'm going to take out a factor of two and now I want to subtract these and this time it's the five that's smaller so so here I go down to 12 which is 17 minus five and now 12 is even so I take out the factor of two and I take out the factor of two again and now um five is bigger than three so I go down to two which is equal to five minus three and now I take out the factor of two which is one and now I can subtract these so I get um two which is three minus one and now I get I can take out the factor of two so I get down to one and now I can subtract one of these from the other so I get zero which is equal to one minus one and I finally got to zero so I can stop and I can see a greatest common divisor is equal to one and so you see it really is taking rather more steps than you could's algorithm but these steps are much easier because I don't need to do division and now if you wanted we could work backwards and solve 68x and plus 142 y is equal to two just by you know starting here and working up just as we did for Euclid's algorithm and by the way when you know finding the greatest common divisor you should remember that this number here is just the greatest common divisor of these two things which you've got by throwing out all the powers of two so the full greatest common divisor you should really take the the greatest common divisor you found here and multiply it by this um power of two in order to find the greatest common divisor of these is really two um so there's one final topic related to this which is the least common multiple so the least common multiple of a and b is the smallest number divisible by a and b um for instance an a times b is certainly divisible by a and b so so the least common multiple of positive numbers a and b is going to be at most a times b but it might be smaller than it for instance the least common multiple of um say um um 12 and 18 it's not going to be 12 times 18 that's too big um it's just going to be 36 so this is definitely less than 12 times 18 and you can see the reason it's smaller is that these things actually have a common factor um so how do we calculate the least common multiple well there's a very fast way of doing it because we can just observe the least common multiple of a and b is just equal to a times b divided by the greatest common divisor of a and b and we can work at this very fast using euclid so we have a fast algorithm for working out um the the least common multiple so let's see why this is true well um i'm going to assume that we can factorize integers into primes which we haven't quite proved yet but um we're going to do so probably in another lecture or two so let's write a is equal to two to the um m two times three to the m three times five to the m five and so on and we'll write b is equal to two to the n two times three to the n three times five to the n five and so on and then we see that a times b is equal to two to the n two plus m two times and so on and the greatest common divisor of a and b is two to the minimum of m two n two times three to the minimum of m three n three and so on and the least common multiple of a and b is equal to two to the maximum of m two n two um times three to the maximum of those and so on and now what we notice is that n plus m is equal to the minimum of m and n plus the maximum of m and n it's kind of obvious and if we insert this in here we find this just implies a times b is equal to the greatest common divisor times the least common multiple because for each prime we just have to say n two plus n two is equal to the minimum of these plus the maximum of these um so for example if i want to work out the least common multiple of say 68 and 142 we don't factor these into primes because factorizing into primes is difficult i mean we were just factorizing into primes as a sort of theoretical tool to show the least common multiple is equal to 68 times 142 divided by the greatest common divisor and we worked this out before it's just equal to two okay um um that's all for this lecture um the next lecture we will be discussing primes in more detail