 One of the most powerful tools in number theory and in cryptography is something known as the Chinese remainder theorem algorithm, and this is based on an ancient Chinese not-so-secret process that first appeared in the work of Sun Xie in the 5th century, where he posed the following problem, an unknown number of objects, when counted by three, two remain, when counted by fives, three remain, and when counted by sevens, two remain. How many objects? So this is equivalent to solving the system of congruences. Counting by three and leaving two says that however many objects we have is going to be congruent to two mod three. Similarly, counting by fives and leaving three is the same as saying our number is congruent to three mod five, and counting by sevens and leaving two is the same as saying that our number is congruent to two mod seven. The solution is a bit hard to follow, and so to solve this, we'll use the following approach. First, we'll do something that we often do whenever we're facing a hard problem, we'll ignore most of the problem. Suppose we find x equals a as a solution to x congruent to two mod three. Then a plus three k will be congruent to two mod three, and so a plus three k is still a solution to x congruent to two mod three. Now we can start to bring in the parts of the problem that we ignored, starting with this x congruent to three mod five. Since a plus three k is a solution to x congruent to two mod three, we'll find k, where a plus three k is also a solution to three mod five, and once we have that, we'll lather, rinse, repeat. So first we'll solve x congruent to two mod three, and after staring at this very complicated equation, we realize that x equals two is a solution, and so all solutions will be x equal to two plus three k. So now I want this to be a solution to three mod five. So substituting two plus three k into my congruence, I have two plus three k congruent to three mod five, and so that means that two plus three k is three more than a multiple of five, three plus five y. I'm going to solve this for k, and so we have, but since k is supposed to be an integer, we require two y plus one over three b sum integer, we'll call it z, and we find. But because y is supposed to be an integer, we require that z minus one over two b sum integer, we'll call that w, and we find, and so this gives us a solution if w equals zero, then k equals two, and x is going to be two plus three times two, otherwise known as eight. Now for fantastically lucky, x equal to eight will solve this third congruence. But we're not. Since eight does not solve x congruent to two mod seven, we can add multiples of three times five, fifteen, and that will preserve our solutions. Eight plus fifteen k is still congruent to two mod three, and eight plus fifteen k is still congruent to three mod five. But our hope is that eight plus fifteen k will be congruent to two mod seven. And now we can try to solve this. Since we're working mod seven, remember working mod n means you never have to work with numbers larger than n. We can actually reduce some of our coefficients. One plus k congruent to two mod seven means that one plus k is two more than a multiple of seven, and we can solve this. And here as long as y is an integer, k will also be an integer. And so we can pick any integer value for y that we want, how about zero, in which case we find k is. And so the solution we need, eight plus fifteen times one, that's twenty-three, and this solves our original congruence. A useful thing to keep in mind is that since our moduli are three, five, and seven, if we add a multiple of three times five times seven, we don't change any of the congruences. And so we note that twenty-three plus three times five times seven k also solves the congruence. So let's try a more complicated problem. We want to find the least positive solution to x congruent to fifteen mod sixteen, twenty-seven mod thirty-one, and twenty-three mod twenty-five. Now since we can reduce the numbers mod n, if we start with the largest modulus, we can simplify as we go. So we want to find a solution to x congruent to twenty-seven mod thirty-one, and so we find a solution is twenty-seven. And if we add a multiple of thirty-one, it'll still be congruent to twenty-seven mod thirty-one. So our second largest modulus is twenty-five, so we know that twenty-seven plus thirty-one y satisfies this first congruence. So we also want to satisfy the second congruence. And so we want twenty-seven plus thirty-one y to be congruent to twenty-three mod twenty-five. We can reduce our coefficients and then solve. And if we pick w equals four, we'll get positive solutions. So we'll let w equals four, and so we find y equals sixteen, and so x equals five twenty-three solves both of these congruences. And now we'll try and bring in that third congruence. So we know that we can add a multiple of thirty-one and twenty-five and not lose our solution. So we want five twenty-three plus a multiple of thirty-one times twenty-five to be congruent to fifteen mod sixteen. And because we're working mod sixteen, we can reduce. And then that says that eleven plus seven y is fifteen more than a multiple of sixteen, so we can solve. And so we can find a solution. One solution is t equals zero and y equals twenty-eight, which gives us a solution of twenty-two to twenty-three. And remember, we can add or subtract a multiple of the product of all the moduli, thirty-one times twenty-five times sixteen, and so we can reduce to find the least positive solution. Twenty-two to twenty-three is congruent to ninety-eight twenty-three mod the product thirty-one times twenty-five times sixteen. And so ninety-eight twenty-three is the least positive solution.