 This lecture is part of the course Berkeley Math 115, which is an introductory undergraduate lecture on elementary number theory. So this lecture will be mostly about basic properties and divisibility, and we'll start discussing Euclid's algorithm. And so first we'll define some rotation. We define A vertical line B to mean A divides B. You may think this means that B over A is defined, but that's not quite correct. What it really means is that B is equal to x times A for some integer x. And if you think that's the same as saying B over A is defined, it's not quite because 0 over 0 is not defined, but 0 is still x times 0 for any integer x. So in particular we see that A divides 0 is always true for any A. 0 divides A is false unless A is equal to 0. Another example, 2 divides A is just a rather fancy way of saying that A is an even integer. So let's do something a little bit more interesting. We can show that 6 divides N times N plus 1 times N plus 2 for any integer N. So let's see why this is true. Well, if we look at this bit here, we see that N times N plus 1 is always even because N is either even or odd, and if N is odd then N plus 1 is even. So this is divisible by 2. And if we push this a little bit harder, we can see that one of these three numbers is divisible by 3 because if N must be able to form 3N or 3N plus 1 or 3N plus 2, and in any of these three cases one of these three numbers is divisible by 3. So this number here is always divisible by 2 and by 3, and since 2 and 3 are co-prime, this implies it's divisible by 6. And I haven't quite shown yet that if something's divisible by 2 co-prime numbers it's divisible by their product, but that's something we will see fairly soon and it's kind of obvious in this case. Well, that's a kind of rather clumsy way of proving this number is divisible by 6 because we've had to break 6 up into 2 and 3 and then do a separate little tricky argument on both of them and then put them together. And there's actually a much neater way to show this is divisible by 6, which is to identify it with a binomial coefficient. More precisely, N times N plus 1 times N plus 2 divided by 6 is just equal to the binomial coefficient N plus 2, 3. So you remember what this number is, it's the number of ways to choose 3 elements from a 6 element set. And that's quite easy to see because if we're choosing 3 elements there are N plus 2 ways to choose the first one and N plus 1 ways to choose the second and N to choose the third. So there are N times N plus 1 times N plus 2 ways to choose the 3 elements in a particular order, but there are 6 different ways of ordering them so we have to divide by 6. Now the number of ways of choosing 3 elements from 6 elements set is obviously an integer because we're just counting something and that's going to give you an integer. So this number here is an integer so 6 divides this number here. Another example, we always have 8 divides N squared minus N if N is an odd integer. So let's start just by checking a few cases of this. 1 squared minus 1 is 0, that's certainly divisible by 8. 3 squared minus 1 is 8, again obvious. 5 squared minus 1 is 24 and so on. So we can check the first 3 cases. It's always a good idea in a number theory to check the first few cases of a theorem just to see what's going on and to make sure you haven't made some sort of stupid error. So now let's try and prove this is true for all odd N. Well since N is odd we can write N is equal to 2N plus 1 for some integer N and now we find N squared minus 1 is 2N plus 1 squared minus 1 and we expand this out, it's 4N squared plus 4N plus 1 and then we've got to subtract 1 again and this is equal to 4N squared plus N. And we're almost there because we've shown this is divisible by 4 but that's not quite the same as showing it's divisible by 8 which is what we want to do. So to finish off we want to show this bit here is even and how do we do that? Well that's kind of obviously just write this as 4 times N times N plus 1 and one of these two numbers must be even because if N is not even then N plus 1 is even so this is 8M N plus 1 over 2 and this bit here is an integer. So next we just point out that the set of X such that D divides X is something called an ideal of the integers so I'll just recall what an ideal is. An ideal of the integers is just something that's closed under addition and subtraction. If you're working with general rings the definition of ideal is a little bit more complicated but for the integers we can get away with this simple definition and that's kind of obvious because if two numbers X and Y are divisible by D then obviously their sum and the product is also divisible by D so the set of multiples of any integer is always this ideal closed under addition and subtraction. Now we're going to discuss Euclid's division algorithm. This is not quite the same as Euclid's algorithm that we're going to discuss later so Euclid's division algorithm says the following. Suppose that we're given A and B which are positive integers then we can divide B by A with the remainder so Q is going to be a quotient and R is called the remainder and the key point is that we can take 0 less than r is less than A in particular r is in some sense smaller than the number A we're dividing by and let's try and give Euclid's proof of this. Well Euclid had a bit of a problem with division because the Greeks had no good notation for integers I mean the Hindu system of notation only turned up several centuries later and before that came along doing division was such a nightmare it's not even clear that Greeks had a concept of dividing one integer by the other as an algorithm what Euclid did instead was he represented integers or numbers more generally as line segments so you might write B as a line segment like this and you might have another line segment A and then what Euclid did was he took the line segment B and you subtract a lot of copies of A from it you keep going until there's no room to subtract any more copies of it and this little bit left over is then going to be the remainder r and also notice that Q is going to be the number of times you have to subtract A from it actually Euclid had several other problems first of all he didn't really count 0 as a number at all so he had to be careful to take B greater than A otherwise you would have 0 copies of A which is a sort of not really confused Greek mathematicians actually they not only didn't count 0 as a number they sometimes didn't even count 1 as a number which makes this algorithm even more confusing so this proves the algorithm whenever B and A are positive integers in fact you don't really need B and A to be positive if you don't mind not having to represent them by line segment so instead we could take any A not equal to 0 and any integer B if you're willing to use negative numbers and 0 it's not too difficult to give the proof of negative numbers here we're subtracting copies of A from B because A and B are both positive if B is negative you'd have to add copies of A to B and if A was negative you would again have to do subtraction instead of addition at some point we can also notice that it's fairly clear that Q and R are unique up to this condition here and I'm not going to prove that because it's very easy so we can also ask the following basic questions if I said suppose you've got a number D then the numbers X such that D divides X form an ideal closed under addition and subtraction and the converse is also true if you've got an ideal then I is equal to the set of multiples of sum number D greater than or equal to 0 and the proof of this makes use of Euclid's division algorithm what we do is we suppose I has some element A greater than 0 if not I is just equal to the single element 0 and the result is easy I is just the set of multiples of 0 so we may as well assume that we've got some A greater than 0 and we pick the smallest A in I with A greater than 0 and there must be a smallest integer because if you've got any non-empty set of positive integers now suppose B is in I what we do now is we write B is equal to A times Q plus R with naught less than R is less than less than A and now we notice that R which is equal to B minus AQ is also in I because I is closed under addition and subtraction you can get this from A and B by subtraction so because A is the smallest positive element this implies that R must actually be equal to 0 so B is equal to A times Q is a multiple of A so I is just the multiples of A where A is the smallest positive element in I and SI is non-zero this is a basic application of Euclid's algorithm it shows that ideals of the integers are exactly the same as multiples of some particular element now what we want to do is define the greatest common divisor of two numbers A and B so the greatest common divisor is going to be the largest integer such that D divides A and D divides B well this is actually not quite true if A is equal to B is equal to 0 then there is no largest integer dividing A and B so the greatest common divisor of 0 and 0 is set to be equal to 0 for a reason that we will see fairly shortly so the name greatest common divisor is actually a little bit misleading because it's not actually the greatest common divisor if A and B are both 0 and here mathematicians are a bit lazy in writing out greatest common divisor is a bit of a pain so it's normally abbreviated as GCD for greatest common divisor well actually mathematicians are even lazier than that and they quite often miss out GCD and just write the greatest common divisor of A and B as AB and this is a real nuisance because the notation like this can mean dozens of things I mean it can mean an ordered pair in the plane so it could mean a point of the plane or it could mean an ordered pair AB there are several other things that could stand for in elementary number theory it usually means the greatest common divisor and you just have to put up with the fact that mathematical notation is a bit of an ambiguous mess so we have to be a little bit careful in A and B as 0 you notice in particular that the greatest common divisor of 0 and A is always equal to A even if A is equal to 0 so the next question is how do we find the greatest common divisor of 2 numbers A and B well let's start by checking the sum method of finding the greatest common divisor so let's do the stupid method so the stupid method is we check all numbers 1, 2, 3, up to A to see if they divide B and we just pick the biggest and this algorithm obviously works because the greatest common divisor of A and B must be at most A so these are the only entries we need to check however it's really slow so what we want is a fast algorithm and the question is what does fast mean well there are all sorts of technical definitions about polynomial time but in practice fast algorithm means one that's reasonably easy to draw on a computer when A and B have hundreds of digits say A and B might both be around say 10 to the 100 so it'd be nice if we could have algorithms that work even for these sorts of numbers and here this obviously isn't going to work because this is going to take about 10 to the 100 steps and the universal have come to an end before we have time to check all of those so here's a better method what we can do is we factor A and B into primes for example suppose we want to find the highest common factor of 24 and 180 that's the greatest common divisor of these what we do is we factor these numbers into products of primes so 24 is equal to 2 cubed times 3 and 180 is equal to 2 squared times 3 squared times 5 and now if we've got a number dividing both of these it's power of 2 must obviously be at most 2 so here we take the minimum of the powers of 2 take the minimum of these two numbers and the greatest power of 3 dividing it is going to be 3 to the power of 1 so here this exponent here is the minimum of these numbers here and we will get 5 to the 0 because the minimum exponent of 5 is that and so on so the greatest common divisor is just going to be 2 squared times 3 which is 12 and for small numbers up to 100 or so this is actually probably about the fastest and easiest algorithm but there's a problem with this factoring large numbers is hard how hard is it? well nobody really knows people are finding better algorithms for factoring numbers regularly but it's still, if you've got a hundred digit number it's still quite difficult to find its factors and a thousand digit number that's out of the question fortunately there's a much better and much faster method for finding the greatest common divisor this is called Euclid's algorithm not the same as Euclid's division algorithm but it uses Euclid's division algorithm so Euclid's algorithm is possibly the oldest non-trivial mathematical algorithm still in widespread use and the easiest way to give Euclid's algorithm is just to give an example so let's try and find the greatest common divisor of 78 and 14 without factoring them so what we do is we write 78 is equal to 14 times 5 plus 8 so this is division with remainder and then we take the remainder and the 14 and we write 14 is equal to 8 times 1 plus 6 and then we take the 8 and the 6 so take 8 is equal to 6 times 1 plus 2 and then we write 6 is equal to 2 times 3 plus 0 the most confusing thing about this algorithm if you're doing it by hand is trying to remember where you get these numbers from I mean you sort of get them confused with these 5, 1, 1 or 3's which are the quotients and you should ignore and now we see the greatest common divisor of these is equal to 2 and let's try and see why that is true well the greatest common divisor of 78 and 14 is the same as the greatest common divisor of 14 and 8 that's because 8 is just equal to 78 minus something times 14 so if you've got two numbers and subtract multiples from one from the other it's obviously not going to change their greatest common divisor and similarly this is equal to the same as the greatest common divisor of 8 and 6 which is the greatest common divisor of 6 and 2 which is the greatest common divisor of 2 and 0 now the greatest common divisor of 2 and 0 is now completely trivial to work out that's obviously just 2 so we've checked that this algorithm when it terminates does give the correct answer because the greatest common divisor doesn't change at each step we can also check that this algorithm terminates and let's just see why it terminates well it terminates because these numbers here are all decreasing and they have to decrease because each of these is the remainder when you divide something by the previous one and you remember for Euclid's division algorithm the remainder is always less than the thing you divide by so we've got decreasing positive integers and if you've got a sequence of decreasing positive integers it must obviously be finite so this algorithm definitely terminates for the general algorithm if you're trying to find the greatest common divisor of A and B then you do something similar you just write A equals Q1B plus R1 B equals Q2R1 plus R2 R2 equals Q3R2 plus R3 and you go on until you get Rn minus 1 equals Qn sorry R I've got my R's modelled up that should be R1 so Rn minus 1 equals Qn plus 1 Rn plus Rn plus 1 and when this is zero then we stop and the greatest common divisor is going to be this number here so let's try and figure out how fast this is so the stupid algorithm is really slow it took a ridiculously large number of steps the factorization algorithm took however many steps it takes to factorize large numbers so we want to check that Euclid's algorithm really is faster than this and to see how fast it is we can ask what is the worst case well we're writing we will start off by writing B is equal to Q times A plus R we want this to be small because the smaller it is the faster we're going to get to zero so the worst case is when Q is equal to 1 and because then we're just going down by one step every time so we can see an example of this let's try and take the greatest common divisor of 13 and 8 so we write 13 is equal to 8 times 1 plus 5 8 is equal to 5 times 1 plus 3 so 5 is equal to 3 times 1 plus 2 3 is equal to 2 times 1 plus 1 and finally we get to 2 is equal to 1 times well this time we actually get 1 times 2 plus 0 now we've got 0 so our greatest common divisor is 1 so all these numbers here are 1 and you can see we can actually work backwards to find the worst possible case so we notice that when we've got these numbers R1, R2, R3 and so on we have each Ri is equal to Ri plus 1 plus Ri plus 2 except we want this recursion relation to be written backwards so let's try and work on what the Ri's are we start with 0 and 1 then each one is the sum of the two previous numbers so we get this sequence here so this sequence the numbers are given by fn which have the property that fn is equal to fn minus 1 plus fn minus 2 so finding the greatest common divisor of fn and fn plus 1 is the worst case for Hewlett's algorithm in some sense it converges as slowly as possible well these numbers are the famous Fibonacci numbers so Fibonacci's the Fibonacci sequence was introduced by Fibonacci in terms of rabbits so he started with a rabbit I'm a mathematician so I'm going to take my rabbits to be spherical so he starts off with a pair of rabbits and each month so the first month after a pair of rabbits is born they don't do anything but after that the next month they produce a pair of offspring and every month after that they produce another pair of offspring if you've ever kept rabbits you will know that this is a reasonably realistic description of what rabbits do and this pair of rabbits is also going to be similar so after the first month they don't produce any offspring but after the next month they produce more offspring every time and this rabbit does the same this pair of rabbits does the same produce an offspring here and this one does and so does this one sorry that should be there and now we have to see how many rabbits that are at each month here there's only one pair here there are two pairs here there are three pairs here there are five pairs here there are eight pairs here there are 13 pairs and so on so it's quite easy to check that Fibonacci's rabbits are reproducing at this rate well let's try and see how fast the sequence is growing so let's take the sequence 0, 1, 1, 2, 3, 5, 8, 13 can we find an upper bound to it? well we can certainly find an upper bound an upper bound is given by 1, 2, 4, 8, 16, 32 so here each number is equal to that number plus that number which is obviously bigger than that number plus that number so this is an upper bound on the other hand if we write this as 1, 1, 2, 2, 4, 4, 8, 8 and so on we can see that each number is either the sum of the two previous numbers or it's less than the sum of the previous two numbers so this is going to be a lower bound so fn, the growth of fn is sort of less than the growth of 2 to the n but it's bigger than the growth of 2 to the n over 2 so it's sort of exponential growth for some number that we'll actually find out in a little bit longer a little bit later in particular we see that n is something like a constant times the log of fn well n is the number of steps of Euclid's algorithm if we start with Fibonacci numbers so what this means is the number of steps of Euclid's algorithm is going to be bounded by some constant times the log of the number n which is about some constant times the number of digits of n so you see if you've got a thousand digit number Euclid's algorithm is going to take most of the few thousand steps which is going to be really easy on any computer if we compare this with the stupid algorithm you remember the stupid algorithm gave us some constant times n which is vastly bigger the factorization algorithm well how are you going to factorize numbers well a very crude way is just to check all the numbers up to the square root of n which is going to give you a constant times root of n they're a much faster algorithm for factoring but they're all much slower than just a logarithm of n so Euclid's algorithm really is giving you a very fast algorithm which just sort of grows the number of digits of your numbers I want to say a little bit more about Fibonacci numbers first of all they certainly worked first discovered by Fibonacci Indian mathematicians knew about them more than a couple of thousand years ago but nothing in mathematics or science is ever named after the first person who discovered it and it's too complicated to change names so we just sort of stuck with all these wrong names um then you can ask can you find a formula for Fibonacci numbers so we're trying to solve the following equation fn equals fn minus 1 plus fn minus 2 and we want some initial conditions f0 equals 0 f1 equals 1 say this is an example of something called a finite difference equation which is kind of a bit like a finite differential equation except it works with integers rather than real numbers and it's got constant coefficients um so a general finite difference equation for constant coefficients is going to look like fn equals a1 fn minus 1 plus a2 fn minus 2 plus a3 fn minus 3 and so on where the a1 a2 and a3 are constants um and there's a very easy way to solve all these differential equations that I'll just recall what you do is you guess the answer well um yeah I say you guess the answer is it makes you it doesn't make you sound like a professional mathematician so instead of saying you guess the answer you use something called ananzatz and this sounds much better although ananzatz turns out just to mean guess so you haven't really gained anything but what ananzatz is is you guess the form of the answer with some unknown coefficients um so what do you mean by guess the form of the answer well there are several ways of doing this um you can take um say a polynomial in n or a power of n um so power of n could be more generally polynomial you can take n to the lambda plus um a lambda plus a lambda minus 1 n to the lambda minus 1 and so on um you can even take lambda to be real um so what we've got is we've got some unknown coefficients lambda a lambda a lambda minus 1 and so on and we want to kind of adjust this to give a solution well these don't work very well but there are other things we can try and also try things like lambda to the n um so if you've got any equation like a differential equation or a or a different or a difference equation you're not quite sure what to do the the first thing you do is is you just try very easy um possible solutions like these and just see if they work and for finite difference equations this one turns out to work really well so the question is what values of lambda make this work so um let's try and solve f n equals f n minus 1 plus f n minus 2 and we're going to try taking f n equals lambda to the n let's try that as a solution well that means we need lambda to the n equals lambda to the n minus 1 plus lambda to the n minus 2 and we can divide by lambda to the n minus 2 lambda squared is equal to lambda plus 1 well that's just a quadratic equation and we find there are two solutions lambda equals 1 plus or minus root 5 over 2 and there's a bit of a problem here because neither of these values of lambda is going to give us the Fibonacci numbers because for heaven's sake and powers of lambda are going to be they're not going to be integers so so this just doesn't seem to work um but we shouldn't give up because what we can do is we can now take a linear combination of these two solutions so we can take a times 1 plus root 5 over 2 to the n plus b times 1 minus root 5 over 2 to the n so we're taking a linear combination of the two solutions we found and now we what we do is we try to adjust a and b to get the Fibonacci numbers well how do we do that well we know F0 is equal to 0 and F1 is equal to 1 so we want a plus b is equal to 0 and we want a times 1 plus root 5 over 2 plus b times 1 minus root 5 over 2 is equal to 1 well this just says a equals minus b so we find we get root 5a is equal to 1 and b is equal to minus a so this gives us an explicit formula for the Fibonacci numbers we find fn is equal to 1 over root 5 times 1 plus root 5 over 2 to the n minus 1 minus root 5 over 2 to the n that's quite extraordinary when you first come across it because these numbers are always integers but the formula for them involves these real numbers that aren't integers the Fibonacci numbers have enormous numbers of slightly weird properties for example I'm going to write them out and 1,1,2,3,5 and 8 and so on suppose we look at 5fn squared plus or minus 4 so here I'm going to take 5 times this plus 4 which is 9 here I'm going to take 5 times the square of this and I'm going to take minus 4 which is 16 and now I'm going to take 5 times the square of this which is 45 plus 4 is 49 and 5 times the square of this 4 is 121 you can see these numbers are all squares another weird property is suppose we take the product of fn minus 1 and fn plus 1 so the product of these is 3 the product of these is 10 the product of these is 24 the product of these is 65 and you'll notice this is very close to the square of the number in the middle so 2 squared is 4 3 squared is 9 5 squared is 25 8 squared is 64 sometimes it's 1 bigger and sometimes it's 1 smaller so we have fn squared equals fn minus 1 fn plus 1 plus or minus 1 so how do you prove these? well that's really easy you just leave them as an exercise for the people watching the video I'm not going to do it then these aren't very difficult to prove and Fibonacci numbers satisfy endless funny identities like this most of which are fairly easy to prove there's one other funny thing you can do with this if you take this number here this number 1 plus root 5 over 2 is somewhat notorious it's known by phi sometimes by tau and it's called the golden ratio and there's probably been more rubbish written about this number than any other constant except possibly for pi so you'll see if you go searching on the internet you'll see people claiming that the golden ratio controls pyramids and it's behind all beautiful architectural buildings and a rectangle is the most beautiful sort of rectangle if it's sider and golden ratio all of this is complete rubbish the building of the pyramids has nothing to do with the golden ratio and there are almost no buildings that incorporate the golden ratio into their construction you'll probably find one or two because some architects are mad and there's undoubtedly been some mad architect who designed the building based on the golden ratio but whatever there's one thing where it might possibly not be complete nonsense which is in biology you can sometimes find various flowers like sunflowers and sun and occasionally their petals are arranged in ways that look as if they have something to do with the fibonacci numbers it's not really clear if this is significant because if you look at fibonacci numbers you get 1, 2, 3, 5, 8 what you notice is for heaven's sake nearly every small number is a fibonacci number so if you find some random flower and find it's got petals arranged according to numbers 5 and 3 maybe that's something to do with fibonacci numbers maybe it's just a coincidence because all small numbers are fibonacci numbers so almost everything to do with fibonacci numbers or the golden ratio that people claim occurs in real life is probably nonsense there's one place it actually does occur and that's in witchcraft if you want to summon demons or something you draw all these pentangles and burn incense or something now if you draw a pentangle inside a pentagon the ratio of almost any two lines you can think of turns out to be either the golden ratio 1 plus root 5 over 2 or maybe it's square or something for instance the ratio between these two lines here is the golden ratio and the ratio between the lengths of these two lines is also the golden ratio and the ratio between say this line is also the golden ratio so the golden ratio turns up all the time whenever you're doing pentagons or pentagrams okay I think I'll take a break and continue with the rest of this lecture in the next video