# Modular arithmetic/Intermediate

Given integers , , and , with , we say that is *congruent to* *modulo* , or (mod ), if the difference is divisible by .

For a given positive integer , the relation (mod ) is an equivalence relation on the set of integers. This relation gives rise to an algebraic structure called **the integers modulo ** (usually known as "the integers mod ," or for short). This structure gives us a useful tool for solving a wide range of number-theoretic problems, including finding solutions to Diophantine equations, testing whether certain large numbers are prime, and even some problems in cryptology.

## Contents

## Arithmetic Modulo n

### Useful Facts

Consider four integers and a positive integer such that and . In modular arithmetic, the following identities hold:

- Addition: .
- Subtraction: .
- Multiplication: .
- Division: , where is a positive integer that divides and .
- Exponentiation: where is a positive integer.

For examples, see Introduction to modular arithmetic.

### The Integers Modulo n

The relation (mod ) allows us to divide the set of integers into sets of equivalent elements. For example, if , then the integers are divided into the following sets:

Notice that if we pick two numbers and from the same set, then and differ by a multiple of , and therefore (mod ).

We sometimes refer to one of the sets above by choosing an element from the set, and putting a bar over it. For example, the symbol refers to the set containing ; that is, the set of all integer multiples of . The symbol refers to the second set listed above, and the third. The symbol refers to the same set as , and so on.

Instead of thinking of the objects , , and as sets, we can treat them as algebraic objects -- like numbers -- with their own operations of addition and multiplication. Together, these objects form **the integers modulo ,** or . More generally, if is a positive integer, then we can define

,

where for each , is defined by

### Addition, Subtraction, and Multiplication Mod n

We define addition, subtraction, and multiplication in according to the following rules:

for all . (Addition)

for all . (Subtraction)

for all . (Multiplication)

So for example, if , then we have

Notice that, in each case, we reduce to an answer of the form , where . We do this for two reasons: to keep possible future calculations as manageable as possible, and to emphasize the point that each expression takes one of only seven (or in general, ) possible values. (Some people find it useful to reduce an answer such as to , which is negative but has a smaller absolute value.)

#### A Word of Caution

Because of the way we define operations in , it is important to check that these operations are well-defined. This is because each of the sets that make up contains many different numbers, and therefore has many different names. For example, observe that in , we have and . It is reasonable to expect that if we perform the addition , we should get the same answer as if we compute , since we are simply using different names for the same objects. Indeed, the first addition yields the sum , which is the same as the result of the second addition.

The "Useful Facts" above are the key to understanding why our operations yield the same results even when we use different names for the same sets. The task of checking that an operation or function is well-defined, is one of the most important basic techniques in abstract algebra.

## Algebraic Properties of the Integers Mod n

The integers modulo form an algebraic structure called a ring -- a structure in which we can add, subtract, and multiply elements.

Anyone who has taken a high school algebra class is familiar with several examples of rings, including the ring of integers, the ring of rational numbers, and the ring of real numbers. The ring has some algebraic features that make it quite different from the more familiar rings listed above.

First of all, notice that if we choose a nonzero element of , and add copies of this element, we get

,

since is a multiple of . So it is possible to add several copies of a nonzero element of and get zero. This phenomenon, which is called **torsion**, does not occur in the reals, the rationals, or the integers.

Another curious feature of is that a polynomial over can have a number of roots greater than its degree. Consider, for example, the polynomial congruence

.

We might be tempted to solve this congruence by factoring the expression on the left:

.

Indeed, this factorization yields two solutions to the congruence: , and . (Note that two values of that are congruent modulo are considered the same solution.)

However, since , the original congruence is equivalent to

.

This time, factoring the expression on the left yields

.

And we find that there are two more solutions! The values and both solve the congruence. So our congruence has at least four solutions -- two more than we might expect based on the degree of the polynomial.

Why do the "rules" of algebra that work so well for the real numbers seem to fail in ? To understand this, let's take a closer look at the congruence

.

If we were solving this as an equation over the reals, we would immediately conclude that either must be zero, or must be zero in order for the product to equal zero. However, this is not the case in ! It is possible to multiply two nonzero elements of and get zero. For example, we have

But wait! Suppose we take a close look at this last product, and we set and . Then we have -- another of the solutions of our congruence! (One can check that the other two factorizations don't lead to any valid solutions; however, there are many other factorizations of zero that need to be checked.)

In the ring of real numbers, it is a well-known fact that if , then or . For this reason, we call the ring of real numbers a **domain**. However, a similar fact does *not* apply in general in ; therefore, is not in general a domain.

## Topics

The following topics expand on the flexible nature of modular arithmetic as a problem solving tool:

## Miscellaneous

### The binary operation "mod"

Related to the concept of congruence, mod is the binary operation ** mod **, which is used often in computer programming.

Recall that, by the Division Algorithm, given any two integers and , with , we can find integers and , with , such that . The number is called the *quotient*, and the number is called the *remainder*. The operation * mod * returns the value of the remainder . For example:

mod , since .

mod , since .

mod , since .

Observe that if mod , then we also have (mod ).

An example exercise with modular arithmetic:

**Problem:**

Let

be a nine-digit positive integer (each digit not necessarily distinct). Consider

,

another nine-digit positive integer with the property that each digit when substituted for makes the modified D divisible by 7. Let

be a third nine-digit positive integer with the same relation to E as E has to D.

Prove that every is divisible by 7.

**Solution:**

Any positive integer can be expressed .

Since **10=3 mod 7**, and since it holds that if **a=b mod c** then **mod c**, then D can be expressed much more simply mod 7; that is,
= **x mod 7**.

Each number in E must make the modified D equal 0 mod 7, so for each , , where c is the coefficient of
and k is an element of {-2,-1,0,1,2}. The patient reader should feel free to verify that this makes **D = 0 mod 7**.

In terms of terms, then, we find each .

Then **mod 7** can be expressed
**mod 7 = (9x)- x = 8x = x mod 7**.
(Note that the 7s, which do not change the mod value, have been eliminated.)

Each number in F must make the modified E equal **0 mod 7**, so for each , .

By design and selection of k, all are integers, and is always an integer because it is the difference of two integers.

is a member of the set {1, 2, 3}. Since no divides 7, 7 may be factored and is the product of two integers.

Let then **7A mod 7 = 0 mod 7** for all , QED.

## Resources