Created 26 Jul 2023
Bezout's Lemma and the Extended Euclidean Algorithm: Linear Combinations
tl;dr: A journey from the ground up in which we use axioms to build a proof that every positive integer can be uniquely prime factored.
This is part 3, where we discover and prove Bezout's lemma using the division algorithm. In general, the level of rigor will decrease as the parts go on, so that the reader doesn't get bored to death. But it should be obvious how to fill out everything with complete rigor. Here is a glossary of math symbols.
Puzzle: water-jug problem
¶You might have heard this one before:
There are two water jugs A and B, of size 8 and 5 litres respectively. They have no markings, so it is impossible to tell how much water is in a jug unless it is completely full or completely empty. There is a sink with a water tap and a drain. How can exactly one litre of water be obtained from the tap using the two jugs?
It's a fun puzzle - definitely worth playing around with it before reading the solution below.
;Solution.Let denote that there are litres in jug A, and litres in jug B.
-> -> -> -> -> -> -> -> ->
;
Now it's time for every mathematician's favourite question: can we generalize it?
We start in the state and want to end in the state (goal state)1. Notice how one jug must always be empty or full, due to the rules of the question ("it is impossible to tell how much water is in a jug unless it is completely full or empty"). Therefore we can deduce that in a valid solution that uses as few operations as possible2, the total amount of water only changes by 5 or 8. This is because:
- transferring water from one jug to another does not change the total amount
- filling an empty jug will change the total amount by 5 or 8.
- filling a non-empty but non-full jug is pointless, because the other jug must be empty or full, thus we would obtain the state of both jugs full, or one jug full and one empty, which is backwards progress because those states can be easily reached from in 1 or 2 moves.
- filling a full jug does nothing
So now, we have traction on the problem: the total amount of water is initially 0, finally 1, and only changes by 5 or 8. In the general case of the jug capacities being and , the total must start at 0, end at 1, and change by or at each step.
Therefore, if we can solve the problem then we must be able to write 1 as a linear combination of and , i.e. we must be able to find integers such that , because multiplication is repeated addition.
In the case of 5 and 8, we can write , i.e. .
And so, we've motivated the main question:
Given integers , do there exist integers such that ?
If there do not, then the puzzle can't be solved.3
Numerical Evidence
¶The first thing we might note is that, will always by divisible by , no matter what integers and we choose (see Lemma 5 in part 1). For example if is even (divisible by 2) and is even, then and will both be even, so will be even.
Therefore, if we want to equal 1, we need to be 1, i.e. and share no common factors (are "coprime").
Now what if they are coprime, say, a=5 and b=8?
The key is to write 8 = 5+3. Then a linear combination as 8 and 5 can be rewritten as a linear combination of 3 and 5, and vice versa. Because . Similarly, a linear combination of 3 and 5 can be transformed into a combination of 3 and 2, which can be transformed into a combination of 1 and 2. But we can always write 1 as a linear combination of 1 and 2, i.e. . So theoretically, we should be able to "undo" our sequence of transformations to get back to the combination of 5 and 8!
Let's try:
Which is a linear combination of 5 and 8!
Now what about and ?
Let's do the same thing. Note that a linear combination of 155 and 27 is a linear combination of (27*5 + 20) and 27, which is a linear combination of 20 and 27 because . And so on, this is a linear combination of 7 and 20, which is a combination of 7 and 6 (because 20 divided by 7 has remainder 6), which is a combination of 7-6=1 and 6. We've hit 1, so we can start building up the desired combination by going backwards.
Are you starting to get the idea? Let's do one more example (I also strongly recommend trying some on your own). This time, we'll write out the divisions that we're doing at the start, as well as the reconstruction steps.
First, our division steps that "reduce" the problem:
We stop once we hit (1,0). Now, so we can start reconstructing, by travelling back up the list of divisions.
which is a solution.
Note that I kept the sign in the middle to be negative, which looks like it made all the coefficients positive4.
By the way, this repeated division process (not including the reconstructing) is called Euclid's Algorithm, and is used to find the gcd of two numbers efficiently5. In our case we were finding the gcd of 259 and 443, which is 1, so we ended up with (1,0). Feel free to experiment with what would happen if we had . If we include the reconstructing, then the process of finding a solution to is called the extended Euclidean algorithm (egcd). There are different ways to construct the solution, one of which is exactly as we've done here. (Feel free to try and generalize what we've been doing).
As long as the initial repeated division (Euclid's algorithm) ends with the pair (1,0), we can perform the reconstruction to find a solution to . In general, Euclid's algorithm ends with the pair being , so if we do the reconstruction process, we will be able to find a solution to .
Bezout's Lemma: Proof
¶Let's switch to our axiom world for a second, to formalize the lemma and prove it.
Theorem. (Bezout's Lemma)For any two positive integers , there exist integers such that .
Given that we want to use the well-ordering principle as a proof technique (since it's one of our axioms), we could try to consider the set of all possible (a,b fixed, x,y vary) and take the smallest one, . Then, if we somehow want to generate an element of the set that is smaller than , so that we can show a contradiction.
Let's think about this on the number line. Suppose coprime for simplicity; then we want to show that there is a solution to . If we start at 0 and are allowed to jump left or right by or , can we get to 1?
Well, if we can't, i.e. if the smallest positive number we could reach was where , then we can essentially think of "going from 0 to e" as one operation. Then we can go from 0 to e, e to 2e, 2e to 3e, etc., until we get close to on the number line. By the division algorithm, we can always land in the region between inclusive. But then we can travel left by a, and we will be in the region between inclusive, which is a contradiction because we'd be able to reach a smaller number than .
Proof. (of Bezout's Lemma)Let . Consider the set
This set is nonempty (since, for example, ) and a subset of the naturals by construction. Thus by the well-ordering principle, S has a least element, say .
Claim..
Proof.By the Divison Algorithm, write with and . Then is a linear combination of and . But , thus either , or with . Since the second option contradicts the minimality of , we must have , and so i.e. .
Now by the claim, . Similarly, repeating the above argument analogously for , we have . Thus is a common divisor of and , so by definition of "greatest".
But also, recall that . Since the gcd of a and b divides the RHS by Lemma 4 of the lemma list from part 1, we have that , and so by Lemma 19.
Overall, since and , we have . So , so can be written as a linear combination of and . Done.
Magic box
¶Let's try another concrete example: finding a solution to .
We could do what we did before, which was the Euclidean Algorithm and then building a solution in reverse6. But what if we try the same thing but going forwards?
Uh, oh, it looks like we started with and ended up with . Did we go in a circle? It certainly looks like it, apart from one thing - why did we get in that bracket? Surely there's something special about it.
Recall that, when we built up the solution in reverse, we started with "" then built it up. So here, looking at the last line of the above, why don't we set ?7 This is easy to find a solution to, e.g. .
What is when and ?
It's one!! So could it be that the significance of is that it gives a solution?
In fact, it looks like something even better is true: for convenience, I'll write a compressed version of what we did again:
Look at the coefficients of and in the last line: it fits the pattern that
Look at the coefficients in the penultimate line: it fits the pattern that
Look at the coefficients in the third-to-last line: it fits the pattern that
Etc.: it's true for all the lines!
If we look at these brackets, and list them out in order:
, , , , , ,
Let's stop writing the x and y, and put these in a table instead, where each column represents a bracket:
0 | 1 | 2 | 3 | 5 | 8 | 29 |
1 | 0 | 1 | 1 | 2 | 3 | 11 |
Then, arranged like this, the determinant of each 2x2 square alternates between 1 and -1.
Now, let's think about how we generated these brackets.
Let's say we have written the line , with . For example, if , , , , , then we have the penultimate line of the above.
So what's the next line? Well, we reduce: write , so is the next quotient in the Euclidean algorithm, and is the remainder.
And so, if we have the two brackets and , then the next bracket is .
Writing this in the table form, if we currently have two adjacent columns like this:
... | ||
... |
Then the next column is like this:
... | |||
... |
And so, if we put the quotients in a row on top:
... | ... | ||||||
... | ... | ||||||
... | ... |
Wow, so now we have an efficient, convenient way to compute solutions!
It's almost... magic! So magic, it's called the magic box!
Let's do a couple of examples. First, let's summarize how we found a solution to :
We first do Euclid's Algorithm to find the quotients:
And so the quotients are .
Then we draw out the start of the magic box:
Now we fill out each row from left to right: each number is equal to the quotient above it in the top row, multiplied by the number to the left of it, plus the number two squares to the left of it.
etc. etc. Then, when we get the whole magic box:
The last 2x2 square gives us a solution to , namely , .
Now try if yourself! Compute the magic box for , and hence find an integer solution. Check against the answer below.
The determinant of the last 2x2 box is , and we can figure out which one by counting the number of columns (since the determinant of each 2x2 box alternates between 1 and -1). There are 6 columns (excluding the first 2) and so the determinant flips 6 times. It starts at and so the determinant of the last 2x2 box is .
Hence , so . We've found our solution, namely , .
;
;
The magic box also relates to continued fractions: we actually computed the convergents of , i.e. (best possible) rational approximations using smaller integers. Really, the "magic" of it is that it feels inside-out: originally we were using the quotients in reverse order, but now, we're using them in the same order that we compute them8.
I won't prove that the magic box works, because this article is quite long already. But feel free to try yourself (hint: induction).
Finally, it will feel satisfying to actually write the magic box in terms of 2x2 matrices, since we're talking about determinants:
Let's say the list of quotients is .
Then we build a sequence of matrices (which are the 2x2 squares in the magic box, from left to right): the first is , and for any matrix in the sequence with , we have that:Now we can write this in terms of like so:
Now, using this recurrence, we have that the last 2x2 square in the magic box, the one that gives the solution to , is:Where we extend the definition of the to include .
Note: this recurrence also justifies that the determinant of each 2x2 square in the magic box alternates between 1 and -1, because:
Remarks: first unobvious result?
¶Bezout's lemma is interesting because it is the first thing we've come across that wouldn't be obvious to an average high-school student. Indeed, most people would say, "why so much rigor?" when we're proving things like the division algorithm that just already feel obvious to everyone.
But that's what a lot of maths is about - ensuring we have rigorous foundations to stand on. In this case, we are all convinced that what we are trying to prove is true, and it does indeed turn out to be provably true, but what about when we're trying to prove something we believe, and it's not true? We need to ensure that all proofs are rigorous (enough), otherwise holes could creep in, and one false assumption would render mathematics ostensibly inconsistent.
There are technically two goal states: and , but if we reach one of these then we can reach the other by transferring the water, so it doesn't really matter. ↩
This assumption is important, because it lets us deduce that we never fill a non-empty but non-full jug (as that would create unnecessary steps). ↩
This is the contrapositive statement of "if the puzzle can be solved, then there do exist integers...". ↩
Why? ↩
Why does the repeated division process end up finding the gcd? Isn't that magical? Hint: because something divides both and if and only if it divides both and . ↩
This would be quite inconvenient for a computer algorithm, because we use the quotients in the opposite order to which they are generated, which means we have to store all the quotients rather than working with each quotient as it is generated. ↩
Actually, we can set it to be anything, because it's multiplied by 0: and we always get an integer solution from the two simultaneous equations, because the matrix has an integral inverse due to its determinant! ↩
This is good because a computer can fill in the table at the same time as generating the quotients, so it's very memory efficient. ↩