Division
Intuitively, to divide by means to find a number such that times is , but we had trouble adopting this defintion of division because sometimes there is more than one possibility for modulo .
We solved this by only defining division when the answer is unique. We stated without proof that when division defined in this way, one can divide by if and only if , the inverse of exists.
We shall now show why this is the case. We wish to find all such that , which by definition means
for some integer . But this is precisely the problem we encountered when discussing Euclid's algorithm! Let .
Suppose . Then no solutions exist if is not a multiple of . Otherwise the solutions for are
for some integers (that we get from Euclid's algorithm) and for all integers . But this means does not have a unique solution modulo since . (Instead has a unique solution modulo .)
On the other hand, if , that is if and are coprime, then is always a multiple of so solutions exist. Recall we find them by using Euclid's algorithm to find such that
Then the solutions for are given by
for all integers . Thus has a unique solution modulo , and division makes sense for this case.
Also, satisfies so in fact . Thus our claims are correct: we can divide by if and only if has an inverse.
We can also see that has an inverse if and only if . (Actually, we have only proved some of these statements in one direction, but the other direction is easy.)
Note: even though we cannot always divide by modulo , sometimes we still need to find all such that .
Computing Inverses
We previously asked: given , does exist, and if so, what is it?
Our answer before was that since is finite, we can try every possibility. But if is large, say a 256-bit number, this cannot be done even if we use the fastest computers available today.
A better way is to use what we just proved: exists if and only if (which we can check using Euclid's algorithm), and can be computed efficiently using the extended Euclidean algorithm.
Example: does exist, and if so, what is it? Euclid's algorithm gives
Thus an inverse exists since . To find the inverse we rearrange these equations so that the remainders are the subjects. Then starting from the third equation, and substituting in the second one gives
Now substituting in the first equation gives
from which we see that .