 This lecture is part of Berkeley Math 115, an introductory undergraduate lecture on number theory, and will be mostly about the Chinese remainder theorem. So this is about solutions of congruences. So I'll just quickly review solutions of linear congruences. So I suppose you want to solve a congruence AX is congruent to B modulo M, where A, B and M are given. So we saw in the discussion on Euclid's algorithm that this is solvable if and only if B is divisible by the greatest common divisor of A and M. So this is obviously necessary and Euclid's algorithm shows that it's sufficient. So let's just do a quick example. Suppose you want to solve 6X is congruent to 7 mod 45. And this is no solutions because if we look carefully at this, we see that this 45 is divisible by 3 and this 6 is divisible by 3. So if there were a solution, then 7 would also be divisible by 3. So this particular case is no solutions. Now let's try 6X is congruent to 3 mod 45. Well, here you see now that this number is divisible by 3. So there should be a solution. How do we find the solution? Well, we just write 6X equals 3 plus 45Y. And we can take out the factor of 3 just to make things simple. It's not really necessary, but we get 2X equals 1 plus 15Y. And now we can solve by Euclid. So I'm not going to write out in detail and you can just find X is congruent to 8 mod 15. Of course, with an example as small as this, it's faster just to do by trial and error rather than by using Euclid. Now, one thing you've got to be a little bit careful of is you can ask how many solutions there are. And you may say, well, 2X equals 1 mod 15 only has one solution mod 15. However, in the original equation, notice that this is three solutions, mod 45. The point is if you've got one solution X, then you can just add 15 to it and we still get a solution. So we've got 8, 8 plus 15 and 8 plus 30 as three different solutions. So this is something you've got to be a little bit wary of. So if A and M are equal to 1, so if the highest greatest common divisor of A and M is equal to 1, then this means the solution is unique and always exists. If A and M is greater than 1, the solution might not exist or there can be many solutions. So that's something you've always got to be a little bit careful of. So more generally, we would like to solve the following problems. So as you want to solve f of X is common to zero modulo M where we might take F to be a polynomial in X. For instance, we might want to solve X cubed is congruent to 5 modulo 60. And the idea of doing this is to split it into several cases. So the first case is M is prime and this tends to be sometimes a bit easier. This is because if A M equals 1, then A has an inverse. And since M is prime, this means that either A has an inverse or A is congruent to zero. And this makes things a lot easier because most elements have inverses. So the second case is M is equal to P to the N is a prime power. And what we will see is that this can be sort of reduced to the case M equals P. And we will be discussing this a bit later in a later lecture. So the third case is M is arbitrary. Well, here we can write M is equal to P1 to the N1, P2 to the N2, P3 to the N3. And for this, we're going to reduce to the case M equals P to the N using the Chinese remainder theorem that we're discussing in this lecture. So there are three steps to solving fx equals zero mod M. You do the general case reduced to prime power, prime power reduced to prime, and then you solve a case of prime. And this lecture, we're going to be looking at the reduction of case three to case two. And later lectures, we'll be studying these two problems. So what we need to do is to be able to solve two different congruences. So let's look at the following problem. Solve, we want to solve x is congruent to A1 modulo M1. Well, that's not very difficult, but we also want to solve x is congruent to A2 modulo M2. And we want to find the same x solving these two equations. So can we always find a solution? Well, let's try x is congruent to 1 mod 6, x is congruent to modulo 10. Try and find a solution. Well, that's not going to be very easy because this equation here implies x is even and this equation here implies x is odd. So there are no solutions. So in general, if we've got two linear equations with different numbers M1 and M2, then we can't generally solve it. And you see the problem here is the fact that these two numbers have a common factor of two. So this one can imply that x is something mod 2 and this one can imply that x is something completely different modulo 2. And this suggests we should be able to solve these if M1 and M2 are co-prime. So let's just check this. So we want to solve x is equal to A1 plus M1y1 equals A2 plus M2y2. And now all we have to do is to solve this equation here and that gives us x. And if we look at this equation, we can see it just says M1y1 minus M2y2 is equal to A2 minus A1. And now this is just a linear equation for two variables that has a solution if M1 and M2 are co-prime. So if we want to solve two linear equations in some unknown, we can do it provided all the moduli we're working with a co-prime to each other. If they have a common factor, maybe we can solve it and maybe we can't. And if we can solve it, there might be several solutions and so on. We also notice that this solution is going to, if M1 and M2 are co-prime, the solution is unique modulo M1 M2 if M1 M2 are co-prime. And that's because we're saying x is co-prime to A1 modulo M1 and x is co-prime to A2 modulo M2. And if you've got two different x's, the difference of two solutions satisfies, let's write y for the difference, y is co-prime to 0 modulo M1, y is co-prime to 0 modulo M2. And these two imply y is co-prime to 0 modulo M1 M2 because M1 and M2 are co-prime. So as long as M1 and M2 are co-prime, there's a unique solution modulo M1 M2 of these two equations here. So let's just do an example. Suppose we want to solve the following two equations. x1 is co-prime to 1 mod 17 and x is co-prime to 3 modulo 21. All we do is we write out x is equal to 1 plus 17 y is equal to 3 plus 21 z. So we've got the linear equation minus 21 z plus 17 y is equal to 2. So we use Euclid to solve minus 21 z plus 17 y is equal to 1. And this has the solution z equals 4, y equals 5. So we just double it. And this has the solution z equals 8, y equals 10. And this gives us, if we just substitute in here, we find that x is equal to 171. So solving two linear equations with co-prime modulus is easy and fast. What happens if we've got three equations? So we've got x is congruent to a1 modulo M1, x is congruent to a2 modulo M2 and x is congruent to a3 modulo M3. So what do we do? Well, obviously we would like M1, M2, M3, a pairwise co-prime. What this means is that M1, M2 are co-prime. M2, M3 are co-prime. And M3, M1 are co-prime. Notice that this is stronger than saying that M1, M2, M3 have no common factors, because, for example, if we take M1 is equal to 6, M2 is equal to 15, and M3 equals 10, then these three numbers, M1, M2 and M3, all three of them, there's no common factor of all three, any two of them have a common factor. So you mustn't confuse being pairwise co-prime with all of them being co-prime. So this isn't the condition we want. We want this stronger condition. And what we can do is we first solve the first two equations and then we get the solution x is congruent to something or other, modulo M1, M2. And we've also got the equation x is congruent to A3, modulo M3. And now we've got another two equations and now M1, M2 are co-prime to M3, so we can solve these and get x is congruent to something, modulo M1, M2, M3. So we can solve any number of linear equations with, provided all the moduli are pairwise co-prime just by repeating the case for two elements. And we can give an example. This is where the name Chinese remained a theme. There's a Chinese mathematician and I'm not going to try and write his name in Chinese or to pronounce his name in Chinese. So he was apparently in about the third century and he had the following problem. He said there are certain things, there are things whose number is unknown. And if we count by threes, there are two left over. He said if we count by fives, we have three left over. And if we count by sevens, there are two left over. And he had to write everything out in words because algebra was in a rather primitive state at the time. But what he's obviously saying is we're trying to solve the following three equations, x is congruent to two, modulo three, x is congruent to three, modulo five, and x is congruent to two, modulo seven. So what we do is we first solve these two equations and three and five are co-prime. So that's easy. And we're just trying to solve two plus three y, x is equal to two plus three y equals three plus five z. And here we've got something we can solve by either by Euclid's algorithm or by guessing. And we can, for example, take y equals two, z equals one. And in small examples like this, it's much faster just to guess the solution than to try and remember what Euclid's algorithm is. So we find x is equal to eight would be a solution for the first two. So these two now become x is congruent to eight, modulo 15. And you notice this 15 comes from the product of three and five. And of course you can't fix x to be eight because this won't satisfy the first equation. You've got to allow x to have multiples of 15 added to it. Now we take these two equations and solve them. So we now want to solve x equals two plus seven y, that's nothing to do with the y over here, equals eight plus 15 z. And again, if you look here, we've got a linear equation in one unknown and y equals minus six z equals minus 12 would be a solution if I've got this right. So we get x equals minus 82. And now we can add multiples of three times five times seven to it. So we can add 105 times anything and we can find a positive solution x equals 23. So you can see x equals 23 is now two mod three, three mod five, and it's two mod seven. I can give an alternative, more abstract proof of the Chinese remainder theorem. So this is a sort of alternative proof. So what we're trying to do is show that we can solve x is congruent to a1 modulo m1 and x is congruent to a2 modulo m2. And we want a solution modulo m1, m2, whenever m1 and m2 are co-prime. And instead of using Euclid's algorithm, we can just count. So what we do is write down all the numbers one, zero, one, two, up to m1, m2 minus one here. And there are m1, m2 of these. And we write out all the numbers from zero up to m1 minus one here. And there are obviously m1 of these. And we write out all the numbers zero up to m2 minus one. And of course there are m2 of these. And now we have a map from this set here to the product of these two sets. The product of this set is m1, m2 elements and this set is m1, m2 elements. And if we take a number x here, we can just map it to x modulo m1, x modulo m2. So we've got a map from two sets of the same size. And let's try to think about whether this is a bijection. First of all, we know that it's an injection. Remember, this means that if x and y have the same image, this implies that x equals y. So let's just check this. So x maps to x mod m1 and x mod m2 and y maps to y modulo m1 and y modulo m2. And now we're saying these are the same image. So x modulo m1 is y modulo m1 and x modulo m2 is y modulo m2. So x minus y is divisible by m1 and m2. And since it's divisible by m1 and m2, it's also divisible by their product because m1 and m2 are co-prime. So x minus y is divisible by m1 and m2. So x must be equal to y because we're taking them both to be in this set of coset representatives. Now, if we've got a map between two sets of the same size that's injective, then it must also be surjective. In other words, for every element here, there must be an element here mapping to it. This shows that given any elements a1, m and a2, so a1 would be in here and a2 is in here, we can find an element here mapping to both of them. So that's a sort of abstract existence argument for the Chinese remainder theorem. So let's just write this right out as an example. Let's take m1 equals 3 and m2 equals 5. So what we have, we have the numbers from 0 to m1, m2 minus 1. So we get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14. So this is modulo m1, m2. And now we can have the modulo m1 which is 3 and we get 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2. Now we write them out as modulo m2 which is 5 and we get 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4. And now you can see we get a bijection between these 15 numbers and these 15 pairs. For instance, you can see from this table that 1 mod 3 and 2 mod 5 corresponds to 7. And you can see that all these 15 pairs are different because that follows because 3 and 5 are co-prime. And since there are 15 pairs and 15 numbers here, we have to get a 1 to 1 correspondence between pairs and numbers modulo 15. If we do this with two numbers that aren't co-prime, so let's try m1 equals 4 and m2 equals 6, say. So we would have the numbers 0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13. I'm getting bored. Let's just go up to 23. And now we write them out modulo 4, so we get 0, 1, 2, 3, 0, 1, 2, 3, and so on. And again, I'm getting bored, so I won't do them all the way. And modulo 6, we get 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, and so on. Now you notice that this map is neither injective nor surjective because if you go up to the number 12 here, we get 0, 0. So the problem is we've got two different numbers modulo 24 that correspond to the same pair. So the number 0 and 12 are the same image, so the map is not injective. Similarly, the map isn't surjective because you notice that every pair we get here, so if we pick any pair here, you notice the elements are both even or both odd. So we never get the pair 0, 1, for example. And the problem here is caused by the fact that M1, M2 are not co-prime, obviously the highest common factor 2. So if M1 and M2 are co-prime, we get a nice bijection between numbers modulo M1, M2, and numbers modulo M1 times numbers modulo M2. If M1 and M2 are not co-prime, then this map is neither injective nor surjective and we don't get a Chinese remainder theorem. So let's look at another example. Let's look at the equation x squared is common to x modulo 10 to the n, where I'm going to take some high power of 10, maybe a thousand or a million or something. So what this says is we want to find numbers that have the same last digits if you square them. For instance, 6 squared is equal to 36 and now you notice the last digit is the same. So that would be modulo 10 to the power of 1. We can also look at, say, 76 squared. Well, this is equal to 5776 and now you notice that it is two digits that are the same at the end. And we can go on like this. We can take a number like 1787109376 and if you square it, this will be something or other ending with 1787109376. So that would be modulo 10 to the 1, 2, 3, 4, 5, modulo 10 to the 10. And these aren't the only solutions. There's a trivial solution. 0 squared is congruent to 0, modulo 10 to the n and 1 squared is congruent to 1, modulo 10 to the n. These are the kind of boring solutions. There are still more solutions because we can look at 5 squared equals 25 and 25 squared equals 625. And if you look 625 squared, that's going to be something rather ending in 625. We can go on like this. We get 8212890625 squared is equal to something rather 8212890625 and so on. So we really can get some slightly odd solutions, very large powers of n. And now we want to understand why this is true. So we want to solve x squared is congruent to x, modulo 10 to the n. Well, what we do is we notice that 10 is not prime, it's 2 times 5. So this is the same as solving the two equations, x squared is congruent to x, modulo 5 to the n and x squared is congruent to x, modulo 2 to the n, because of course 5 to the n and 2 to the n are co-prime. So if we can solve these two equations, then by the Chinese remainder theorem, we can put the solutions together and solve this equation. And now let's solve these. x squared is congruent to x, modulo 5 to the n. Well, that's easy to solve. It has two solutions, x equals 0 and x equals 1. May ask, does it have other solutions? Well, maybe. But it has at least two solutions. What about x squared is congruent to x, modulo 2 to the n? Well, this has two obvious solutions, x is congruent to 0, x is congruent to 1. It may also actually have other solutions, but we don't really... Sorry, that should be a 1. We don't really worry about these. The point is we've got two solutions, this modulo x to the 5 and two solutions, modulo 2 to the n, or at least two solutions. And now we can find solutions by pairing these off. For instance, if we take these two solutions, we get the solution x is congruent to 0, modulo 10 to the n, which is not terribly exciting. And if we take these two solutions, we can get x is congruent to 1, modulo 10 to the n, which solves this but isn't very exciting. But now we can do something a little bit cleverer. What we can do is we can take the cross solutions. So if we take x equals 0, modulo 2 to the n and x is congruent to 1, modulo 5 to the n, then we get some other solutions. For example, this is now going to give us the solutions x equals 6, which is 1, modulo 5 and 0, modulo 2. Or we could take x equals 36. So x is congruent to 1 or 0, modulo... So here it's 1, modulo 5. And here it's 0, modulo 2. Here it's 0, modulo 2 squared. And it's still 1, modulo 5 squared. No, it isn't. That should be at 76. So it's 1, modulo 5 squared and so on. So if we take the next one along, which is 376, this is going to be 1, modulo 5 cubed and it's going to be 0, modulo 2 cubed. And so you can keep going like this and you can see you can keep on extending this number as long as you like because of the Chinese remainder theorem. We can also get the other solution by taking the other cross ratio. So we can take x congruent to 0, modulo 5 to the n and x is 1, modulo 2 to the n. And that gives us another set of solutions which look like 5, 25, 6, 25 and so on. There's actually another way of producing these particular solutions. We can just keep squaring. So 5 squared is equal to 25. 25 squared is equal to 625. 625 squared will be equal to something where we only care about the last few digits so it will be 0, 625 with some digits before that. And the reason why this works is suppose we've got a number x is congruent to 0, modulo 5 to the n and x is congruent to 1, modulo 2 to the n. Well then we notice that x squared is congruent to 0, modulo 5 to the n plus 1. In fact, modulo 5 to the 2n we only need 5 to the n plus 1. And x squared is congruent to... Well, what's it congruent to 1, modulo 2 to the n plus 1? Well, it's actually congruent to 1, modulo 2 to the n plus 1. And we see that because x is equal to 1 plus 2 to the n y. So x squared is equal to 1 plus 2 times 2 to the n y plus 2 to the n y squared. And you can see this is divisible by 2 to the n plus 1. So if we've got any solution of x is congruent to 0, mod 5 to the n and x is congruent to 1, mod 2 to the n then squaring it we get a solution of this equation with one extra digit. So every time we take one of these numbers and square it we get one extra digit to the solution of x squared is congruent to x, modulo 10 to the n. So if we start with 5 we can just produce arbitrary long solutions by repeated squaring. You may say, why don't we start with 6 and repeatedly square it? Well, that doesn't work because if we start with 6 and square it we get 6, 36, and 36 doesn't work. That's the number I was getting confused by down here. So I'll just leave it as a question. Why does repeated squaring work if you start with 5 but not if you start with 6? So let's just finish off by asking how many solutions are there, modulo m. So we want to solve f of x is congruent to 0 modulo m for some polynomial. And we've seen we can reduce to prime powers using the Chinese remainder theorem. And now we want to know how many solutions are there. Well, the Chinese remainder theorem tells us this as well because the number of solutions modulo mn for mn p prime is just the number of solutions modulo m times the number of solutions mod n. This is just essentially a restatement of the Chinese remainder theorem because every time we've got a solution modulo m and a solution modulo n, the Chinese remainder theorem says we get a solution modulo mn. So for example, let's try and work out how many solutions to x squared is congruent to 1 modulo 680. Well, what we do is we write 680 as a product of primes. So we see it's got a factor of 5 and a factor of 2 and then we've got 68, which is 3417. So we've got 2 cubed times 5 times 17. So what we've got to do is we've got to solve three equations. x squared is congruent to 1 modulo 8 and x squared is congruent to 1 modulo 5 and x squared is congruent to 1 modulo 17. So let's work out the solutions. Well, x squared is congruent to 1 modulo 8. Remember, this is this funny one that has more solutions than you expect. So it's a quadratic equation with four solutions. So the solutions are 1, 3, 5, and 7. And here, x squared is 1 mod 5. Well, that's pretty trivial. We just have 1 and minus 1. And x squared is 1 mod 17. Sorry. So x squared equals 1 mod 17 has, again, there's two solutions, 1 and minus 1. So we find there are four solutions, mod 8, and there are two solutions, modulo 5 and two solutions, mod 17. So altogether, there are 16 square roots of 1 modulo 680. OK, that's all about the Chinese remainder theorem. The next lecture will be about Euler's phi function or Euler's totian function.