Modular Arithmetic

The Mod Operator

Recall the Division Theorem; for any integers and with , there exist unique integers and such that

In a lot of situations we don't actually care about the quotient at all; the remainder is the interesting part, so we give it its own operator.

Note

Definition
The modulo operator returns the canonical remainder when one integer is divided by another. Given integers and with , we write , read as " modulo ", to mean the smallest non-negative remainder when is divided by . That is,

Basically, is just "the remainder when you divide by "; we throw away the quotient and keep only the leftover part. Notice that if and only if , since a remainder of zero means is a perfect multiple of .

In most programming languages this operator is written as %, but that symbol is never used for this purpose in actual mathematics.

Example. Find , and .
For each one, write out the Division Theorem and read off the remainder;

It is important to remember that the remainder is always non-negative, even when is negative. It is tempting to say , but is not a valid remainder; instead we push the quotient one lower ( rather than ) so that the leftover becomes . Therefore, , and .

Modular Congruence

We just saw that , and of course there are infinitely many integers satisfying (namely ). All of these numbers have something in common, so we say they belong to the same equivalence class. Instead of writing something clunky like , we use a special congruence notation, .

Note

Definition
Given integers and and a positive integer , we say that and are congruent modulo and write

to mean that .

Basically, two numbers are congruent modulo if they land on the same spot when you wrap the number line around a circle of circumference ; think of a clock, where 13 o'clock and 1 o'clock are the same thing because .

Note

Theorem
The following statements are all equivalent:

  1. .
  2. .
  3. and have the same remainder when divided by .
  4. for some integer .
  5. .

Proof. Statements (1), (2) and (3) all say the same thing by definition, so it is enough to show that (3) (4) (5) (3), looping back around.

(3) (4): Suppose and leave the same remainder ; then by the Division Theorem,

(4) (5): If , then , which is precisely the definition of .

(5) (3): Suppose , and write and where . Then

Since divides both and , it must also divide their difference . But , and the only multiple of strictly between and is ; hence .

The last characterisation is by far the most useful one in practice:

Example. Is it true that ?
Rather than computing both remainders separately, just subtract and test divisibility;

so . Therefore, . Notice how checking only needs one division, so it's usually the fastest way to verify a congruence.

Congruence is an Equivalence Relation

Note

Lemma
Congruence modulo is an equivalence relation on . That is, for all :

  • (Reflexive) .
  • (Symmetric) If , then .
  • (Transitive) If and , then .

Reflexivity and symmetry are pretty self explanatory so I'm not gonna write much; since everything divides , and if then . For transitivity, we know and , and since divisibility is preserved under sums,

This is exactly why congruence behaves so much like ordinary equality; it slices up into equivalence classes (one for each possible remainder), and acts like "" between the classes.

Properties of Modular Arithmetic

Note

Theorem
Suppose and . Then:

  1. If , and satisfies , then .
  2. If and , then .
  3. If , then for all .
  4. If and , then .
  5. If , then for all .
  6. If , then for all .
  7. If for some , then .
  8. If for some , and , then .
  9. If , then for all .

Basically, congruences can be added, subtracted and multiplied together just like ordinary equations, and both sides can be raised to a positive power (property 9 is just property 4 applied to itself times). Property 1 says a congruence survives shrinking the modulus to any of its divisors; for instance , and since , we also get for free (both leave remainder ).

The odd ones out are properties 7 and 8, which are the only two ways to cancel. You cannot freely divide both sides of a congruence; cancellation is only allowed if you also divide the modulus by the same factor (property 7), or if the factor being cancelled is coprime with the modulus (property 8).

We prove two of these; the rest follow from very similar arguments.

Proof (property 4). Since and , we can write and for some . Then

Since , we have , and hence .

Proof (property 8). Since , we know that

Because , we can write for some (the gcd is always an integer linear combination, via the extended Euclidean algorithm). Multiplying through by ,

Now divides the first term (it contains a factor of ), and divides the second term (since ). Therefore , i.e. .

Example. Find .
The whole point of properties 2 and 4 is that we can reduce every number before doing any arithmetic, instead of multiplying huge numbers together. Reducing each term modulo ;

Then,

Therefore, ; we never had to touch a number bigger than .

Example. It is true that . Can we cancel the common factor of to conclude that ?
No! Checking directly,

so ; naive cancelling produced a false statement. The problem is that , so property 8 does not apply. What we are allowed to do is property 7, dividing the two sides and the modulus by ;

which is true, since and are both odd. Therefore, before cancelling a factor from a congruence, always check first; if shares a factor with the modulus, that factor must be divided out of the modulus too.

The Set of Integers Modulo m

Since congruence modulo behaves so much like equality, we can build an entire self-contained number system out of it.

Note

