Processing math: 100%
Skip to main content

Section 4.5 Why modular arithmetic matters

This has been fun and all. But why are we creating so much machinery? There are two reasons.

The first is practical. Simply put, modular arithmetic makes it much easier to solve certain otherwise very difficult problems about integers. The reason is that we can reduce problems about the (infinitely many) integers to checking whether things are possible when we look at the (finitely many) cases modulo n. For instance, we can prove things like these statements without inequalities, calculus, or graphs:

  • “The polynomial x5x+2 has no integer roots”. (See Exercise 9.6.4)

  • “The curve x3=y27 has no lattice points”. (See Fact 15.3.3.)

The second reason for doing modular arithmetic is theoretical. We get a new number system! (See Chapter 8.) It's a number system which has new problems, new solutions, and new things to explore. And that's what we'll be doing from now on.

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 n, 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 n.

Example 4.5.1.

For example, to add [22]+[21] modulo 23 it might be smarter to use the residues 1[22] and 2[21]. The answer [3] is of course the same as [22+21]=[43]=[20] modulo 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).

Here is a more involved example, which avoids the big numbers which would otherwise occur to do a computation without assistance.

Example 4.5.3.

Let's calculate 420 (mod 6). First we will use least nonnegative residues; write the justification for each step in the margin if you have a print copy!

420(42)101610410(42)516545(42)24424444

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 42 (mod 6):

420(2)20((2)2)10410(2)10((2)2)545(2)5
((2)2)2(2)42(2)(2)2(2)4(2)(2)(2)4.

There are a few things to be aware of when doing this, of course. To remind you of a very important such caveat, recall Example 4.4.5; with exponentiation, you can only replace the base with something in the same congruence class. Using the language of Proposition 4.3.2, we say that [a]n is well-defined, but there is no guarantee that a[n] makes any sense.

Example 4.5.4.

Just to make sure you get this, on your own compare

23 (mod 5),73 (mod 5), and 28 (mod 5),78 (mod 5).

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 (modulo n). We first need the following interesting fact.

What does \(a^{2^n}\) even mean? By definition,

\begin{equation*} 2^n=2^{n-1}\cdot 2 = 2^{n-1}+2^{n-1}\text{,} \end{equation*}

so \(a^{2^n}\) is the same as

\begin{equation*} a^{2^{n-1}+2^{n-1}}=a^{2^{n-1}}\cdot a^{2^{n-1}}=\left(a^{2^{n-1}}\right)^2 \end{equation*}
Example 4.5.6.

In this case, it will be easier to do examples before stating the algorithm. To compute x20, first we see that 16 is the highest power of 2 less than 20.

  • Compute x2 modulo n.

  • Square that for (x2)2=x22=x4 (modulo n).

  • Then square twice more for x23=x8 and x24=x16; we reduce modulo n at each point.

Now write x20 as x to a sum of powers of 2;

x20=x16+4=x24+22=x22x24

Then do this final multiplication modulo n as well. You might want to try it to see you get the same thing.

Example 4.5.7.

Now let's get really explicit, and calculate 223 (mod 11). First,

23=24+22+2+1, so 223=224222222.

Then get the powers of 2 needed:

224 (mod 11), (22)2=425 (mod 11), 
(24)2=523 (mod 11), and (28)2=329 (mod 11)

So we get, as a computation one can do completely without a calculator,

22422222295421820796338 (mod 11)
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 2log2(n) operations, as opposed to normal powers which might require n operations; in addition, you only deal with numbers at most size n2, as opposed to gigantic ones, when you mod out after each step, so it requires very little memory.