 So this lecture is part of an online course in the theory of numbers and will be about Fermat's theorem. So Fermat's theorem is the one that says A to the P is congruent to A mod P if P is prime. And it's possibly one of the single most useful theorems in number theory. So it was originally proved by Fermat, but many of Fermat's proofs have just been lost. And the following proof was given by Euler. So Euler spent quite a lot of his life reconstructing Fermat's work. It's quite likely that Euler's proof may be the same as Fermat's proof. It's a fairly obvious way of proving it. So what we do is we prove it by induction on A. And it's obvious for A equals zero, and we're going to prove it for positive A. And the proof for negative A is pretty much the same. So what we want to do is to show that the result for A, so we want to show A to the P is congruent to A mod P, implies A plus one to the P is congruent to A plus one mod P. So this is what we have to prove. And to do this, what we do is we just expand A plus one to the P by the binomial theorem. So this is A to the P plus P choose one, A to the P minus one plus P choose two, A to the P minus two and so on, all the way down to P choose P minus one, A plus one. And now we sort of stare at this and we notice we've got this term A to the P, which by our inductive hypothesis is congruent to A. And here we've got a one, which is congruent to one. So let's look at this A and this one. And these give us A plus one, which is what we want here. On the other hand, we've got all this junk in the middle. So what do we do with this? Well, it's all divisible by P. And the reason being that all these binomial coefficients are divisible by P. So P choose I is equal to P factorial over P minus I factorial times I factorial. And we notice that this is divisible by P, but the denominator is not divisible by P, at least if one is less than or equal to I is less than or equal to P minus one. So if I is zero, this bit's divisible by P. And if I is P, this bit's divisible by P. But anything else, neither of these bits have a factor of P. And here we're using the fact that P is prime, of course. So this shows that A plus one to the P is congruent to A plus one modulo P. So the result is true for all A by induction. There's an alternative version of Fermat's theorem. So one version says that A to the P is congruent to A mod P. The other version says that A to the P minus one is congruent to one mod P, provided A and P are co-prime. Now, the fact that this second version implies the first version is obvious. We just multiply it by A. And you might think you can go from the first version to the second version by dividing by A, but you sort of can. But you've got to be rather careful when dividing in congruences. You remember, we saw this example earlier that two times two is congruent to two times zero mod four, but two is not equivalent to zero mod four. So you can't divide this congruence by two. It gives a wrong answer. So when can we divide a congruent? So suppose that AB is congruent to AC modulo M. Then B is congruent to C mod M if A and M are co-prime. So we need this extra condition to divide by numbers in a congruence. And this follows fairly easily because if A and M are co-prime, then AX plus MY equals one for sum XY. And this says that AX is congruent to one modulo M. So in other words, X is an inverse of A mod M. And now to go from this to this equation here, all we have to do is we get XAB is congruent to XAC mod M by multiplying by X. And then we get B is congruent to C mod M because X times A is equal to one. So we can sometimes divide by numbers in congruences. Now, if you notice up here, we assume that A and P were co-prime, in which case we can divide by A and get the second version of Fermat's theorem. Incidentally, you can ask, is there a generalization of Fermat's theorem to cases when P isn't prime? And it's the second version that generalizes nicely to P not being prime. So we can divide by Euler and we'll be Euler's theorem that we will discuss in a later lecture. Now, we're going to have some applications of Fermat's theorem, but in order to apply it, we need the concept of the order of A modulo M. The order of A modulo M is the smallest integer E greater than zero with A to the power of E is congruent to one mod M. That's if E exists. There might not be any such integer E. For instance, if A is zero, then no power of A with E greater than one is going to be one. And now we notice that A to the N is congruent to one modulo M if and only if N is divisible by E. So this is a very useful fact and this is kind of obvious. First of all, if N is equal to EC, then A to the N equals E to the EC, which is equal to A to the E to the C, which is equal to one to the C, which is equal to one. On the other hand, if A to the N is congruent to one, then we can divide by E with remainder. So we can put N is equal to Q times E plus a remainder R with nought less than or equal to R is less than E. And then we see since A to the N equals one and A to the E equals one, this implies A to the R equals one. And since R is less than E and E was chosen to be smallest integer with this property, this implies R equals zero. So E divides N. In particular, we've got a useful corollary from Fermat's last theorem. So we have A to the P minus one is congruent to one mod P for P A equals one. So the order of A divides P minus one because we said that the order of A divides N whenever A to the N equals one. So this is a very useful fact about integers co-prime to P. We're now going to use that. So let's have our first application. So earlier on, we proved that there were infinitely many primes of the form one plus four N by using the following fact. If P divides N squared plus one, then P equals two or P is congruent to one modulo four. And you can just check this quickly if we take N to be say one, two, three, four, five, six, seven, eight. And we look at the primes dividing N squared plus one. The primes, well one squared plus one is just two. And then we get five and here two and five divide ten. Here we get seventeen, here we get two and thirteen, here we get thirty-seven, two and five, and here we get five and thirteen and so on. You can see that these are all either two or they're one mod four. It's actually not too difficult to check that every prime of the form one mod four divides N squared plus one for some N. Anyway, let's prove this. So suppose P divides N squared plus one. Let's assume P is odd because the case P equals two is completely trivial. Then this says that N squared is congruent to minus one modulo P. That's just another way of saying P divides N squared plus one. Then we see that N to the four is congruent to one mod P. And from this we see that the order of N divides four. So it must be one, two or four. But from this equation here we see that the order is not equal to one or two. So the order of N is four. We should say it's order mod P just because it kind of different orders for other numbers. And now we just apply the observation we had earlier that the order of A with AP equals one divides P minus one. So the order of N which is equal to four divides P minus one. So P is congruent to one modulo four. By the way, we use the fact that P is equal to is odd because we want to know that minus one is not equal to one. Because P is not equal to two. If it was equal to two then the order of N is going to be less than four. Now we're going to have some applications to showing that some numbers are or aren't primes. And for this we need the following useful lemma. Suppose P divides A to the Q minus one but not A minus one. Here we're going to take P and Q to be primes. Then P is congruent to one modulo Q. And this is very useful for finding primes dividing numbers of the form A to the Q minus one as we will see in a bit. Well this is very easy to show using the ideas about orders we have. We know that A to the Q is congruent to one modulo P. So the order of A divides Q. On the other hand the order is not equal to one because we basically said so up here. And Q is prime and since Q is prime the order of A must be one or Q. And if the order isn't one it must be Q which is what we were trying to show. So since the order of A equals Q this implies Q divides P minus one which is what we were trying to show. That's just the same as saying P is one mod Q. And that's because the order of any element has to divide P minus one. Okay now we can start finding some primes. So here's the first example. Let's show two to the thirteen minus one which is eight one nine one is prime. Well you all know how to find prime, how to check whether the number is prime. We can test all primes less than or equal to root n to see if they divide n. So this is the basic test to test whether the numbers are prime. Well it works but trouble is it's rather a lot of work. I mean we would have to test all primes less than the square root of this which would be all primes up to about 90. And you know we could do it but using firm as thin we can greatly speed this up. So if P divides two to the thirteen minus one and P does not divide two minus one. Well this condition is kind of completely vacuous because no primes divide two minus one. Then thirteen divides P minus one. So this is what we proved from the last sheet that two has ordered thirteen mod P. So thirteen must divide P minus one. And now we just need to check primes of the form thirteen n plus one. And there aren't so many of these. N must be even so we get two times thirteen plus one which is twenty seven. Well that's no good. Then we get fifty two plus one which is fifty three. And then we get six times thirteen which is seventy eight plus one which is seventy nine. And then the next one is bigger than the square root of eight one nine one. So the others are bigger than the square root of eight one nine one. So we just check that fifty three and seventy nine do not divide eight one nine one. This is an easy piece of long division. And as you can see this is speeded up testing whether this is prime by factor of about ten because instead of having to check more than twenty primes we just need to check two. Now I want to discuss Fermat primes. So one of the things early number theorists did was they looked at for primes of the form two to the n minus one two to the n plus one and the ones of the form two to the n minus one are called the Mersenne primes and these ones are called Fermat primes. So let's let's try and study Fermat primes. So so we can ask when is two to the n plus one prime. Well first of all if n is odd and greater than one then it's not prime. The reason is that if that if M is odd then X to the m plus one is divisible by X plus one because it's equal to X plus one times X to the m minus one minus X to the m minus two plus X to the m minus three all the way down to plus one. If M was even we would this wouldn't quite work out. So first of all we observe n must be odd. Well we can do better than that. In fact n cannot be divisible by any odd number greater than one because if n was equal to ab with b odd then we can write two to the n plus one equals two to the a to the b plus one and this is divisible by two to the a plus one. So we find n is a power of two. So Fermat investigated these numbers where n is a power of two. Well that's apart from the case n equals naught which doesn't really count. Here we're going to take n greater than zero just to avoid the silly case when n is zero. So we look at the first few cases so two to the two to the naught plus one is three, two to the two to the one plus one is five, two to the two to the two plus one is seventeen, two to the three plus one is two fifty seven, two to the two to the four plus one is six, five, five, three, seven and these are the so-called Fermat primes. And Fermat checked their prime and let's check that six, five, five, three, seven is prime. And we're going to do it in the way that Fermat probably did it. I mean these days you can just type six, five, five, three, seven into Wikipedia and Wikipedia has an entire page about this number telling you it's prime. So that's very quick. Anyway, if you don't have something like that what you can do is follow. So suppose p divides six, five, five, three, seven and let's take p less than a week from the square root of six, five, five, three, seven. We want to show that there's no prime satisfying these conditions. Well this means p divides two to the sixteen plus one. So two to the sixteen is congruent to minus one mod p. Now we can, we can square it and we get two to the thirty-two is congruent to one mod p. And now we can look at what is the order of two? Well it divides thirty-two and it divides thirty-two because of this condition here. It does not divide, it does not divide sixteen and it does not divide sixteen because of this condition here. So two to the sixteen is minus one so two to the anything dividing sixteen can't possibly be one. And since the only factors of thirty-two are either divide sixteen or thirty-two so the order of two is exactly thirty-two. So we can now apply this. So two has ordered thirty-two mod p so p is congruent to one mod thirty-two and we also want p is less than or equal to the square root of six, five, five, three, seven. And now what Fermat presumably did is he wrote down the possible primes p. Well let's first of all write down all numbers that are one mod thirty-two. We get thirty-three, sixty-five, ninety-seven, one twenty-nine, one six one, one ninety-three, two twenty-five and then the next one would be two hundred fifty-seven which is, now we can cross them out as follows. This is too large so we don't need to bother checking it. It's bigger than the square root of that. There are some others we can cross out because they're divisible by three. So that's divisible by three. That's divisible by three and that's divisible by three. And there are some others we can cross out because they're divisible by five. So sixty-five is divisible by five. That's divisible by five for whatever. And if you're quite good at mental arithmetic, you might have noticed that this one is divisible by seven. So this leaves exactly two numbers to check. So we just have to check that 65537 is not divisible by 97 or 193. And at this point you have to stop and do some long division, which I'm not going to do because it's the only thing more tedious than doing long division is watching somebody else do it. So anyway, this has reduced the problem of checking, you know, about 40 or 50 primes less than 257. So just taking two primes, which is a couple of minutes work if you're good at long division. So this is probably how Fermat proved that 65537 is a prime number. Well, what about the next number? So we have 2 to the 32 plus 1. And as I'm lazier than Fermat, I'm not going to write it out explicitly. Well, Euler found a factor of 641. And how did he find this? I mean, Euler was good at hand calculating, but even for Euler testing all primes up to 641 must be a, would have been a bit of a pain. Well, of course he didn't. He used a very similar argument to what we did. So we know that if P divides 2 to the 32 plus 1, then this says 2 to the 32 plus is congruent to minus 1 mod P. 2 to the 64 is congruent to 1 mod P. So as before, order of 2 is 64. So P is congruent to 1 modulo 64. And what Euler presumably did was he wrote down the possible numbers. 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705, 769. I don't know how far Euler went, but he presumably did this. And then you cross off the ones that are obviously not prime. So that's divisible by 5. That's divisible by 3. This one's divisible by 3. This one's divisible by 5. That one's divisible by 3. That one's divisible by 5. Well, that does leave a few to check. And what Euler probably did is he checked 2 to the 32 plus 1 for divisibility by these numbers. And at 641, he got lucky and found a factor. There's actually a rather minor historical puzzle about this, which is how come Fermat himself didn't find this factor? So we know Fermat was quite happy doing large amounts of numerical calculation. I mean, he would have had no problem working out 232 plus 1. In fact, he worked out 2 to the 64 plus 1 explicitly, which is much bigger. And he had no problem doing long division. Testing these five numbers would have taken him a few minutes. And he certainly knew an argument very similar to this one. So why didn't he do that? I mean, he claimed that he thought that all numbers of the form 2 to the 2 to the n plus 1 are prime. Well, no one's quite sure. The most likely guess is that Fermat did check 641 to see whether it was a factor in this and just made a numerical error in his calculation. But we don't know for sure. OK, so next lecture, we'll be showing how to use Fermat's last theorem to check some other much larger numbers to see whether they're primes.