Section 4.5 Why modular arithmetic matters
“The polynomial
has no integer roots”. (See Exercise 9.6.4)“The curve
has no lattice points”. (See Fact 15.3.3.)
Subsection 4.5.1 Starting to see further
In order to accomplish all these goals, we will take some time learning how to do such computations. Here are two generic practical rules for using modular arithmetic.First off, always first reduce modulo
then do your arithmetic (add, multiply, exponentiate). We have seen lots of examples of this.Secondly, always use the most convenient residue (recall Definition 4.4.6) of a number modulo
Example 4.5.1.
For example, to add
xxxxxxxxxx
mod(22,23)+mod(21,23)==mod(-3,23)
Sage note 4.5.2. Checking equality.
Use the double equals sign ==
to check if two numerical expressions are equal, not just =
(which assigns a value to a variable).
Example 4.5.3.
Let's calculate
On the other hand, we can avoid using numbers of absolute value bigger than five if we carefully select least absolute residues where appropriate! Recall that
Example 4.5.4.
Just to make sure you get this, on your own compare
The second pair is quite different from the first pair.
Subsection 4.5.2 Taking powers
As one example of how modular arithmetic might matter a bit, let's examine the following algorithm for taking ridiculously high powers of numbers (moduloFact 4.5.5.
For any integer
In general,
That is to say, each “power of
Proof.
What does \(a^{2^n}\) even mean? By definition,
so \(a^{2^n}\) is the same as
Example 4.5.6.
In this case, it will be easier to do examples before stating the algorithm. To compute
Compute
moduloSquare that for
(modulo ).Then square twice more for
and we reduce modulo at each point.
Now write
Then do this final multiplication modulo
Example 4.5.7.
Now let's get really explicit, and calculate
Then get the powers of
So we get, as a computation one can do completely without a calculator,
xxxxxxxxxx
mod(2,11)^23
Algorithm 4.5.8.
In general, we can compute
Write the exponent
where each or (This is called the binary representation of )Compute
and so forth as above, each time reducing moduloMultiply
together as in the examples above. Obviously, if (such as for in the example) you skip it, as it just contributes one to the product.
Remark 4.5.9.
Those interested in efficiency should note that this requires roughly two times the number of binary digits of your number operations, or about