Inverse
The next operation we want to discuss is the inverse operation. The inverse of addition is subtraction, which can be done easily. But the concept of multiplicative inverse in modular arithmetic is less clear (for real numbers, the inverse of is simply ). Being able to compute an inverse is important in many algorithmic applications, and we will cover the theories behind it.
We say that is a multiplicative inverse of modulo if . Does an inverse always exist? Let us try some simple examples.
When , every nonzero number has an inverse. Inverses of are respectively. When , notice that and do not have an inverse, while and do (inverses are and respectively).
Let us start with a simple observation, which is easy to prove.
If has a multiplicative inverse modulo , then .
So we know that is necessary for the existence of an inverse. It turns out that this is also sufficient.
If , then has a unique multiplicative inverse modulo .
Consider multiplying by every number in modulo (i.e., ). So, we get . We claim that these numbers are all distinct under modulo . Assume for contradiction that for . This would imply that or for some . Since and do not share any common divisor (greater than ), we know that , a contradiction since ranges between and .
The uniqueness of inverses allows us to write things like . If we did not know uniqueness, such notation would not make sense.
Now we present an algorithm that computes the multiplicative inverse of modulo . Before going into an abstract setting, let us look at a simple example on how to derive an inverse. Let us try to compute the inverse of modulo . Let be an inverse (we know it exists). We set up two equations that we know:
By subtracting twice the second equation from the first, we have:
By subtracting this one from the above,
At this point, let us warn the students that such derivations (by manipulating the equations) in general may not necessarily find a solution. The derivation gives us a necessary condition that, if an inverse exists, it must be equal to under modulo . This fact, in combination with the existence of the inverse (guaranteed by Lemma 10), implies that is an inverse and the only one (modulo ). The lesson here is that such derivations alone do not always find all solutions we are looking for: It is the combination between mathematical derivation and the existential theorem that allows us to make a conclusion.
Now, let us turn the above example into a more general algorithm, which finds , provided this exists.
- .
- while do
- , , and
- return
Notice the simplicity of the above algorithm and its similarity to the Euclid's algorithm for computing the greatest common divisor. Its correctness follows from a simple loop invariant:
Let be the multiplicative inverse . The following is loop invariant:
Notice that this loop invariant implies the correctness of our algorithm. First it holds before the algorithm enters the loop since we have (here we use the existence of the inverse) and . When the algorithm terminates, we have that and , so by the loop invariant, as desired.
Assume that the statement holds before entering the loop. Let be the values of after the end of the loop. The loop made the update so that and , so we have that . Notice that . By multiplying by and subtracting it from , we have:
In other words, , concluding the proof.
Prove that INVERSE can be implemented in time, when and are -digit numbers.