MaVS

Modular Arithmetic

Introduction

Throughout this lesson, all variables are integers. Modular arithmetic is a number system based on integers with an additional component called a modulus. Usually when we do arithmetic, integers can get infinitely big. However in modular arithmetic, every integer is reduced to its remainder upon division by the modulus.

In general, any integer aa can be written as n(q)+rn(q)+r for some quotient qq and remainder rr. Then ara \equiv r mod nn. Then we say aa is congruent or equivalent to rr modulo nn. Often "modulo" is shortened to "mod".

Example: 1515 is the same as 33 mod 1212 because 1515 leaves a remainder of 33 when divided by 1212. So 15=12(1)+315=12(1)+3, which means 15315 \equiv 3 mod 1212.

Consider the general case where we are working mod nn, then we can simplify any integer aa mod nn to its remainder upon dividing by nn, which we call its least positive residue.

As with the previous example, 33 is the least positive residue for 3,15,27,3,15,27, and so on.

Visualizing Modular Arithmetic

Because of the circular nature of residues, modular arithmetic operations can be thought of as mappings performed on points around a circle, where each least positive residue corresponds with a point. Notice that if the modulus is 1212, this looks just like the face of a clock.

 Modulus:

Properties

Modular arithmetic is an integer system, so it inherits properties from the integers like addition and multiplication. Modular arithmetic also inherits 0 as the additive identity and 1 as the multiplicative identity. In other words, a+0aa+0 \equiv a mod nn and a1aa\cdot 1 \equiv a mod nn for any a,nZa,n \in \Z. We will see that additive inverses are always defined, but multiplicative inverses are not defined in general.

Addition

Similar to a clock, we can perform operations like addition and multiplication. We will consider each operation as a map that transforms the remainders to their result when the operation is applied. First, we look at an example of addition.

Example: 33 hours after 1010 o'clock is the same as 1010 hours after 33 o'clock, which is 11 o'clock. In other words, 10+33+10110+3 \equiv 3+10 \equiv 1 mod 1212.

Adjust the numbers for modulus and additive mapping to see more examples of addition in modular arithmetic.

 Modulus:
 Additive mapping:

Notice that each number maps to exactly one other number and every number is mapped to by exactly one other number. This shows that the inverse of addition is defined for every residue, so the additive inverse, subtraction, always exists. This means that for any integer aa and modulus nn, we can always write the additive inverse of aa as a-a, with aa0a-a \equiv 0 mod nn.

Multiplication

Another operation of modular arithmetic is multiplication.

Example: 44 repeated 33-hour time periods, the clock will read the same. This is because 3403 \cdot 4 \equiv 0 mod 1212, which is represented on the circle below.

In this visual, arrows of the same color belong to a cycle that starts with some number aa that is consecutively by another number bb until the pattern eventually repeats with aa again. Just like before, adjust the numbers for modulus and multiplicative mapping to see more examples of multiplication in modular arithmetic.

 Modulus:
 Multiplicative mapping:

In normal arithmetic, the multiplicative inverse of a number is its reciprocal. To avoid using division, we instead write the multiplicative inverse of aa as a1a^{-1}. The inverse of aa mod nn is denoted a1a^{-1}. However, some numbers get mapped to another number more than once, so you can't "undo" that multiplicative mapping.

Example: 42454184 \cdot 2 \equiv 4 \cdot 5 \equiv 4 \cdot 1 \equiv 8 mod 1212. In this sense, we can't "undo" multiplication by 44 mod 1212, so the multiplicative inverse of 44 isn't always defined.

In fact, the multiplicative inverse of some integer aa modulo nn is only defined when gcd(a,n)=1gcd(a,n)=1.

Modular Arithmetic Lessons