 This lecture is part of Berkeley Math 115, an introductory undergraduate course on number theory. This particular lecture will be mostly about congruences and Fermat's theorem. So I'll stop by reviewing congruences. So we say A is congruent to B modulo C just to mean A minus B is divisible by C. So this notation was introduced by Gauss and turns out to be really useful. So you should think of two numbers as being congruent as being sort of the same in some ways if you ignore multiples of C. Congruence preserves addition and subtraction multiplication. So if A1 is congruent to A2 and B1 is congruent to B2, here if the number C is fixed we sometimes miss it out. So we sort of think of these all as being mod C except I'm not actually saying mod C for simplicity. Then this implies A1 plus B1 is congruent to A2 plus B2 and A1 times B1 is congruent to A2 times B2 and A1 minus B1 is congruent to A2 minus B2. So usual arithmetic with addition, multiplication and subtraction is preserved. Division isn't necessarily preserved. For instance we notice that 2 is congruent to 0 modulo 2, but 2 divided by 2 is not congruent to 0 divided by 2 modulo 2. So that's just 1 is not congruent to 0. So there's one thing you've got to be careful of that we'll see things quite often. Let me see quite often A1 to the B1 is also not necessarily congruent to A2 to the B2. So congruences don't work for exponentiation. We'll see a variation that does work for exponentiation a bit later. So a set of residue classes is a set of numbers such that every number is congruent to one of them. Modulo whatever number we were fixing as the number we take congruences with respect to. For instance if we take C equals 5 a set of residue classes might be 0, 1, 2, 3 or 4. That's because if we've got any number it's congruent to exactly one of these numbers modulo 5 because we can write A is congruent to 0, 1, 2, 3 or 4 modulo 5. This is of course just the remainder if you divide A by 5 and you notice these are all different modulo 5. So what we can do is we can think of these numbers as forming a set that's closed under addition and multiplication because we just take addition and multiplication modulo 5. So for instance addition looks like this. So if we add two numbers and here we get 5 which is 1 plus 4 although that's congruent to 0 so we put a 0 then similarly and we get 2, 3, 4, 5, 0, 1, 3, 4, 5, 0, 1, 2, 4, 0, 1, 2, 3 and 0, 1, 2, 3, 4. So this is an addition table for numbers modulo 5 and the multiplication table kind of looks similar. So 0 times anything is going to be 0 and 1 times anything is going to be that thing of course. And if you multiply 2 by 4 you get 4, if you multiply 2 by 3 you get 6 which is just 1 and similarly 2 by 4 is 8 which is the same as 3 and 3 by 3 is 9 which is 4 and 3 by 4 is 12 which is 2 and 4 by 4 is 4 which is 1. So here's how you do addition and multiplication modulo 5. If you've done course in algebra, abstract algebra, you know that you come across this and the set of residue classes actually form something called a ring. So a ring can be defined informally as something with addition, multiplication and subtraction that obeys most of the rules of high school algebra. You've got to be a little bit careful, it doesn't actually obey all the rules. For instance one of the rules of high school algebra is that if A, B is equal to 0 then A equals 0 or B equals 0 and this sometimes breaks down for congruences. Since we have 2 times 3 is congruent to 0 mod 6 but 2 is not congruent to 0 mod 6 and 3 is not congruent to 0 mod 6. It does work if the number you're taking congruences with is prime because if A times B is congruent to 0 mod P where P is prime that means P divides A times B which implies P divides A or P divides B because that's the property we proved for primes which implies A is congruent to 0 mod P or B is congruent to 0 mod P. So none 0 numbers whose product is 0 are called zero divisors so generally if we're working with congruences we can get these rather annoying zero divisors which cause endless problems but if we're working modulo a prime we don't have this problem and that's why people very much like working modulo a prime rather than modulo anything else. So if we're taking a set of residue classes modulo a number m say we can obviously take the numbers 1, 2, 3, 4 up to m minus 1 it's the obvious choice. There's no real reason why we have to do that if we're taking numbers modulo 5 for example instead of taking 0, 1, 2, 3, 4 we could take minus 2, minus 1, 0, 1, 2 Perfectly good choice. If we were really interested in powers of 2 we could take 1, 2, 4, 8 as a residue class except we also need to add in 0 because 0 isn't a power of 2 if we want to be really perverse we could take 10, 21, minus 13, 3, 9, 8, 7, 6, 5, 4 as our residue classes. I mean it would be a kind of a stupid thing to take this as a set of residue classes but it would work. So the usual choice of 0, 1, 2, 3, 3, 4 and so on is the most convenient one but there's nothing really special about it. So let's give a few examples. There's a well known test for divisibility by 9 supposedly we want to test whether 3, 5, 7 is divisible by 9 or more generally what's the remainder if you divide 3, 357 by 9 Well a simple way is just to add up the digits 3 plus 5 plus 7 is 15 then we add up the digits of 15 1 plus 5 is 6 so 357 is congruent to 6 modulo 9 so why does this work? Well you know 357 is an abbreviation for 3 times 10 squared plus 5 times 10 plus 7 times 10 to the 0 so now we notice that 10 is congruent to 1 modulo 9 so this can be written as 3 times 1 squared plus 5 times 1 plus 7 times 1 modulo 9 so we can say these two numbers are actually congruent modulo 9 and this is just 3 plus 5 plus 7 which is 15 and we can do the same thing again 15 is just short for 3 times 10 to the 1 plus 5 times 10 to the 0 which is congruent to 3 times 3 plus... so what's that 3 doing there? That's a 1 so 1 plus 5 modulo 9 so this actually used to be used when people did lots of hand calculations to sort of check for example if you want to check that 1, 2, 3, 4, 5 times 6, 7, 8, 9 is equal to something rather complicated that I don't really care about if you did this by hand there was a pretty good chance you might have made an error so as a check you would make sure that these are the same modulo 9 so you add up the digits of this 1 plus 2 plus 3 plus 4 plus 5 which is congruent to 6 modulo 9 and you do the same thing for this so this modulo 9 is 3 modulo 9 so if you multiply these together it should be 18 which is 0 modulo 9 so you then check and see that the digits of this sum up to something divisible by 9 and if they're not you know you've made an error there's a similar check for divisibility by 11 so if I want to know what is the remainder if you take 1, 2, 3, 4 and divide it by 11 so I want to know what is this congruent to modulo 11 what you do instead of taking the sum of the digits you take an alternating sum so I'm going to take 4 minus 3 plus 2 minus 1 which is congruent to 2 modulo 11 so this gives a fairly quick check to see whether a number is divisible by 11 you just add up the digits with alternating signs and this works for much the same reason as before we can write this as equal to 1 times 10 cubed plus 2 times 10 squared plus 3 times 10 plus 4 times 1 and now we notice that 10 is congruent to minus 1 modulo 11 so this is 1 is congruent to 1 times minus 1 cubed plus 2 times minus 1 squared plus 3 times minus 1 plus 4 times 1 which is minus 1 plus 2 minus 3 plus 4 so we just get the alternating sum of digits so this gives a quick test for divisibility by 9 and 11 it also gives a test for divisibility by 3 because 3 divides 9 a test for divisibility by 2 and 5 are trivial because you know you just look at the last digit which leaves 7 as the only small prime there's no real good test for so if you're used to doing numerical calculation in your head you know that dividing by 7 is the tiresome prime to deal with so another example is 1, 2, 3, 4, 5, 6, 7 a square square of an integer and there's a very quick way of dealing with this we just notice that the last digit is 7 and the last digit of a square can never be 7 and let's try and think why this is well we have a is congruent to b mod 10 where b is equal to the last digit so a squared must be congruent to b squared and what can b squared be? well b must be equal to 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9 so b squared must be 0, 1, 4, 9, 16, 25, 36, 49, 64 or 81 so the last digit must be 0, 1, 4, 9, 6, 5 so these are the only possible last digits of square numbers which you can see by doing the mod 10 in particular this number isn't the square of an integer you can just see it instantly actually doing squares mod 10 sort of works but you can do much better by looking at squares mod 8 so we can ask what is a squared mod 8? well we just look at a which must be congruent to 0, 1, 2, 3, 4, 5, 6 or 7 and look at a squared mod 8 and we get 0, 1, 4, 1, 0, 1, 4, 1 so we find a squared must always be congruent to 0, 1 or 4 modulo 8 so let's have an application of these we can ask can we write integers as sum 3 squares and obviously you can't write all integers like this you can usually check that 7 is not equal to a squared plus b squared plus c squared for any c there are only a few cases to check but in fact we can show there are infinitely many cases when you can't do this so if a is congruent to 7 modulo 8 then we can say a is not equal to b squared plus c squared plus d squared and we can see this as follows because each of these numbers here is 0, 1 or 4 modulo 8 so the sum of all 3 of them must be 0, 1, 2, 3, 4, 5 or 6 modulo 8 because these are the only numbers modulo 8 you can get by adding up 3 of these in particular 7 never appears so no number that is 7 modulo 8 is the sum of 3 squares for example if we take 1 million and 7 this can't be written as a squared plus b squared plus c squared you can see that immediately without checking hundreds of thousands of possibilities for a, b and c if you ask about 4 squares then in fact there's a famous theorem of Lagrange that says that every number is in fact a sum of 4 squares so if you're looking at squares then doing congruences modulo 8 is a really good thing to do we can also do something for cubes so let's try and ask which can we write all but a finite number of numbers as a sum of 3 cubes well cubes don't work modulo 8 so well but they work really nicely modulo 9 so let's look at what a squared is modulo 9 and a cubed is modulo 9 well we notice immediately that a plus 3 cubed is equal to a cubed plus 3a squared times 3 plus 3a times 3 squared plus 3 cubed here we're using the binomial theorem and we notice that this is divisible by 9 so if you add 3 to a number you don't change what it is modulo 9 so to work out what a cubed is modulo 9 we only need to look at 3 numbers minus 1, 0 or 1 because any number can be obtained from one of these by adding a multiple of 3 to it and these are obviously just congruent to minus 1, 0 or 1 modulo 9 so a cubed plus b cubed plus c cubed must be congruent to minus 1, 0 or 1 plus minus 1, 0 or 1 plus minus 1, 0 or 1 modulo 9 and these numbers can be minus 3, minus 2, minus 1 0, 1, 2 or 3 modulo 9 so any number that's a sum of 3 cubes must be must be one of these numbers modulo 9 so if n is congruent to 4 or 5 modulo 9, n is not the sum of 3 cubes question of which numbers are sums of 4 or more cubes modulo 9 turns out to be rather difficult and complicated so now we'll come to the perhaps the single most useful theorem about congruences which is due to Fermat this says if p is prime then a to the p is congruent to a modulo p for example we know immediately that 2 to the 11 minus 2 is divisible by 11 without actually calculating 2 to the power of 11 this fails if mp is not prime if we look for example if we look at 2 to the 6 modulo 6, 2 to the 6 is 64 which is just 4 modulo 6 which isn't congruent to 2 modulo 6 so this doesn't work for most numbers so as another example we might want to show that if we take the number 1 fifth of n to the 5 plus a third of n cubed plus 7 fifteenths n, this sure doesn't look like an integer but this is always an integer and there's an easy way to do this, let's multiply it by 15 so we have 3n to the 5 plus 5n cubed plus 7n which is divisible by 3 and by 5 because if we can show it's divisible by 3 and 5 that shows it's divisible by 15 well now let's check the visibility by 3 when we know n cubed is congruent to n mod 3 so this becomes 3n to the 5 plus 5n plus 7n which is congruent to 3n to the 5 plus 12n which is obviously divisible by 3 and we can do the same thing for 5 we have 3n to the 5 plus 5n cubed plus 7n is now congruent to 3n plus 5n cubed plus 7n and here we've used the fact that n to the 5 is congruent to n and this is congruent to 10n plus 5n cubed and this is obviously congruent to 0 and this is all done modulo 5 so Fermat's theorem is proving expression is also always an integer so how do we prove Fermat's theorem well here we're going to use something we proved about binomial coefficients so we recall that if you've got the binomial coefficient p choose k this is divisible by p if 1 is less than or equal to k less than or equal to p minus 1 for example if we take p equals 5 and we work at x plus y to the 5 we find this is equal to x to the 5 plus 5x to the 4y plus 10x cubed y squared plus 10x squared y cubed plus 5xy to the 4 plus y to the 5 and we notice that all this stuff in the middle is divisible by 5 so we find x plus y to the 5 is congruent to x to the 5 plus y to the 5 modulo 5 and exactly the same thing works for any prime p because if we expand out we find all the binomial coefficients divisible by p except the ones at the beginning and the end so we find x plus y to the p is congruent to x to the p plus y to the p modulo p so you know if you're trying to deal with high school students or something you think that x plus y cubed is equal to x cubed plus y cubed they're usually wrong but they're right if you're working modulo 3 so this formula sometimes makes arithmetic much easier well now we can prove Fermat's theorem by induction so we want to show that n to the p is congruent to n modulo p and let's just check this let's try n equals 0 well 0 to the p is obviously congruent to 0 mod p so that works what about n equals 1 well again it's trivial 1 to the p is obviously congruent to 1 modulo p what about n equals 2 2 to the p well that's equal to 1 plus 1 to the p which is congruent to 1 to the p plus 1 to the p which is congruent to 2 modulo p so that's okay what about n equals 3 well 3 to the p is congruent to 2 plus 1 to the p which is congruent to 2 to the p plus 1 to the p by what we've just shown that a plus b to the p is a to the p plus b to the p which is congruent to 2 plus 1 mod p because we just showed 2 to the p is congruent to 1 which is congruent to three mod p. And now we can just go on like this. More precisely, we prove it by induction. So we assume n to the p is congruent to n modulo p. And then we deduce n plus one to the p is congruent to n to the p plus one to the p by what we just said x plus y to the p is x to the p plus y to the p. And then this is congruent to n plus one because we just assumed n to the p is n modulo p, which is what we're trying to prove. This is n plus one modulo p. So if it's correct for zero and if it's true for number n, it's true for n plus one. So it's true for all positive integers. And if you like, you can prove it for negative integers either by working backwards by induction or noticing that any negative integers are congruent to a positive integer modulo p. So that's the first proof of Fermat's last, Fermat's, it's not his last theorem, Fermat's theorem. We'll be having another proof a few lectures time, which is actually maybe a slightly needed proof. So here's another application of Fermat's theorem. Problem is the number 35 prime. Well, this looks like a kind of stupid question. You can immediately see it's divisible by five and therefore not prime. But what I want you to do is pretend this has say a thousand digits. And I'm going to give you a computer and a thousand digit number. And I want you to tell me whether it's prime or not. And so what I'm going to do is I'm going to run a test for this number that will sometimes work for very much bigger numbers. Well, if it's 35 is prime, then we can ask is two to the 35 congruent to modulo 35? If not, then 35 is not prime. If it is, does that imply 35 is prime? Well, we'll discuss that when we've done this example. So what I'm going to do is I'm going to calculate two to the 35 modulo 35. So let's first try a stupid calculation. What I can do is I can write two times two equals four, two times four equals eight, two times eight equals 16, two times 16 equals 32, two times 32 equals 64, two times 64 equals 128. And I can just keep going like this until I get to two to the 35. Well, that's obviously not going to work if 35 was a really big number, because this would, first of all, this would take more than the age of the universe to do. And secondly, these numbers would get so ridiculously large that they wouldn't actually fit into the universe. So that's no good. Let's try again. Well, here I'm going to go up to two times 16 equals 32. And then I'm going to say two times 32 equals 64. But now I can reduce this modulo 35. So I can say this is this is congruent to 29 modulo 35. And then instead of taking two times 64, two times 29, which is equal to 58 modulo 35, which is congruent to 23. And then I take two times 23 is going to be 46, which is going to be congruent to something else. And I can go on like this. So we reduce mod 35 each step. And this solves one of the problems because now the numbers I get here are not going to grow ridiculously big. They're just going to stay less than 35 or less than this thousand digit number and a thousand digit numbers, computers can cope with that just fine. So, but that's still no good because it's still going to take 35 of these steps. And if 35 was really large, that would just take too long. So here's another idea. So here's a second speed up. Suppose I want to work out two to the 35. Instead of multiplying two times two times two times two times two and so on, which is kind of a stupid way, there's a much slicker way as follows. I write 35 is equal to two to the five plus two to the one plus two to the zero. Okay, so I'm really writing out 35 in binary. And now what I'm going to do is I'm going to work out powers of two. So I'm going to have two to the one equals two. Two to the two equals two squared equals four. Two to the four is equal to four squared, which is equal to 16. Two to the eight is equal to 16 squared, which is congruent to 11, mod 35. And then two to the 16 is now going to be congruent to 11 squared, which is congruent to 16, mod 35. You see, I'm reducing mod 35 to keep it smaller. Then two to the 32 is going to be congruent to 16 squared, which turns out to be congruent to 11, mod 35. So I work out two to the power of a power of two by repeated squaring. And now I can work out two to the 35 by picking out these three here. So I can take two to the, I've got two to the zeroes. One, so I take the one and the two and the 32. And I find two to the 35 is congruent to two to the 32 times two squared times two to the one. Now I just multiply these up, mod 35. And this turns out to be congruent to 18, mod 35. So 35 is not prime. And if you think about this, you will see that this, this algorithm is actually pretty fast, even for a number with say a thousand digits. So for example, if the number has about a thousand digits, then we're going to need to square two a few thousand times, that's not too bad on a computer. And each time we're going to be doing calculations of integers that have a few hundred or a few thousand digits. So, you know, the total number of steps is going to be maybe a thousand times a thousand. And maybe you want to throw in another factor of a thousand because you're multiplying a thousand digit numbers or something. So it's gonna, you know, if you're working with a thousand digit numbers, it will take maybe a thousand cubed or a thousand for four steps or something, which is perfectly reasonable on current computer. So this is a really fast way of checking whether a number is prime. Well, it doesn't always work. So we said that if two to the p is not congruent to two mod p, this implies p is not prime. What if two to the p is congruent to two modulo p? Does this imply that p is prime? And the answer is not always. Here's an example. Let's take the number 561. This is a so-called Carmichael number. Named after some Carmichael who discovered this phenomenon. Now, 561 is the following funny property. 561 is equal to three times 11 times 17. So this isn't the funny property. The funny property is that 561 is congruent to one mod three minus one and is congruent to one modulo 11 minus one. And it's congruent to one modulo 17 minus one, as you can easily check. This just means it's divisible by two and 10 and 16. And what this means is that if you've got any number a, a to the 561, well, this is going to be a to the two times something plus one because it's congruent to 561 is congruent to one modulo two. So it's going to be, and this is going to be congruent to a mod three because a squared is congruent to one. And similarly, this is equal to a to the 10 times something or other plus one, which is going to be congruent to a mod 11 because a to the 10 is congruent to one modulo 11. And finally, it's equal to a to the 16 times something plus one, which is going to be congruent to a modulo 17. Again, because a to the 16 is congruent to one modulo 17. So we find that a to the power of 561 is congruent to a modulo three, 11 and 17. And so a to the 561 is congruent to a modulo three times 11 times 17, which is just 561. So Fermat's theorem actually works for this funny number 561, even though 561 isn't prime. So this is an example of something called a probabilistic prime number test. So you run this test on the number and you know, if you've got a big number N, you might calculate two to the N and ask, is it congruent to two modulo N? And if this fails, then the number's definitely not prime. If it passes it, you might try three to the N as congruent to three modulo N. And if it fails that, then the number's definitely not prime. If it passes and it passes a few more, you might suspect that it's prime, but you can never be certain because it might be one of these funny Carmichael numbers and it's actually now known they're an infinite number of these. Fortunately, there are better probabilistic prime number tests that are less likely to fail. So we do actually have some other tests that we will discuss later. Now I want to complete something that we discussed in an earlier lecture but didn't really finish, which I wanted to show there are infinitely many primes of the form for N plus one, with N greater than or equal to zero. So in other words, if you write the number to base four, then it's last digit is going to be one. And I sort of gave about half of a proof, but there was one step that I didn't carry out. We want to show the following thing. If P divides N squared plus one, then P equals two or P is congruent to one mod four. So the only numbers that divide square plus one prime to the form one mod four. Let's look at the first few cases. Let's take N equals one, two, three, four, five, six, seven, eight. And if we look at N squared plus one, this is two, five, 10, 17, 26, 37, 50, and 65. And if we look at the prime factors, we get two, five, two, five, 17, two, 13, 37, two, five, and here we get five and 13. And we notice that all these numbers here are one modulo four and we never get numbers like three or seven or 11. So we can show this using Fermat's theorem. So let's take P to be odd. And let's suppose N squared plus one is congruent to zero mod P. Sorry, when I said these are one mod four, they're one mod four or two. Two, of course, is not one modulo four. Well, suppose N squared plus one is congruent to zero. Well, this says N squared is congruent to minus one. These are all modulo P. On the other hand, we know from Fermat's theorem that one is congruent to N to the P minus one modulo P. Here we're just dividing N as congruent to N to the P mod P and dividing by N. And this is congruent to minus one to the P minus one over two modulo P because N squared is congruent to minus one. So we must have one is congruent to minus one to the P minus one over two. And this is minus one if P minus one over two is odd and one if P minus one over two is even. So it can't be minus one. So P minus one over two is even. So P is congruent to one modulo four. By the way, there's one thing I've said here. I said we could know that N to the P minus one is congruent to one mod P by dividing by N. And you remember you've got to be a bit careful dividing by N. So we know N to the P is congruent to N modulo P. And if we divide by N, this says that N P minus one is congruent to one mod P if N is not congruent to zero mod P and P is prime. So you remember that if P was a prime, then we can't have two non zero things modulo P multiplying by zero, which means we can actually divide by numbers, provide they're not divisible by P. So we can actually use this congruence. Anyway, so we've shown that if P divides N squared plus one, this implies P equals two or P is congruent to one modulo four. And I just recall how we use this to find an infinite number of primes of the form four N plus one. Suppose we've got some primes P one up to P K, that are all one modulo four. If we want to find a new prime modulo four, we take two times P one times P two times P K and then we square it and then we add one. And we take a prime factor, well, this prime factor must have the following properties. It must be two or one modulo four because it divides a square plus one. Second, it's not equal to two or P one up to P K because if it was equal to two or P one up to P K, it would divide this bit, it would divide the whole lot so it would have to divide one. So it's a new prime of the form one modulo four. So we get an infinite number of primes of the form one modulo four. If you actually try this, it's actually not a terribly efficient way of producing primes of the form one mod four because these numbers get very big, very rapidly. Okay, so that's the first part of this lecture in the second part of the next video, I'll be discussing some more applications of Fermat's theorem to testing numbers for being prime and factorizing them.