 This lecture is part of Berkeley Math 115, an introductory undergraduate course on number theory, and will be mostly about continued fractions. And so we're going to be using them to solve Pelean equations. I'll just give a quick bit of background. What we want to do is to solve equations in several variables where f is some sort of polynomial. And the complexity depends on the degree of f. It can be one, two, three, four, and so on. Once a degree, one are called linear because they sort of give the equations of lines. Once a degree, two are called quadratic because they've got squares in them and squares are called quadratic things. Degree three are called cubics. Degree four are called quadrics and so on. Unfortunately, this terminology is a bit of a mess. We can also talk about the number of variables. And if there's one variable, this is kind of very easy to solve because it's just a polynomial and you can, in one variable, so you can find its roots over the reals and so on. So if it has two variables, this is called a binary polynomial. If it's three variables, it's called ternary and so on. Now degree one polynomials are rather easy to solve using Euclid's algorithm and polynomials in one variable are rather easy to solve. So the next first sort of non-trivial case is degree two polynomials in two variables. And these sort of look like ax squared plus bxy plus cy squared plus dx plus ey plus f equals zero. And by making changes of variables, you can sort of get rid of the linear terms. You just change x to x plus a constant to complete the square and get rid of that and you do the same for y. So what we have to do is to solve an equation form ax squared plus bxy plus cy squared equals some constant. So this bit here, as I said, is a binary quadratic form and the next few lectures are going to be mostly about binary quadratic forms. So a very famous example of this is the Pellian equation, which we want to solve x squared minus dy squared equals one. It is named after this guy called Pell who had absolutely nothing to do with solving the equation. The name is some sort of historical accident, but it's kind of stuck, so that's what everybody calls it. So it was actually solved many centuries before Pell. I think one of the first people to solve it was this Indian mathematician Brahma Gupta. He was in about 650 AD. So this was really very early, almost a thousand years before Europeans managed to solve this equation. And he said one who can solve the equation x squared minus 92y squared equals one within a year is a mathematician. If you try and solve it by trial and error, you're probably not going to manage it, at least not if you do it by hand. So we want to find a way to solve equations like this. And if you think about this, this equation says that x squared minus 92y squared is approximately zero. So that means x over y squared is approximately 92, so x over y is approximately the square root of 92. So what we have to do is to find a rational number x over y close to this number, the square root of 92. And this is a very general problem in mathematics. You want to find a good rational approximation to some real number. And there's a good technique of doing this using continued fractions, which I'll illustrate as follows. So as you take the number pi, and we want to find a good rational approximation to it with x and y integers. Well, let's take the integer part of it as approximately three plus something. This is 0.14159 and so on. And we're going to write pi as a continued fraction. Now a continued fraction means you want to write pi as some integer plus one over some integer plus one over some integer plus one over some integer and so on. And it's fairly obvious how we can do this. We take this number here and we write one over this bit here is equal to, well, it's going to be about seven plus some remainder term. And then you take one over this bit here and it will be it's about 15 plus something. And then this something one over this something is about one plus something and one over something turns out to be about 292 plus something. And so pi is going to be approximately three plus one over seven plus one over 15 plus one over one plus one over 292 and so on. And when you've got a continued fraction like this, it's quite easy to find a good rational approximation. For example, here's the first rational approximation. What you notice is that one over 15 plus something is approximately zero. Well, you know, because 15 is quite a large number. So we find that pi is approximately three plus seventh, which is the well known classical approximation to it, but you can do better than that. You notice this number 291 over 292 is even closer to zero. So pi is going to be approximately three plus one over seven plus one over 15 plus one over one. And if you work out what this is, turns out to be 355 over 113, which is a very famous approximation to pi found several hundred years ago. And it's quite good because it's got three digits here, but it gives it gives pi correct to more than. So it's got six digits here, but it actually gives pi correctly to more than six digits. So this is about 3.1415929 or something. And pi is actually 3.1415926. So you see it's got 1234567 digits correct, even though you've only used six digits in the approximation. So anyway, so continued fractions are a really neat way of finding good rational approximations to numbers. Now let's go back and solve for Pelean equation x squared minus 92 y squared equals one. And first of all, we notice that 92 is actually divisible by four. So we try solving x squared minus 23 y squared equals one, which is going to be a bit easier because 23 is smaller. And if we're lucky, y will be even and then we'll have a solution to this by dividing y by two. And if it isn't even, well, we'll see what we can do about that. So let's let's first solve this. Well, we expand 23 as a continued fraction. So you get out your pocket calculation, you find the square of 23 is about four plus some error term that I'm not going to worry about. And one over this error term is about one plus something and one over this something is about three plus something. And you continue like this, you find the square root of 23 can be expanded as something like four plus one over one plus one over three plus one over one. One over one plus one over eight and so on. And you keep going like this. And you can take various bits of this and see how good they are as a solution to this. For instance, you might say three is quite large. So we try saying root 23 is about four plus one over one. So that's going to give us the number five. So we get five squared minus 23 times one squared equals two. Well, two is a little bit 2B. We actually want this to be equal to one. So this approximation isn't quite good enough. Well, how about we try, you know, there's quite large number eight here. So let's try setting this equal to zero. So we get that the square root of 23 is about four plus one over one plus one over three plus one over one. And now you can work this out. Well, that's four plus one over one plus one over four and one plus one over four is five over four. So that's four plus four fifths, which is equal to 24 over five. And now if we try this, we find 24 squared minus 23 times five squared is 24 is 576. And if you subtract 23 times 25, that's 575. So this is equal to one. So we've solved our original equation here. And all we need is for this number five to be even and then we can solve x squared minus 92 squared y squared equals one. Well, unfortunately, as you probably noticed, the number five is not even. So what do we do about this? Well, it turns out we can actually find lots of other solutions to this equation if we've got one of them. So here we've got 24 minus 23 times five squared equals one. I'm going to write this as 24 minus five square root of 23 times 24 plus five times the square root of 23 is equal to one. And now you notice if I've got a minus b root 23 times a plus b root 23, that's going to give me a solution of a squared minus 23 b squared equals one. And what I can do is I can just square this. So we get 24 minus five root 23 squared times 24 plus five root 23 squared is also equal to one. And now what I do is I can just write this as something plus something times the square root of 23. And if you square this out, it turns out to be 1151 minus 240 times the square root of 23 times 1151 plus 240 times the square root of 23. Now this is equal to one. And now you see we've now got another solution of x squared minus 23 y squared equals one. We've got 1151 squared minus 23 times 240 squared is equal to one. And now this is now become even so we can solve our original equation 1151 squared minus 92 times 120 squared is equal to one. Here I've just multiplied this by four and divided this by four. So we've found a solution of the original equation. So now what I want to do is to do a rather more complicated example just to show you how bad things can get. So this time I'm going to do x squared minus 67 y squared equals one. And I'm going to do it a little bit more explicitly. So let's write out the continued fraction. Well, we have the square root of 67 and now I'm going to do everything explicitly with integer arithmetic. So the square root of 67 is equal to let's leave a bit of space here eight plus the square root of 67 minus eight divided by one. And so this is the start of the continued fraction and then I take the inverse of this. So one over square root of 67 minus eight is equal to square root of 67 plus eight over three, which is equal to five plus the square root of 67 minus seven over three. And if you work out what you get as a solution to the equation here, here we've got the square root of 67 is about eight. So we try eight squared minus 67 times one squared. And this is equal to minus three and that's no good. We want this to be equal to one. And here if we look at the continued fraction eight, we would have eight plus a fifth. And that if we work that out, that's giving us 41 squared minus 67 times five squared. That doesn't work either. That's equal to six, which isn't one. So this is using the approximation root seven is about eight. This is using the approximation square root of 67 is about eight plus one over five, which would be 41 over five. So we get 41 and five there. So that doesn't work. So we don't give up. We go on to the next term, which is three over the square root of 67 minus seven, which is equal to the square root of 67 plus seven divided by six, which is equal to two plus the square root of 67 minus five over six. And let's see what we get here. Well, here we now get 90 squared minus 67 times 11 squared is equal to minus seven. That's no good either. And here we're using the approximation of eight plus one over five plus one over two for the square root of 67, which is 90 divided by 11. Well, if that doesn't work, we just keep going. So the next one is six over the square root of 67 minus five. And I'm going to miss out some of the calculation because it's getting a bit boring. This is going to be one plus square root of 67 minus two over seven. And if you work out what we get here, we actually get a nine. Let me write these out. There's one over 31 squared minus 67 times 16 squared, which is equal to that. And now working out the continued fractions are getting a bit tedious, but there's a quicker way of doing it. What you notice is there's actually a recursion relation for each of these numbers. So 90 is equal to two times 41 plus eight. So the 41 and the eight are here and the two is here. And similarly, 11 is two times five plus one. So the five and the one are here and this two is here. So we're getting a sort of recursion relation for these numbers, depending on this number here. For example, here we would use this number one and we would get 131 is equal to one times 90 plus 41. So there's the 90 and the 41. And similarly, we get 16 is equal to one times 11 plus five. So there's the 11 and there's the five and there's the one. So we can work out these numbers here using a simple recursion relation. And let's do a few more. So we get seven over the square root of 67 minus two, which is one plus the square root of 67 minus seven over nine. And here we would get 221 squared and here we get 27 and we get a minus two. And you notice the 221 is one times this plus this and 27 is one times that plus that. And the next one is nine over the square root of 67 minus seven. And here we get seven plus the square root of 67 minus seven over two. And here we get a nine. So that's no good. We do a few more. Two over the square root of 67 minus seven is equal to one plus the square root of 67 minus two over nine. And that's no good. We get a minus seven there. And we get a nine over the square root of 67 minus two. And this is equal to one plus square root of 67 minus five over seven. And does that work? No, we get a six there. And the next one is seven over the square root of 67 minus five. And this is equal to two plus the square root of 67 minus seven over six. And we get a minus three here. And you wonder how long is this going to go on for? Well, if you stare at it, you see there's something funny going on. Look at these numbers here. Minus three, six, minus seven, nine, minus two, nine, minus seven, six, minus three. Can you see a pattern? Well, one pattern is that it's kind of symmetric. So these numbers here are all the same. And this is sort of seems to be in the middle of something. So we seem to be coming to the end because, you know, we started with minus three, six, minus seven, nine. And now we're going back down again. And this suggests we should be getting to something, something interesting fairly soon. So the next one is six over the square root of 67 minus seven. And this is giving us five plus the square root of 67 minus eight over three. And now the number we get is one. And that shouldn't be too surprising because actually I missed out a number at the beginning here. We actually had a solution one squared minus 67 times zero squared is equal to one. I mean, this equation does have a rather trivial solution x equals one, of course, which is not the one we're interested in. So this one here corresponds to this one here. Now we've finally found a non-trivial solution. And now if you work at all these numbers here, which I won't bother doing because it's not terribly interesting, you find this number here is actually 48842 squared minus 67 times 5967 squared is equal to one. So we found a solution of this equation, which by hand, and as you see, you wouldn't have been able to find this by hand by trial and errors. It's just too big. So continued fractions give a very efficient way of finding quite large solutions of of Pellian equations. And if you look at this, you can see there are several other patterns going on. For example, these numbers, you know, six minus seven nine minus two nine. Well, if you look at the denominators here, you're getting six seven nine two nine seven. So the same numbers except with with signs every second step. And as we said before, there's another pattern that if you look at these numbers here, they're giving the recursion relations for these numbers here. So you can work these out by some simple recursion relations. And if you write them out to you, you can find several other patterns going on. And really the best thing to do to understand these is not to watch somebody else do it, but to do an example yourself. So let me give you an example if you want to practice on. So here's a sort of exercise. Let's do x squared minus 61 squared 61 times y squared equals one. And we want to find a non-trivial solution. I should warn you that the solution to this is actually really rather large. And if you manage to do this by hand, you are being you are extremely patient. But it does work in the end. By the way, there's a problem we didn't solve, which just show this process actually terminates. I'm not going to prove that because the proof is a little bit messy to follow, but becomes kind of obvious if you do this calculation by hand and sort of understand what's going on. The point is you can find all these numbers here are bounded by roughly the square root of 67. So there are only a finite number of possibilities for what you get here. So eventually this process must cycle. And once it cycles, it just keeps on cycling. And if it's cycling, you can cycle it backwards and you can cycle it backwards and get to the one that you started with up here. So if you cycle forwards, you must also get to a one eventually. So rather than give a proof that this terminates, I will say if you actually try it on an example, a couple of examples, you will sort of understand it so well that you can kind of see that it always terminates. If you just say a little bit more about how you can find an infinite number of solutions. Now the problem with this example here is that although it has an infinite number of solutions, the infinite number of solutions get rather big. For instance, the next one is 4771081927 squared minus 582880428 squared times 67 is equal to 1. And I don't want to have to deal with numbers like this. So I'm going to do a much smaller example to illustrate how to find infinitely many solutions. So let's just do x squared minus 3y squared equals 1. And this is a very simple solution, x equals 2, y equals 1. So now I want to show you how to generate an infinite number of solutions from this. So we just copy what we did for 92. We observe that 2 plus root 3 times 2 minus root 3 is equal to 1. And now we can just raise both sides to the power of n. 2 plus root 3 to the n times 2 minus root 3 to the n is equal to 1. And this will be a plus b root 3 and this will be a minus b root 3 and this will be equal to 1. So we find that a squared minus 3b squared equals 1. So this gives us a new solution of this for every value of n. So let's just do the first few. So 2 plus root 3 squared is equal to 7 plus 4 root 3. And we find 7 squared minus 3 times 4 squared is equal to 1. That's 49 minus 3 times 16, which is 48. And 2 plus root 3 cubed is equal to 26 plus 15 times the square root of 3. So we find 26 squared minus 3 times 15 squared is equal to 1. That's 576. That's 3 times 225 and so on. So we can generally find an infinite number of solutions of x squared minus dy squared equals 1 unless we obviously can't. So we certainly want to take d to be not a square, for example. Otherwise there are no solutions at all. But if d is a square, then the continued fraction thing kind of breaks down because the square root of d is already an integer. So that shows how to use continued fractions to solve this equation. What we're going to do in the next lecture is look in a bit more detail at more general quadratic forms. So in general we want to solve the equation ax squared plus bxy plus cy squared equals some constant. And to do this we'll be studying these binary quadratic forms in more detail.