Definition
Given any positive integer , the ring of integers modulo is denoted , and is the number system with elements and addition and multiplication operations the same as in , except always reduced to the smallest non-negative remainder modulo .
( is also sometimes written as .)

Basically, is clock arithmetic; only the numbers to exist, and everything else wraps around. We may write " in " to mean exactly the same thing as congruence modulo m. For example, the following statements are all valid:

  • in .
  • in .
  • Working in , we have (since ).

Notice how useful the negative representatives are; being allowed to swap for in means we can trade a big ugly number for a tiny one, which makes taking powers almost trivial.

Example. Show that leaves a remainder of when divided by .
Since , property 9 lets us replace the base with before taking the power, and because is even. Here are three ways of writing the exact same proof:

  • .
  • .
  • in .

Therefore, ; all three notations are interchangeable, so use whichever is cleanest for the question at hand.

Reducing Powers Modulo m

Finding large powers in is difficult, because you are allowed to reduce the base modulo , but you can NEVER reduce the exponent modulo . As a quick counterexample, take in ; reducing the exponent would suggest , but

which are clearly not the same. The exponent counts how many times we multiply; it is not itself a number living in .

Instead, the trick is to hunt for a small power of that reduces to something close to in (ideally or ), and then use the Division Theorem on the exponent;

Example. Find .
Try small powers of in ;

so we found a power congruent to almost immediately. Splitting the exponent as ,

Therefore, .

Example. Find .
First reduce the base; , and is the smaller (in size) representative, so work with that. Trying small powers of ,

so the cube gets us to . Splitting the exponent as ,

Therefore, . Don't forget the very last step; the mod operator demands a remainder between and , so must be converted to before you write down the final answer.

Example. Find .
Small powers of don't immediately give here, but keep going;

and since , we get . Splitting the exponent as ,

Therefore, . We will see some more efficient ways to reduce powers modulo in Topic 5.

When No Power Reduces to 1

Sometimes the "find " strategy is doomed from the start. If , then every power of shares that common factor with , so no power of can ever be congruent to or . In that case, look for a repeating cycle in the powers instead.

Example. Find .
Here , so every power of is a multiple of in , and neither nor is; hunting for would be a waste of time. Instead, list the powers and wait for a repeat;

Since , the powers cycle with period from exponent onwards; multiplying both sides by repeatedly gives for all . So we can strip multiples of off the exponent (as long as we stop before reaching );

Therefore, . Note the subtlety: the cycle starts at exponent , not ( is not part of the loop), so we reduce the exponent using the cycle we found, never by blindly taking and allowing exponent .

Alternate solution. We can also exploit property 6 from the properties of modular arithmetic. Write and ; then it is enough to find in , where so the usual trick works;

Now multiply both sides and the modulus by (property 6 with );

Therefore, , agreeing with the first method. The decision here is worth remembering; when the base shares a factor with the modulus, either find the cycle directly, or factor that common divisor out and work in the smaller coprime modulus.

Applications of Modular Arithmetic

These kinds of questions rarely announce themselves as "modular arithmetic"; the skill is recognising that some quantity wraps around with a fixed period, and choosing the right modulus.

Last Digit Problems

The last digit of a number is exactly its remainder modulo (this drops straight out of the base representation, since every higher digit is multiplied by a power of , and ).

Example. Find the last digit of .
We want . Hunting for small powers of in ,

and since ,

Therefore, the last digit of is .

Day of the Week Problems

Days of the week repeat with period , so any "what day will it be" question is just arithmetic in .

Example. Today is a Thursday. What day of the week will it be days from now?
Reduce the number of days modulo ;

So days is complete weeks (which change nothing) plus extra days, and counting six days on from Thursday lands on Wednesday. Even quicker, notice that ; going forward days is the same as going back one day, i.e. the day before Thursday. Therefore, days from now it will be a Wednesday.

Divisibility Tests via Digit Sums

Since , property 9 gives for every ; so in , every number collapses down to the sum of its digits. This is exactly where the "divisible by 9 if its digit sum is" test comes from (and the same works modulo , since too).

Example. Is divisible by ?
Expanding in base and reducing each power of to ,

Therefore, ; the number is divisible by because its digit sum is.

Showing a Number is Not a Perfect Square

A classic exam trick is to rule something out by reducing modulo a small number and checking which residues are even possible.

Example. Show that is not a perfect square.
Work in . Every integer is congruent to one of modulo , so by property 9 there are only four cases for ;

So every perfect square is congruent to or modulo ; a square can never be or in . Now, since , any number is congruent modulo to just its last two digits, hence

Since is not an achievable residue for a square, cannot be a perfect square. Therefore, we ruled it out without computing a single square root; picking a small modulus and eliminating impossible residues is often far faster than direct computation.