It would be good to know something that reassures us that there will be a solution in some family of cases, and here’s a result, named after its discovery by the ancient Chinese:
[Chinese Remainder Theorem] Let and be coprime, and let and be any integers. The simultaneous congruences have a solution modulo .
Proving the Chinese Remainder Theorem
Suppose given and coprime.
We’ll solve two of the easiest imaginable pairs of simultaneous congruences first, and then we’ll observe that, in fact, that’s enough work to do anything.
The first easy pair of simultaneous congruences is The first equation says that for some , and the second says that is a multiple of . In other words, we have , so But and are coprime, so we know we can solve this.
Another easy pair of simultaneous congruences are This looks exactly the same, but the other way around: the second says that is of the form for some , and the first says that is a multiple of . In other words, we need We know we can do this.
In fact, instead of going through the method twice, the same process does both these pairs of congruences: if we use Euclid’s algorithm to give a solution to in fact taking and gives us what we want:
What then of our original equations I claim that if we take , we have what we need.
Indeed, since and , we have while, since and , we have Both of those are exactly what we needed.
This gives us a new way of finding solutions, which I’ll show off:
What are the solutions to:
We’ll use our “building blocks” from the proof of the Chinese Remainder Theorem. In order to find this, we discovered we needed to invert mod : we need to solve This has a solution .
As a result is congruent to mod and modulo , and is congruent to mod and modulo .
Hence our solution is
The bit in the statement which says that the moduli have to be coprime is definitely important!
Consider the following: These have a solution, but it’s , and not modulo 25.
On the other hand, these don’t have any solution, modulo anything.
Similarly, you can check that have a solution, which is .
We won’t prove it, but the rules are this:
You can check whether two congruences with moduli and agree by looking what they say modulo (for example, the two above agree, because they both say );
If two congruences agree, they have a common solution modulo .
More calculations modulo primes
Earlier, we pointed out that modular arithmetic modulo primes is very well-behaved: every nonzero residue is invertible.
We’ll going to go on and use that.
The first thing we’ll talk about is exponentiation in modular arithmetic.
In integer arithmetic, it’s usually stupid to try to calculate very large powers: for example, has a huge number of digits ( of them, to be precise).
But, in modular arithmetic there are no large numbers. For example will be congruent to something between and modulo , and it’s reasonable to ask what.
One very stupid way of working it out would be to do the multiplication in the integers, then divide by and find the remainder.
We can do better, by doing our arithmetic all modulo in the first place. So: That’s still going to be a lot of multiplication, if we keep multiplying by (modulo ) more than a thousand times!
There are considerably more intelligent ways. For example, we can square modulo quite quickly.
That lets us do some exponents by repeated squaring. For example,
1234 isn’t quite as nice, but we can do and end up getting the answer.
We’ll end up only multiplying about twenty times if we do it this way: that’s much less!
But, in fact, there’s a method that’s even faster still for this situation. We’ve just computed that That does almost all the work for us, as Hence That makes the whole thing easy.
The relevant observation here was really that there was some integer such that . So two obvious questions are:
When does there exist such an ?
When it does exist, can we compute it?
Our answer to the first is not too difficult: