 This lecture is part of Berkeley Math 115, an introductory undergraduate course on number theory, and this lecture will be on Wilson's theorem. So Wilson's theorem tells you what happens if you take p-1 factorial modulo p for p prime. Well, let's start by looking at some examples, and I'm going to do it for p not necessarily prime, so let's take any number m. I'm going to take it to be 1, 2, 3, 4, 5, 6, 7, 8, 9, and let's work out m minus 1 factorial, which gives you 1, 1, 2, 6, 24, 1, 20, 7, 20, 5, 0, 4, 0, 4, 0, 3, 2, 0. Now let's work out what is m minus 1 factorial modulo m, and here we get 1, and here we get 1, which I'm going to write as minus 1. You know, minus 1 is congruent to 1 mod 2, so that doesn't matter. And here 2 is minus 1 mod 3, and here 4 is 6 is 2 mod 4, and here we get 24 is minus 1 mod 5, and here we get 0, here we get minus 1, here we get 0, 0. So, let's see what's going on. Well, if m is a prime, we've got these numbers here, and if we work out p minus 1 factorial, you see there's a rather obvious pattern, which is always minus 1. So, this gives us Wilson's theorem, which says that if p is prime, this implies b minus 1 factorial is congruent to minus 1 modulo p. So, let's try checking it for p equals 11, except I'm feeling lazy, so I'm not actually going to work out 11 minus 1 factorial and reduce it modulo 11. What I'm going to do is I'm going to write out the numbers from 1 to 10, and I want to multiply these together, but before doing that, I'm going to try and save some work by pairing them off. So, first of all, I notice I don't really need to include 2 and 6, because 2 and 6 cancel out, because 2 times 6 is congruent to 1 mod 11, so I can miss them out. Similarly, 3 and 4 cancel out, and 5 and 9s are 45, so 5 and 9 cancel out, and then 7 and 8s are 56, so 7 and 8 cancel out. So, all these numbers cancel out in pairs, and the product is congruent to 1 mod 11. So, 11 minus 1 factorial is congruent to 1 times 10, which are the two numbers left over, which is congruent to minus 1 mod 11. Notice, by the way, that 10 and 1 sort of pair off with themselves, so 10 is its own inverse, because 10 squared is congruent to 1 mod 11, and of course 1 squared is congruent to 1 mod 11. So, that's why these two numbers are left over, they're the numbers that are their own inverses. So, let's see what's going on. So, most numbers a pair off with a number a a to the minus 1, and if a is not equal to minus 1, so if a is not equal to a to the minus 1, these cancel. If a is equal to a to the minus 1, then they don't cancel out, because we don't get a factor of 10 times the inverse 10, because we've already used op 10, if you see what I mean. So, a equals, so the ones that don't cancel are the ones with a congruent to a to the minus 1, which is just the same as saying a squared is congruent to 1. And this says a squared minus 1 is congruent to 0, so a minus 1 times a plus 1 is congruent to 0. And now, because p is prime, this implies a minus 1 is congruent to 0 or a plus 1 is congruent to 0, so a is congruent to plus or minus 1. You remember if a number is prime and divides the product with two numbers, then it has to divide one of them. So, we can now see why Wilson's theorem is true for any prime p. So, if we work at p minus 1 factorial, we get the numbers 1 times 2, up to p minus 2 times p minus 1, and all these numbers here, these pair off as pairs a, a to the minus 1. And these two numbers don't, because p minus 1 to the minus 1 is congruent to p minus 1, and similarly for minus 1. So, p minus 1 factorial is just a congruent to 1 times p minus 1, which is congruent to minus 1 modulo p. So, let's see what happens if p is not prime. So, what is m minus 1 factorial modulo m, where m is not prime? And there's a very plausible argument that m minus 1 factorial is congruent to 0 mod m. Put a question mark, because this actually turns out to be false. And the argument is as follows, suppose m is equal to ab for 1 less than or equal to ab is less than m. Then, m minus 1 factorial includes a and b as factors, so is divisible by a times b, which is equal to m. And this is actually false. Well, it's very nearly true, but it's actually false. And if you go back to this thing we calculated at the beginning, you can see it's false for the number 4. So, point out that m minus 1 factorial is actually not divisible by m in this particular case. And we can see what goes wrong in this case is if we take m equals 4, we can write 4 is equal to 2 times 2. But now, these two factors of 2 are actually the same, so we can't quite deduce that m minus 1 factorial is divisible by 4. This is actually the only counter example, and I'm just going to leave this as an exercise. Check that if m is not prime and m is not equal to 4, then m minus 1 factorial is congruent to 0 modulo m. I mean, you might think that what's going wrong here is that m is a square, and you might think, well, other squares are going to give this problem, but if you check them carefully, you'll find they actually don't. So, this is great because we now have a test for primes where we can now test whether a number is prime, so we can say m is prime if and only if m minus 1 factorial is congruent to minus 1 modulo m. I guess we should take m greater than 1 because otherwise 1 satisfies this. And this test is totally useless. The problem is that it's very difficult to work out m minus 1 factorial mod m, except by first checking whether m is prime or not, in which case you can just use Wilson's Theorem. In general, you can ask the problem, what is a factorial modulo m for some numbers a and m? And this seems hard to calculate. If m is very large, m might be 100-digit prime or something, and then if a is reasonably large, it's very difficult to work this out. I mean, for a up to a few million or a few billion, you can work it out on a computer by multiplying them together, but if a and m both have hundreds of digits, then who knows? This is a bit disappointing because some products are rather easy to work out. We saw we could work out a to the power of b modulo m very fast, even if a, b and m are very large. This is multiplying together large copies of a, but nothing like this seems to work for finding a factor. It's rather unlike there is a really fast algorithm because if there were a fast algorithm, it would be easier to check where the numbers are primes, and this is quite a tricky problem. So let's see some applications of Wilson's Theorem. So the first application is let's find the square root of minus 1. Well, you may think the square root of minus 1 is equal to i if you do complex numbers, but we're not doing complex numbers. What we want to do is to find the square root of minus 1 modulo p. So we want to solve x squared is common to minus 1 modulo p, and we can't always do this. So if we try p equals 3, there's no solution as you can easily check. In general, if p is common to 3 modulo 4, there's again no solution. And this is easier to figure out because if x squared is common to minus 1 modulo p, then x to the 4 is common to 1 modulo p. So x has order exactly 4 because the order must divide 4 and it can't be equal to 2 by this equation. And since we know x to the p minus 1 is common to 1 by Fermat, well, since x has order 4 and x to the p minus 1 is common to 1, this implies that 4 must actually divide p minus 1. So p is common to 1 modulo 4 if minus 1 has a square root. So p can't be 3 modulo 4. That's, I guess, if here we're taking p, I should have said p. Let's take p to be odd because if p equals 2, then x squared equals minus 1 still has a square root. You notice if p equals 2, this argument breaks down because minus 1 is actually equal to 1, so x actually is order 2. So we now have the following problem. If p is common to 1 modulo 4, does minus 1 have a square root? And let's just check a few cases. So we take p equals 5 or 13 or 17 and we notice that 2 squared is common to minus 1 here. So for 13, we can try 2 doesn't work, 3 doesn't work, 4 doesn't work, 5. 5 squared is common to minus 1, so that works here and 17 is easy because that's just 16 plus 1, so 4 squared is common to minus 1. So the first few primes we check minus 1 does indeed have a square root. So can we prove this in general? Well, there's an easy way to do this because it turns out that p minus 1 over 2 factorial is a square root of minus 1 if p is common to 1 modulo 4. And let's see why this is true. Well, let's just take p equals 13 and see what's going on. So we write out the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. These are the numbers, coprime to 13 modulo 13 and p minus 1 factorial, p minus 1 over 2 factorial is going to be the product of these numbers here. And we've also got these leftover numbers and notice these numbers all pair off. 6 is minus 7, 5 is minus 8, 4 is minus 9, 3 is minus 10, and so on. So what is p minus 1 factorial? Well, it's equal to p minus 1 over 2 factorial, which is the product of all these, times p minus 1 over 2 factorial times minus 1 to the 6 because each of these numbers is one of these 6 numbers times minus 1. So multiplying all these together, we get the product of all these numbers together with 6 factors of minus 1. And minus 1 to the 6 is easy to work out. This is just 1, of course. So p minus 1 factorial is given by p minus 1 over 2 factorial squared. Well, we know p minus 1 factorial by Wilson's theorem is just minus 1. So minus 1 is congruent to p minus 1 over 2 factorial all squared if. Well, why did this work? If we have an even number of minus signs. So how many minus signs do we have here? Well, the number of minus signs is just p minus 1 over 2. So the number of minus signs is p minus 1 over 2. So this works if p minus 1 over 2 is even. And this just says p is congruent to 1, modulo 4. So to summarize, we see p, let's take p to be odd, has a square root of minus 1 if and only if p is congruent to 1 modulo 4. In which case the square root is p minus 1 over 2 factorial. You might ask, by the way, what happens if p is congruent to 3 modulo 4? What is p minus 1 over 2 factorial? Well, the same argument as before shows that p minus 1 over 2 factorial all squared. It's again minus 1, this comes from Wilson's theorem, times minus 1 to the p minus 1 over 2. And now this is going to be odd because p is congruent to 3 modulo 4. So this is just congruent to 1. So p minus 1 over 2 factorial must be a square root of 1. So it's either congruent to plus 1 or minus 1. Both of these cases can occur. If we take p equals 3, we get plus 1. And if p equals 7, we get 1 times 2 times 3, which is minus 1 modulo 7. We can also use this idea to give a proof of Fermat or Euler's theorem. So you remember the version of Fermat's theorem says that a to the p minus 1 is congruent to 1. Mod p and Euler's generalization says that a to the phi of m is congruent to 1 modulo m. And let's give another proof of this. What I'm going to do is I'm going to take the product, let's first of all do the prompt p. Let's take the product 1 times 2 all the way up to p minus 1 mod p. And now what I'm going to do is I'm going to compare this with the product a times 2a times 3a all the way up to p minus 1 times a, where a is now co-prime to p. Well, the thing is these numbers a, 2a and so on are just the same as the numbers 1 up to p minus 1 in a different order because we just have a map from these numbers to these ones by taking any number n to a times n and we can take a n back to a to the minus 1 times a n. So these two products are actually the same. However, they differ by a factor of, well it's pretty obvious, they differ by a factor of a to the p minus 1 because you've just got p minus 1 extra copies of a in here. Well, if two numbers are the same and they differ by a factor of a to the p minus 1, this just means a to the p minus 1 is congruent to 1 mod p. Well, we can do the same for Euler's theorem. We get the same proof except we multiply all numbers co-prime to m and the set of all numbers co-prime to m, they're just phi m of these and if you do the same proof, you find 1 times 2 times m minus 1, where these are all co-prime to m and you compare it with a times 2a and so on and just as before, we get a 1 to 1 correspondence if a is co-prime to m so it has an inverse. So the product of these numbers is the same as the product of these numbers and just as before this implies a to the phi of m is congruent to 1 modulo m. The same proof, if you want to work with abstract group theory, the same proof gives a proof of Lagrange's theorem for Abelian groups but this doesn't seem to work for none Abelian groups because it depends on the order you're multiplying things in a bit. So this suggests the following problem. Suppose m need not be prime. I mean it might be prime but more generally it might not be prime. Now we've seen that m minus 1 factorial is not a terribly interesting number if m is not prime. So that's the product of all numbers up to m minus 1 but we can ask instead what is the product of all residue classes co-prime to m? So in our previous proof of Euler's theorem, instead of multiplying all numbers up to m minus 1 we just multiply together the ones that are co-prime to m. Well as usual we should stop by looking at a few examples so let's just do 1, 2, 3, 4, 5, 6, 7, 8 and we write out the numbers as a co-prime to it. So here we have 1, 3, 1, 2, 3, 4, 1, 5, 1, 2, 3, 4, 5, 6. Here we have 1, 3, 5, 7. Let's just do 9, 1, 2, 3, 4, 5, 6, 7, 8 and we take the product of all these modulo m. So what do we get? Well here we get 1, here we get 1, here we get minus 1. I should say that's equal to minus 1. Here we get 1 times 3 is minus 1, here we get minus 1, here we get minus 1. So we've always seen we're getting minus 1, here we get minus 1, here we get, whoops, here we get plus 1 so we don't always get minus 1. So this is a little bit odd because we almost had a generalization of Wilson's theorem. You see, you might think that if you're taking the product of all residue classes co-primes at n then you're going to get Wilson's theorem working but goes wrong for 8. And that's not a calculational error, it really does go wrong for 8. So let's try and see why it goes wrong for 8 and what other numbers it goes wrong for. So what is the product of all residue classes modulo m that are co-prime to m? Well just as before, we can pair off elements with their inverses. So we pair off A with 8 minus 1 and if A is not equal to A to the minus 1 the product is 1. Well if A is equal to A to the minus 1 the product is 1 but we only have one copy of A. So we're left with all numbers A with A equals A to the minus 1. So the product that we're trying to work out is equal to the product over all A such that A is equal to A to the minus 1 of A. So we need to work out what this is. Well suppose there's only one number, there's only one number A not equal to 1 with A equals A to the minus 1 and this must of course be A equals minus 1 because minus 1 is equal to its own inverse. Then we get Wilson's theorem. The product of all numbers A with A, m equals 1 modulo m is equal to minus 1 again. So we saw for instance that this holds for the number 6 or 9 because again for these numbers there's only one number equal to its own inverse other than 1 which is minus 1. What if there are other numbers? Well suppose A squared is congruent to minus 1 and A is not equal to plus 1 or minus 1. Well then we've actually got at least four numbers so 1 minus 1 A and minus A are four different numbers just like x squared is congruent to 1. Suppose these are the only four numbers. Then the product is 1 times minus 1 times A times minus A which is now equal to 1. So if there are exactly four numbers that are square roots of minus 1 then the answer to our problem is 1. So this happens for instance for m equals 8 we can take A equals 3 and the numbers are 1 minus 1 3 and minus 3 mod 8 and we get the product is 1 as before. Well so that's if there are four numbers. What if there are even more? So we've got our number A. So we've got a number A not equal to minus 1 with A squared congruent to 1 and we've got these numbers 1 minus 1 A minus A. And then there's another number B such that B squared is congruent to 1. Well then we can find some other numbers that are also congruent to 1 because minus B also has this property and so does A times B and so does minus A B. So here the product of these is 1. What's the product of these four numbers? Well it's we've got a factor of B squared which is 1 and another factor of B squared which is 1 and then we've got a factor of minus 1 times A times minus A which is again 1. So the product of all of these is 1. And now we see that all the numbers with A that whose square is 1 split into groups of four numbers whose product is 1. There may be more of these. There'll be D minus D and so on. So you can easily see that these collections of four numbers are all disjoint because if B was equal to one of these numbers here then since A and minus A and minus 1 of inverses these two sets would actually be the same. So the numbers with X squared is congruent to 1 split into groups of four. This is assuming there's some number A squared congruent to 1 with A not equal to plus or minus 1 because otherwise if A was equal to 1 or minus 1 the product of these would be minus 1 not 1 with product 1. So the product over all numbers A with A squared equals 1 of A is now equal to 1 because the product of each of these groups of four is equal to 1. So we've now solved our problem. The product over all numbers A that are co-prime to M modulo M of A is equal to either 1 or minus 1. So it's equal to minus 1 if there are if minus 1 is the only solution to X squared equals 1 other than 1 and it's equal to 1 if there are more than two solutions to X squared equals 1. So using this we can now find other cases when the product is actually equal to 1. For example, after 8 the next example is 15. So for 15 there are now there's more than one number whose square is 1 because 1 squared, 4 squared, 11 squared and 14 squared are all congruent to 1 modulo 15. It's because 4 squared is 16 and 11 is minus 4 and so on. So the product over all residue classes co-prime to 15 of A is now equal to 1 not minus 1. OK, that'll be all for this lecture.