 In this video, I want to provide the proofs for Fermat's little theorem and Euler's theorem. These are pretty important theorems from number theory, elementary number theory. They talk about reduction one can do with modular exponents. But in the context of group theory, these two theorems are just trivial corollaries of Lagrange's theorem. Remember that Lagrange's theorem says that if you have a subgroup of a group, then the order of H must divide the order of G. As a consequence, if you take any element inside of your group, if you take that element raised to the G power, the order of G power, you always get the identity. That's really the consequence we have right there. Lagrange's theorem tells us that you raised an element to the order of the group, you get back the identity. Euler's theorem and Fermat's little theorem are two things that are going to use that fact right there. While oftentimes in a number theory course, we would first prove Fermat's little theorem for primes, and then you generalize it to Euler's theorem for composite numbers, we're going to work backwards because Lagrange's theorem could shink right here, already gives it to us from that direction. We have to first introduce Euler's totient function. This is often denoted as a phi. The totient function is going to be a function from the positive integers to the real numbers, which admittedly you're not going to get anything other than a whole number, but I'm defining it broadly because, I'm introducing that the totient function here is an example of what's called an arithmetic function. Arithmetic functions are essentially these functions of this form, you take positive integers to the real numbers. But more importantly, this is what we're going to call a multiplicative function. A multiplicative function will define just in a second, but the totient function does the following. If you take phi of one, we're going to define that to be one, and for any other positive integer, phi of n is going to be the number of positive integers that are co-prime to n when you go from zero to n, like so. Let me give you some quick examples of that. Phi of one by definition is equal to one. Phi of two is equal to one because one's the only thing co-prime up to two. Phi of three, you're going to get two, because you have the numbers one and two, which are co-prime to three. Phi of four, you're going to get likewise two because you get the numbers one and three. The issue is that two and zero, they're not co-prime to four, so you get just one and three. If we do say phi of five, you're going to get four numbers because you get zero, one, two, three, and four. Zero is never going to be co-prime, but you get four other ones, one, two, three, four. If you get phi of six, there's going to be two numbers there. So you get zero, one, two, three, four, five. You take away zero, two, three, and four. You're just left with one and five. And we can keep on going with these things here. You're going to get phi of seven is equal to six. Phi of eight, let's see, that's equal to four, I believe. And you can keep on going. Now there's a couple of tricks for computing this totion function quickly. If you ever take phi of a prime number, this is always going to equal p minus one because other than zero, all the numbers less than p will be co-prime to the prime. And one can generalize this principle by taking phi to the pk. If you have a power of prime, this will always equal p to the k minus p to the k minus one. And the idea is because you have a power of a prime, the only thing that would be not co-prime to p to the k will be multiples of p. And so if you count from zero up to pk there, pk should say one less than pk. If you count all the multiples, there's gonna be p to the k minus one many multiples of a prime. And so you take that difference right there. So this allows us to compute prime powers. And then another thing that I was mentioning earlier is that the phi function is multiplicative. A multiplicative function has the following properties that first of all, phi of one is always defined to be one. And then phi of a times b equals phi of a times, and I'll move this a little bit down, I got crowded there. So if the GCD of a comma b is equal to one, then this tells us that phi of a times b will equal phi of a times phi of b. So, and I'm not gonna prove this fact, that that's a very appropriate thing to do like in a number theory course. But what we're just saying is that if two numbers are co-prime, we can compute the phi by factoring amongst co-prime factorizations. So for example, how do I do six? Which is phi of, that's gonna be phi of two times phi of three. Two is a prime number. So it's gonna be two minus one, which is a one. And then three is also prime numbers just three minus one, which is a two. One times two is equal to two. How did we do eight? Well, eight as a power of a prime, you're gonna get eight minus four, which is the previous power of two. So you end up with just a four right there. If we wanted to do something a little bit more complicated, like say, phi of 12, it's not too complicated, but phi of 12 would look like the following. You get phi of four times phi of three. Four is a power of prime, so it's gonna look like four minus two. And then phi is a prime itself, so you're gonna get three minus one. So you end up with two times two, which is equal to four. There's gonna be four numbers co-prime to 12, and this is gonna be one, five, seven, and 11. Those are the four numbers co-prime to 12. So calculating the totion can be very useful. Why is it so useful from a group theory perspective? Well, if you take the multiplicative group, mod n, we've seen that this group will, the number of elements that are in this group will be those elements co-prime to n. Well, if phi counts the number of elements co-prime to n, the order of this group will always be phi of n. All right, so let's now talk about Euler's theorem. Euler's theorem from number theory says the following. Let a and b be integers, so that n is positive, and the GCD of a and n is equal to one. Then if you take phi raised, sorry, if you take a raised to the totions of the modulus power, that is always congruent to one mod n. And how do we see this? Why is this true? Well, this comes from the following fact. If the GCD of a and n is equal to one, that means that a belongs to the multiplicative group Zn star, which the order of Zn star, as we've already mentioned, is equal to phi of n. And by Lagrange's theorem, if you take an element and you raise it to the order of the group, you always get the identity. Therefore, if you take a, which belongs to the multiplicative group Zn star, and you raise it to the order of Zn star, you're gonna get back the identity. So this is a complete immediate consequences of Lagrange's theorem. If you look at it in the perspective of group theory, if you wanna prove this using just divisibility and number theoretic things, it's a lot harder proof. Group theory's already done all the heavy lifting right here. And then a special case of Euler's theorem is what's referred to as Fermat's little theorem, not to be confused with Fermat's last theorem or even Fermat's Christmas theorem, which are other related theorems in number theory. Fermat has a knack of stating theorems in number theory, but not actually proving any of them. I'm not sure if he proved this one either. But anyways, we'll prove it. So if P is a prime number, and if P does not divide A, then A to the P minus one is congruent to one, mod P. Or more specifically, it's often written, I should say more specifically, it's often written in the alternative form that B to the P is congruent to B mod P. So if you raise a number to the Pth power, it's used to get back to the original number when you work mod P. Which I want to admit that this equation right here implies the second one very quickly, because if you have A to the P minus one, this is congruent to one. If you times both sides by A, you'll get A to the P is congruent to A, great. So this version right here is the immediate consequence of this version. So let's prove the first version. This is again an immediate consequences of Lagrange's theorem. We're actually using Euler's theorem right here, because Euler's theorem tells us the following. P of a prime is equal to P minus one. We saw that already. And if P does not divide A, that actually tells us that the GCD of A and P is equal to one. So then by Euler's theorem, this identity holds, which we already approved the second identity. And so that gives us the proof of Fermat's little theorem. Now, why are such theorems actually useful whatsoever? Well, I want to mention that Fermat's little theorem provides one of the first basic primality tests that we have, primality tests. These are actually very important things from number theory. They have some important consequences in computer science, which we'll see actually in the next chapter. A primality test is a test to determine whether a number is a prime number or not. And the most naive of any primality test is just a factor. And if you can factor a number, then you can determine whether it's a prime or not because of the factors that it's not prime. But the problem is when the numbers get big, right? If I come up with like some 100-digit number, factoring is gonna be a brutal task. That would take a long time to do it. Is there some other way of determining whether the number's prime or not, even if we don't factor it? Well, if we are uncertain if n is a prime number, what we can do is we can compute. So basically, this primality test that we're gonna run right now is, we have a number n and we're asking, is it a prime? We don't know. So what we can do is we can then compute b to the n mod n. So we're gonna compute this. And because what we're gonna see is the following is that by Fermat's little theorem, if b to the n is not congruent to b mod n, then that implies that n is not a prime number. So we can prove that a number is composite without factoring it, which is a very impressive thing. Now, in order to compute b to the n mod n, that could be, n could be a very big number. And if we're running this primality test, n is probably very big. So how do we deal with something with really big exponents? Oh, well, we could use the repeated squares algorithm that we presented earlier in this lecture series. We could run this primality test by using repeated squares. So we could very quickly, I mean, because the complexity of repeated squares is essentially just log base two of n right here. It's a very fast algorithm. So we could compute these powers very quickly. And so then we could determine if b of n is congruent to b or not. Now we have to be careful. Fermat's little theorem only goes one direction. If b to the n is not congruent to b, then that will tell us it's not a prime. But if b to the n was congruent to b, that doesn't tell us anything. It doesn't tell us it's prime. It doesn't tell us that it's not prime. We don't know. Now the thing is, in terms of the grand scheme of things, prime numbers are fairly rare. Basically the prime number theorem tells us that the density of the prime numbers amongst the natural numbers is zero. So we chose an arbitrarily large number. Most likely it's not prime. And so if we were to check, like, oh, b to the n, if it's not equal to b, we're done. We know it's not prime. But if it works, if b to the n does equal, or if it is congruent to b, then we just try a different number. Like try a to the n. If that's not equal to a, then we know it's not prime. But if it did equal a, then we try a different one. c to the n, is that congruent to c? If not, then we know it's not prime. If it is, then we kind of keep on going, right? We can often find a number eventually. But of course, if it's a prime number, you'll never find one. And so what this primality test can do is the following. We can determine with confidence that a number is not prime, or we can determine that it's probably prime, right? I mean, it's not an actual proof because this is not a necessary, all right, this condition right here is sufficient to show that a number is composite, but it's not a necessary condition. And so the fact that we can't find a number could just be that, oh, it's hard to find. So this primality test is not the best of any primality test because it can never prove that something's a prime. It can only prove that it's probably a prime. But the issue is, the reason why we can't use this to prove exactly primes is because we have something called Carmichael numbers. Carmichael numbers are exactly those numbers which trick this primality test. Carmichael numbers are numbers which b to the n is congruent to b for any choice of b, but they themselves are, they're their composite numbers. Now the good news is prime numbers, our Carmichael numbers are extremely rare. I think they're even rarer than primes, I believe. I believe they're infinite, but they're very, very sparse. So if you have this primality test and it says this thing is probably prime, it's more likely prime than a Carmichael number. And in fact, you can kind of compare it. So again, this is not the most sophisticated primality test. There are much more sophisticated ones that do come from number theory. But I wanna present to you just to start to think, how does one do these type of algorithms? How does one check whether a number is prime or not? Turns out Lagrange is there and provides us a very naive way of checking whether something's a prime number or not. And I would encourage you to try to practice some of these things yourself. So you could try testing some of these things like with the computer software program, Magma. A link to Magma can be found on this video right here. If you use the command mod exp of a comma n comma, excuse me, a to the m comma n, this will compute, put a semicolon at the end. This will compute a to the m mod n. And so for example, with this primality test, you would put the n right here as the same number over and over again. And so you can check, you could run a for loop. Let me scroll up a little bit. You could run a for loop that basically did the following. So it's like, how do I test? How do I test if n is prime? You could run basically the following test. You could run a for loop where you're gonna be like, hey, A, we're gonna go from two and we're gonna run it to say like, let's go to like a hundred, something like that. And then we're gonna do the following. What we're gonna do is we're gonna compute mod. And we'll have a number to represent this. So let's say that the exponent is equal to E is equal to mod exp of a comma n comma n, semicolon. And then you're gonna run basically, you're gonna kind of return the following, just return these values right here. And so we could, I'll just kind of stick it the following way. So we're gonna say A comma this, we're gonna return the number. And so if these numbers are in agreement, that means that you move on to the next one. And if it's not in agreement, right? We wanna probably do some type of test. So like, are these things equal? If these things are equal, if these things are equal, then we can return if A is equal to that, then, well, basically we just keep on going. So if they're not equal, basically we're gonna do the following. We're going to break, we're gonna break the for loop. And so maybe you could do like a while loop or something if you wanted to. This is just pseudo code, so don't take it too seriously. We wanna break the loop and then we're gonna basically return false in that situation, something like that. But then if the for loop ends without terminating, then instead we return true, which of course that's only probably true. But this is a very simple primality test that one could implement either in magma or gap or basically any computer software program that can support large powers of exponents mod end. And so this is just a simple test that you just have to be able to do. So on the other hand, you can justStar benefits and then wrap up this into pre-signed and then you can do all sorts of things, and then just repeat it and then start working. So that's a simple test. We're gonna do it like this, I'll show you what it's like. So let's say we're running a thing