 So this lecture is part of an online course on the theory of numbers and will be about linear diaphanetine equations. So diaphanetine means you want integer solutions and linear just means you don't have x squared or x cubed or anything turning up. So if we've got only one equation and one unknown then this just says ax equals c which is completely trivial. So let's look at the case where we've got two unknowns x and y and one equation. So we're given a, b and c and we want to find x and y satisfying this. There's an obvious necessary condition. The greatest common divisor of a and b must divide the left hand side so it must divide c. And we want to show this condition is also sufficient. Well the easiest way to explain this is to do an explicit example. So suppose we want to solve 71x plus 17y equals 1 and we notice the greatest common divisor of 71 and 17 is 1, which divides 1 so we're okay. And the idea is we start by applying Euclid's algorithm to find the greatest common divisor of 71 and 17. This seems a bit silly because it's kind of obvious what the greatest common divisor is but let's apply Euclid's algorithm anyway. So we have 71 is equal to 4 times 17 plus 3 so we divide 71 by 17 and get the remainder. Then we say 17 is 5 times 3 plus 2, 3 is equal to 1 times 2 plus 1 and 2 is equal to 2 times 1 plus 0. And since we've got 0 here, the algorithm stops and the greatest common divisor is this number 1. Well I'm going to write a 71 up here and we kind of look at these numbers in this column. And the point is that the greatest common divisor of any 2 is equal to 1. And we can use that to write 1 as a linear combination of any 2 of these by starting at the bottom and working upwards. So we now do the calculation like this. First of all we can write 1 is equal to 3 minus 1 times 2 by using this equation here. Then we can write 1 is equal to 3 minus 1 times 17 minus 5 times 3. And here we're using the fact that 2 is equal to 17 minus 5 times 3 so this becomes 6 times 3 minus 1 times 17. So now we've written 1 as a linear combination of 3 and 17. Now we work up again, we write 1 is equal to 6 times 71 minus 4 times 17. So that's because we can write 3 as 71 minus 4 times 17. So this bit here is 3 and this bit here is 2. I guess, sorry I haven't finished yet, minus 1 times 17 which is equal to 6 times 71 minus 25 times 17. So there we've written 1 as a linear combination of 71 and 17. So we have 6 times 71 minus 25 times 17 equals 1 which is a solution to our original equation. Of course for a number as small as 71 and 17 there's no big deal about finding 6 and 25. We'll just find these by trial and error if we wanted. But you'll notice that this would actually work even if A, B and C were absolutely huge. I suppose A, B and C each had a thousand digits in or something like that. You couldn't possibly solve the equation by trial and error but on any reasonable computer it would be still fairly fast to find X and Y because as we saw last lecture Euclid's algorithm is actually pretty fast. By the way here we're using the version of Euclid's algorithm with division with remainder. I mentioned there was actually a more efficient way of finding the greatest common divisor where you only divide by 2 and use subtraction and don't do division at all and you can also use that to solve linear di-fantine equations. In fact it's probably even faster if X and Y are very large. Anyway you can see from this that we can solve AX plus BY equals C for X and Y if and only if the greatest common divisor of A and B divides C. Now on the one hand this condition is obviously necessary. On the other hand it's also sufficient because we can solve AX plus BY is the greatest common divisor by using Euclid's algorithm as above. We've done it for 71 and 17 but it should be kind of obvious that this would work for any pair of numbers and give the greatest common divisor. And then we can just multiply this by C over AB. If we multiply by C over AB then we find A times X times C over AB plus B times Y times C over AB is equal to C. So we have complete control over this equation. There's a simple criterion for when we can solve it and we've got a really fast algorithm for solving it if it's solvable. Incidentally the same works for polynomials over a field. In other words if we want to solve AX times P of X plus BX times Q of X equals C of X where AB and C are given polynomials and we want to find P and Q then we can solve it in the same way. The point is that polynomials over a field have a sort of Euclidean algorithm. If we've got two polynomials AX and BX then we can write AX equals Q of X times B of X plus R of X where the degree of R is less than the degree of B. And this is just like the condition for division with remainder we had for integers except that we're using the degree rather than the absolute value. So we can find an analog of Euclidean's algorithm for polynomials in one variable and solve equations like this. Well Euclidean would not have been too happy with our solution of AX plus BY equals C because he didn't actually really like negative numbers and didn't really use them at all. I mean for Euclidean number was the length of a line and it doesn't really make a whole lot of sense to have a negative length of a line. So we can ask the following question. Suppose A, B and C are greater than or equal to zero, can we solve for XY with X and Y greater than or equal to zero? Actually strictly speaking Euclidean didn't really count zero as a number either so he would probably have insisted that X and Y should be at least one but we'll do the case that X and Y are at least zero because there's not really a lot of difference between them. So let's look at an example. Let's solve 7X plus 5Y equals C and ask what possible values can we find for C with X and Y greater than or equal to zero? Obviously if we allow X and Y to be negative then we can solve for all integers C by what we've just said. Well let's just take a look and see which values of C we can get. So we have five times something so 5Y can be 0, 5, 10, 15, 20, 25 and 7X can be any multiple of 7 so we get 7, 14, 21, 28. So the possible values of 7X plus 5Y look like this, we get 7, 12, 17, 22, 27, 14, 19, 24, 29 and so on, 21, 26, 31 and so on. Now if we look here we get some sort of random looking numbers, we can get 5 and 7, we don't get 8, we don't get 9, we get 10, we don't get 11 and so on. However if you go up far enough, suppose you go up to 24, then we get 24, we also get 25, we get 26, we get 27, we get 28, we get 29. And we get 30, this 30 is over here, we get 30, 31 and we seem to be getting all numbers greater than or equal to 24. So can be any number greater than or equal to 24, well we haven't quite proved this yet but it suggests that we can solve this for any sufficiently large number C divisible by the greatest common divisor. And for numbers less than this critical number, well it seems to be kind of random, it's not clear what's going on. And let's show that this is true and find out what this critical number actually is. So suppose we found a solution of 7x plus 5y equals n, where we're allowing x and y to be possibly negative, well suppose x is negative, then what we can do is we can change x and y. If we add 5 to x and subtract 7 from y, then the answer is still 7, so we've made x bigger at the cost of making y smaller. Well suppose we can't solve for n, well suppose we keep on making x bigger until it's just less than 0, so here x is going to be less than 0 and here x plus 5 is going to be greater than or equal to 0. So we're sort of looking at the critical point at which x goes from being negative to being positive. Now if we can't solve for n, then this thing here has to be less than 0, otherwise we would have a solution. So what's the largest value of n? So what's the largest possible n? Well we want x and y to be as big as possible given these constraints, so we would take x equals minus 1 and y minus 7 equals minus 1. And this gives us 7x plus 5y equals n, so n is 7 times minus 1 plus 5 times 7 minus 1, which is equal to 7 times 5 minus 7 minus 5, which is equal to 23. So this is the largest number you can't represent as a sum of positive or negative linear combinations of 5 and 7. And you see in this argument there's nothing special about 5 and 7. If we're trying to solve ax plus by equals c for xy greater than or equal to 0 and the greatest common divisor of a and b divides c, then this is possible. So let's take the greatest common divisor of a and b to be equal to 1 just for make things a little bit simpler. Then this is possible for c greater than a b minus a minus b. So we've shown that a b minus a minus b is the largest number which you can't solve this equation for. You can think of this as being a sort of stamp problem. Suppose for some reason you could only buy stamps that cost either 5 cents or 7 cents. Then you can send a letter for any postage greater than 23 cents. Next we can ask, well we've done equations with two unknowns, what about three unknowns? So suppose you try and solve ax plus by plus cz equals d. Well this can quite easily be reduced to the two variable case. So we can solve ar plus bs is the greatest common divisor of a and b. And then we can solve tab plus u times c is equal to the greatest common multiple of a, b and c which is just a, b, c. And then if we can solve this equation, we can solve this equation. Then we can just multiply this equation by t and add it to that and solve our original equation. So we can solve if a, b, c divides d. And this gives us a necessary and sufficient condition. And of course we can solve this rather quickly using Euclid's algorithm. A single linear equation in any other unknowns can easily be reduced to the case of linear equations in two unknowns which can be done with Euclid's algorithm. By the way, if you want to solve things like ax plus by plus cz equals d for x, y and z greater than or equal to zero, this is kind of tricky. You can solve it, let's assume a, b and c is equal to one. You can solve it for all large values of d, but finding the minimum value of d is quite a lot trickier than for the case when you've just got two variables. So that's pretty much dealt with the case of one equation in two or more unknowns. What about several linear equations? So suppose you've got a system a11x1 plus a1nxn equals c1, a21x1 plus a2nxn equals c2, all the way down to amnm1x1 plus amnxn equals cm. So this is just like in linear algebra. Now if we were doing this over a field, you know we can reduce to echelon form and we would just, if we write this as a matrix equation ax equals c where we're using various vectors and so on for a and c. Then we can reduce to the case of a matrix which looks like this or it may have more zeros elsewhere and so on. And once we've reduced to this form then it's easier to solve. The problem is if we're working over the integers we can't do this because I mean you know you'd have to start by taking a11 being none zero and then you would subtract a multiple of a11 from a21 to make it zero. And we can do that over a field but we can't do it over the integers. So what do we do over the integers? Well over the integers what we're going to do is we use row and column operations to make a11 as small as possible or rather make its absolute value as small as possible greater than zero if necessary. By row and column operations I mean on this matrix a we're allowed to add multiples of any row to any other row and similarly we're allowed to add and subtract multiples of any column from any other column. And if we make a11 as small as possible then we can subtract multiples of a11 from everything in the first row and everything in the first column to make the other entries of row one column one. Equal to zero and then we can just repeat this and make the matrix more or less diagonal. I think this will become clearer if I actually work out an example. So suppose we've got the system of equations 2x plus 3y plus 4z equals 5 and 6x plus 7y plus 8z equals 9. So we can write this in terms of matrices we have 2, 3, 4, 6, 7, 8 multiplied by x, y, z equals 5, 9. And now what we're going to do is we're just going to do row and column operations on this matrix in order to keep simplifying. You'll see that doing these row and column operations looks very much like Euclid's algorithm. First what we can do is we can, here we've got a 2 and a 3 so we can subtract a multiple of column one from column two in order to make this entry smaller. So obviously to do that we just subtract one copy of column one from copy two so we get 2, 1, 4, 6, 1, 8. We've got to be a bit careful here because in doing that we actually have to change these variables and we change these to x plus y, y, z. So you see what we're doing is we're subtracting column one from column two here and we're adding row two to row one then. If you sort of think about it a bit you'll see that this makes the equations stay the same. And now what we can do is we can subtract a multiple of, we can subtract a multiple of the first row from the second row in order to make this entry zero. So we get 2, 1, 4, 4, 0, 4, x plus y, y, z equals 5, 4. And now here we're doing a row operation and if you do a row operation you don't need to fiddle with this matrix here but you do need to fiddle with the right hand side. So row operations change this bit here and column operations change this bit here. And now we're going to do some more column operations so we're going to subtract twice this column from this column here so that will produce 0, 4, 1, 0. And we're going to subtract four times this column from this column so we get 0, 4. And then we need to do something complicated here so we need to add twice this and four times this to that. So here we change this to x plus y and then we get 3y plus 2x plus 4z here and then we get z here and this is equal to 5, 4. And now we can subtract column 1 from column 3 so we get 0, 1, 4, 0, 0, 0. And again we're doing column operations so we need to do something to this and what we need to do to this is get x plus y plus c, 3y plus 2x plus 4z, z equals 5, 4. And now what we can do is we can work backwards. You see this matrix is more or less diagonal at least it would be if I swap the first two rows around and I'm not going to swap the first two rows because I've run out of room but it doesn't really matter. And now you can see that this equation says 0 times z is undefined so z can be anything. So what are x and y? Well we don't know but we have to work backwards. So we know 3y plus 2x plus 4z is equal to 5. You've got to be a bit careful you might think it's equal to 4 but notice that's not in the top left position. And we know 4 times x plus y plus z is equal to 4 so x plus y plus c is equal to 1. Okay now we can work backwards so now we can work out what x plus y is from this. So x plus y is going to be x plus y plus c minus c which is, well we don't know what z is so let's just write this as 1 minus c so z is something. And then we can keep going backwards so next we can, so that's found this x plus y. Next we can find this y by working backwards from here so you see this y is going to be this thing here minus 2 times x plus y minus 4 times z. So y is going to be this stuff here which is 5 minus 2 times x plus y which is 1 minus c minus 4 times z which is 3 minus 2z. And finally we can work out x in the same way by saying it's x plus y minus y so this is x plus y minus y which turns out to be 2 minus 2 plus c. So here we found the solution we can pick any value of z and then y is equal to 3 minus 2z and x is equal to 2 minus 2 plus c. And it should be reasonably obvious that if you've got any system of linear equations and you want to solve them for integers then you can do a similar sort of thing and either find that they've got no solutions or find a neat description of all solutions. Incidentally this method of solving linear systems of di-fantine equations turns out when you're studying finitely generated abelian groups. And pretty much exactly this argument here proves the theorem in abstract algebra that any finitely generated abelian group is a direct sum of cyclic groups. That's just a sort of fancy way of saying you can solve linear systems of linear di-fantine equations by reducing the matrix to a sort of diagonal form.