 This lecture is part of an online course on the theory of numbers, and will be about some tests for primes. In other words, suppose we've got a number n, we want to try and tell whether it's prime or not. These tests won't actually always work, but they will give some information. So the last lecture we used Fermat's theorem to show that we could use it to speed up testing numbers of the special form 2 to the n plus or minus 1 to see if they're prime. And what we're going to discuss is what about general numbers n? So suppose n doesn't have any special form, and what we want to do is we want to think of n is large. If n isn't large, we can just test to see whether it's prime by just testing for the divisibility by all numbers up to its square root or something. So we might think of n as being, say, about 10 to the 100. It might have 100 decimal digits. So it would be completely impractical to test all prime factors up to the square root of n. Well, we can get some information by using Fermat's theorem. Fermat, you remember, showed that if n is prime, then 2 to the n is congruent to n. And we can ask what if n is not prime? Well, let's take a look. Let's take n to be 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. And work out what is 2 to the n mod n, just to get an idea of what's going on. So for 2 we get 0. Here we get 2, 0, 2, 6, 64 is 4. That's 2. 8 gives us 0, 9, 5, 12 is 8. 10 is 1024, which is 4. 11 is 2. 12 is 4. 13 is 2. And now you see that whenever n is prime, the result is 2, as predicted by Fermat's theorem. Oops, it isn't 2 there. Well, it actually is because 0 is congruent to 2 modulo 2. So that's OK. And we also see that if n isn't prime, then the result is usually not 2. You can ask, is it always not equal to 2? And unfortunately it can sometimes be 2. We'll see an example a little bit later this lecture. But most of the time it's not equal to 2 if n is not prime. So this gives us the following test. Compute 2 to the n and reduce mod 2. And then if the result is not 2, n is not prime. If the result is 2, then n is probably prime. So this is an example of something called a probabilistic test. So it doesn't always give you a definitive answer, but it sort of gives you some evidence in favour of one conclusion or the other. So if you were doing Bayesian reasoning or something, your confidence that n was prime would go up if it passed this test. Well, there's one obvious problem which is how do we actually carry this out? So let's try and work out, let's try and calculate 2 to the n. Well, that's easy. We work out 2, then we multiply it by 2, then we multiply it by 2 again, and we go on until we've got 2 times 2 and so on, n times. It's easy enough working out powers of 2. Well, there's a bit of a problem with this. Problem one, if we work out 2 to the power of n, you remember we were taking n to be about 10 to the 100. Well, this will have about 10 to the 100 digits, so 2 to the n fills most of the observable universe. And problem two takes a really long time to compute. Like if you turn the entire visible universe into some vast parallel computer and let it run for the age of the universe, maybe you might manage to sort of compute this number, but it's, you know, this could be called the stupid algorithm. And what we want to do is to find a couple of improvements to it to make it actually practical. So we need 2 improvements. The first idea is we can reduce the amount of space we need. So you remember it takes up too much time and too much space. And we start off by reducing the amount of space. And the idea is we reduce mod n each step. So we take 2 and then we multiply it by 2, then we reduce mod n. And so on, and we keep going like this. And for each step, you see the number is going to be not much bigger than n. So this keeps the calculation on a small piece of paper. So instead of requiring the entire universe to write down the answer by reducing mod n each step, we've reduced the amount of space. So now the space is reasonable. Time required is still ridiculous. So anyway, this idea of reducing modulo n each step is a very common idea whenever you're doing calculations modulo a number. For instance, if you wish to calculate a determinant modulo some number, what you don't do is you first calculate the determinant over the integers and then reduce it modulo n. What you do is you reduce modulo n every step and that keeps the numbers reasonably small and makes the calculation a lot quicker. So this idea of reducing mod n each step is a very basic idea in doing numerical calculations on large numbers. Well, we've still got to do something about the time. And there's a neat method of doing this, which the basic idea actually goes back several thousand years to ancient Egypt. I'll explain a little bit about that later. Anyway, the idea is to put n in binary. So we write n is equal to 2 to the n1 plus 2 to the n2 plus 2 to the n3 and so on. And if n is 10 to the 100, there will be a few hundred terms. So it would be a bit of a mess to do by hand, but there's no problem on a computer. And then we notice that 2 to the n is equal to 2 to the n1 times 2 to the n2 times 2 to the n3 and so on. And now, how do we work out these factors? 2 to the n1. What we write? 2 to the 1 is equal to 2. 2 squared is equal to 2 to the 1 times 2 to the 1. And then we reduce mod n. Well, it's a bit stupid reducing mod n at this step because we've only got up to 4. But what I mean is after a few steps, we really do need to reduce mod n. Then we work out 2 to the 4 is congruent to 2 to the 2 times 2 to the 2. And then we find 2 to the 8 is congruent to 2 to the 4 times 2 to the 4 and so on. So we first will make a table of 2 to the power of 2. And you notice that there's going to be a few hundred of these if n is a few hundred digits. And then once we've got all these, we can work out 2 to the n by doing this calculation and again reducing mod n. I should say we reduce mod n each step here, of course. We'll have to keep these numbers reasonable. And you can see that similarly, we can find a to the b mod m for any a, b and m. And this is fast. What I mean is even if a, b and m all have hundreds of digits, we can still do this on a computer in a fairly small amount of time. You can sort of estimate the amount of time this needs. So here the number of times you need to square this is about the logarithm of n up to some constant. And you also need about log of m multiplications here. And each multiplication, if you do it the obvious way, it's going to take about log of n squared steps. So altogether, we're going to take about log of m cubed steps, roughly speaking. If you're really clever, you can use a fast multiplication of integers based on the fast Fourier transform and get this down to about log of m to the 2 plus epsilon steps. But in any case, it's polynomial time in the length of the input. So this is faster compute. And because this is faster compute, computing a to the b mod m is one of the basic operations of computational number theory. So I mentioned that the Egyptians had something rather similar. And this is the ancient Egyptian method for multiplication. It's also sometimes called a sort of Russian peasant method of multiplication or something like that. And the idea is if you want to multiply m and m, what you do is you put m in binary. And you write m is equal to 2 to the a plus 2 to the b plus 2 to the c and so on. And then you keep doubling n. So you form the numbers 2n, 4n, 8n, 16n. And then you can form m times n as 2 to the a times n plus 2 to the b times n plus 2 to the c times n and so on. Of course, the Egyptians didn't quite phrase it like this. I mean, they didn't have quite have an explicit concept of writing numbers in binary. I mean, binary and decimal numbers weren't invented for hundreds of years later. But although they didn't explicitly write m in binary notation, they sort of used this operation. In fact, what they did apparently was they had tables of powers of 2 and they could then convert m into binary by repeatedly subtracting various powers of 2 from m and use that to do multiplication. And now you see this is actually quite similar to our method for working out powers of n except here, this is treating multiplication as repeated addition and instead we were doing exponentiation as repeated multiplication. So the Egyptian method for multiplication is formally more or less the same as the method we gave for doing exponentiation. So it's an example of an algorithm that's several thousand years old that's still in use. So let's do an example of this. So let's try and work out whether 35 is prime. And you may think this is a kind of stupid question. It's sort of obviously prime because it's divisible by 5 but let's sort of pretend we haven't noticed this and try and use this method to test it. So what we do is we write 35 in binary. It's 2 to the 5 plus 2 to the 1 plus 2 to the 0. So 2 to the 35 is equal to 2 to the 32 plus 2 squared plus 2 to the 1. And now we work out the 2 to the 2 to the n. So if 2 to the 1 is 2, 2 squared equals 4 which is congruent to 4 mod 35. So we should reduce mod 35 each step which is kind of stupid at first but it will soon pay off. And then 2 to the 4 is 4 times 4 which is equal to 16 which is congruent to 16 mod 35. And then 2 to the 8 is congruent to 16 times 16 which is 256 which is congruent to 11 mod 35. And you see now it's a really good idea to reduce mod 35 otherwise we would start ending up with ridiculously big numbers. So 2 to the 16 is now congruent to 11 times 11 which turns out to be congruent to 16. And 2 to the 32 is then congruent to 16 times 16 which is congruent to 11. So by some fluke we're sort of going backwards and forwards between 11 and 16. And now we work out 2 to the 35 which is equal to 2 to the 32 times 2 to the 2 times 2 to the 1. So I should have said that's times there. And this is congruent to 11 times 4 times 2 which is congruent to 18 mod 35. And now you notice that 18 is not congruent to 2 so 35 is not prime. So we've managed to show that 35 is not primed by a method that's only a few hundred times more complicated than observing that it's divisible by 5. Well of course for small numbers as I said this method is kind of really silly but it really comes into its own for very very large numbers. Well the other problem we have to deal with is whether what happens, what if 2 to the n is congruent to modulo n. And here we could say that n is, you know, this is some evidence that n is prime. So what do we do? Well we could also check 3 to the n is congruent to, whether 3 to the n is congruent to 3 modulo n. And we can try several other numbers. So this suggests the following question. Can we have a to the n is congruent to a mod n even if n is not prime? This is for all n, sorry for all a. And rather sadly the answer is yes we can but sadly we don't actually get a prime test that always works. So here's an example. We could take n to be 561 which is 3 times 11 times 17 so it's very definitely not prime. And we notice that 561 is congruent to 1 modulo 3 minus 1 and it's also congruent to 1 modulo 11 minus 1 and modulo 17 minus 1. So that's 2, that's 10 and that's 16. So 2, 10 and 16 all divide n minus 1. So this means that a to the 561 is congruent to a to the 1 modulo 3 or 7 or 11. That's because 561 is congruent to 1 modulo 10. So these two are congruent modulo, sorry it's just 17, modulo 11 and similarly because 561 is congruent to 1 modulo 16 that's congruent to that modulo 17 and so on. So a to the 561 is congruent to a modulo 561 because 561 is equal to 3 times 17 times 11 and these are co-prime. So if a number is divisible by 3, 17 and 11 then it's divisible by 561. So this probabilistic test for testing whether numbers are prime or not sometimes fails horribly. Sometimes all these tests will suggest the number is prime and it just isn't. So this number a is called a Carmichael number and Carmichael numbers are numbers with this funny property that a to the n is congruent to a mod n for all a and they're quite rare but they're an infinite number of them so we don't actually get a perfect primality test. The primality test we gave using Fermat's theorem is actually a very crude one. It's the simplest possible thing we could do with Fermat's theorem and there are actually several variations of it that are much less likely to suggest that a number is prime if it isn't really prime. Okay so next lecture we will be covering Euler's generalization of Fermat's theorem to composite numbers.