You are on page 1of 12

Introductory Modular Arithmetic in

Mathematical Competitions - Lecture Notes


October Math Club: Number Theory (NT)
Aryansh Shrivastava

0 Introduction
Modular Arithmetic is one of the most essential tools in the field of Number
Theory. As a result, for many Number Theory problems, a thorough under-
standing of Modular Arithmetic is integral. In this handout, we will cover
the preliminary ideas of Modular Arithmetic that are frequently employed
for use in problems, stemming from various math competitions such as the
AMC 8/10/12, all from scratch, assuming no prior knowledge. However,
higher-level ideas such as quadratic residues and Pell equations will not be
covered in this handout.

1 LCM and GCD: Quick Asides


Throughout the handout, assume gcd(a, b) denotes the greatest common divisor
of integers a, b > 0, while lcm[a, b] denotes the least common multiple of
integers a, b > 0. gcd(a, b) can usually be calculated either with common
sense or with the Euclidean Algorithm1 . Note that the somewhat useful
gcd(a, b) · lcm[a, b] = ab identity can be used to find lcm[a, b], provided that
gcd(a, b) is known and lcm[a, b] is nontrivial to compute directly.

Now that we have that out of the way, let’s start off with a few important
notations and definition pertaining directly to modular arithmetic, providing
examples along the way.
1
Look this algorithm up if you don’t know what it is.

1
2 The Basics of Modular Congruence
We say that there is modular congruence between two integers a and b in
an integral modulus m ≥ 2 if and only if the division a−b
m
yields an in-
teger. In general, we write this in modular congruence form as a ≡ b
(mod m), pronounced “a is congruent to b modulo m.” We can also write
this in parametric form as a − b = mk; k ∈ Z2 .

Example 2.1. Use modular definitions to justify 10 ≡ −2 (mod 12). Con-


vert the congruence into parametric form.

10 − (−2)
We have that 10 ≡ −2 (mod 12) because = 1 is an integer by
12
modular definition. In parametric form, this becomes 10 − (−2) = (12)(1); 1 ∈ Z .

Another convenient notation we can use is called modular remainder form.


In this, we just use x mod m to mean the remainder when an integer x ≥ 0
is divided by an integer modulus m ≥ 2. It is important to note that we
leave out the parentheses surrounding mod m from before. Note that the
remainder when x is divided by m is also known as the modular residue of
x (mod m). Additionally, (mod m) is also known as the modular suffix.

Example 2.2. What is the value of 10 mod 3?

From the previous definition, 10 mod 3 is just the remainder obtained when
10 is divided by 3. Note that 10 = 3(3) + 1, so the division 10 ÷ 3 leaves a
remainder of 1. Hence, 10 mod 3 = 1 .

Switching between different forms is an important skill, and we can establish


the following identities just by converting between the a ≡ b (mod m) and
a − b = mk; k ∈ Z forms3 :

• a ≡ b (mod m) ⇐⇒ b ≡ a (mod m)4

• a ≡ a mod m (mod m)

• If a ≡ A (mod m) and b ≡ B (mod m), we have all of the following:


2
Z is the set of all integers, so this is simply shorthand to establish that k is an integer.
3
As an exercise, try to derive these by converting between the two forms.
4
This is the reason why we don’t have to worry about the order of a and b in a modular
congruence.

2
– a + b ≡ A + B (mod m)
– a − b ≡ A − B (mod m)
– ab ≡ AB (mod m)
a A 5
– b
is not necessarily congruent to B
(mod m)
!
a A m
– (♣) ≡ mod
gcd(a, A) gcd(a, A) gcd(m, gcd(a, A))

– an ≡ An (mod m) for any integer n ≥ 06

Remark. Do not memorize these identities! Instead, use the problem set,
along with the examples in this handout, to practice many different problems
involving these identities to fully internalize them. A blatant example of this
has been marked by (♣) above (which is actually a very important identity,
as we’ll see later).

Example 2.3. Study the nature of the (♣) identity. Then, without re-
ferring back to it, use it to reduce the congruence 20 ≡ 4 (mod 16) through
division. (If reduction is performed correctly, the result should make sense.)

First, let’s look at the first part, namely 20 ≡ 4. The greatest common factor
that we can divide by to make those numbers simpler is gcd(20, 4) = 4, just by
common sense. This gives us 5 ≡ 1. Now, let’s look at the (mod 16) suffix.
The modulus here needs be divided by gcd(16, gcd(20, 4)) = gcd(16, 4) = 4
in order for the congruence to remain valid. Doing as described, we obtain a
new suffix of (mod 4). Putting it altogether, we get 5 ≡ 1 (mod 4) . The
result makes sense, as 5−1 4
= 1 is indeed an integer, which means that the
congruence is true, so we’re done.

3 Solving Linear Congruences


We commence with this section by alluding back to preliminary algebra, such
as solving a linear equation like 2x − 1 = 3. The typical method to solve
these was through performing the exact same arithmetic operations to both
sides of the equation. For the rest of this handout, let LHS refer to the
5
Division does not work the regular way in modular congruences, and instead, the
property below this shows how to correctly utilize modular division.
6
Deriving a similar property for logarithms or roots is much more complicated, and is
not important to address here.

3
left-hand side (of an equation), and let RHS refer to the right-hand side (of
the same equation).

For the sake of completeness, we present the complete sequence of steps


required:

• 2x − 1 = 3 (Given)

• 2x = 4 (Add 1 to both sides)

• x = 2 (Divide both sides by 2, yielding the answer)

Just as you may expect, we can also solve linear congruences in a similar al-
gebraic way. Once again, we must be careful that division does not work the
normal way in modular arithmetic, and instead make use of the (♣) identity.
It is also worth noting that numbers that are not integers are not valid in a
modular congruence.

Example 3.1. Solve the congruence 2x − 1 ≡ 5 (mod 12).

We want to end up isolating x on one side, so let’s just try to group all of
the constants on the RHS. Let’s start by adding 1 to both sides. This gives
us 2x ≡ 6 (mod 12). Now, we would like to divide both sides by 2, but,
unfortunately, division doesn’t work the normal way in congruences, so we
use the (♣) identity from before. As usually, we will proceed with the appli-
cation of this identity in a logical fashion, not in a formulaic one. We want
to divide out by gcd(2, 6) = 2, but the modulus must also be divided by
gcd(12, gcd(2, 6)) = 2. Implementing the divisions gives us x ≡ 3 (mod 6) ,
which is in fact our answer since x is isolated by itself on the left.

Remark. There are actually infinitely many possible numerical values of x


that can satisfy the above congruence, just as long as they are congruent to
3 modulo 6. A few examples of different values that work are −3, 9, and 15.
As an exercise, try to validate that these values satisfy the congruence in the
previous example by plugging them in for x.

Another important idea is that of a modular inverse, which is defined as


follows. Define the modular inverse of a (mod m) to be a−1 (mod m)
such that aa−1 ≡ 1 (mod m). Also, note that a−1 (mod m) exists if and
only if gcd(a, m) = 1. Multiplying both sides of an equation by the modular
inverse of a coefficient can be useful as an alternative to the (♣) identity, as
we’ll investigate in the next example. Moreover, sometimes, it can be hard to

4
find the value of a modular inverse by guess-and-check, so we will establish
more concrete ways to do this later in this handout.

Example 3.2. Solve the congruence 5x ≡ 3 (mod 6).

We want to solve for x so we can make use of multiplying both sides by 5−1
(mod 6) in order to cancel the 5 on the LHS, as 5 · 5−1 ≡ 1 (mod 6). This
gives us x ≡ 5−1 · 3 (mod 6). We aren’t done yet, as we can simplify 5−1
(mod 6). We could try all possible inverses and multiply each one by 5 to see
if it leaves modular residue 1, but instead, we can be clever. Observe that
5 ≡ −1 (mod 6). Therefore, 5 · 5 ≡ (−1)2 ≡ 1 (mod 6). This means that
5−1 ≡ 5 (mod 6)! Therefore, x ≡ 5 · 3 ≡ 15 (mod 6) =⇒ x ≡ 3 (mod 6) .

Alternatively, we could also solve the congruence without invoking modu-


lar inverses. Note that 3 ≡ 3 + 2(6) ≡ 15 (mod 6), so we can rewrite the
problem as 5x ≡ 15 (mod 6). Now, we use the (♣) identity. We divide
the congruence part by gcd(5, 15) = 5, which means that we must divide
our modulus by gcd(6, gcd(5, 15)) = 1, which is the same as not dividing
at all, so our modulus is not affected. Doing this gives us an answer of
x ≡ 3 (mod 6) , which is the same as what we got before.

4 Exponential Remainders
Nowadays, it is well-known for many test writers to invoke remainders when
scary-looking (and usually incomputable) exponents are divided by a certain
integer. Sometimes, even though the problem is not directly formatted like
this, it may internally invoke such exponents, which is why it is important
to develop modular machinery to address these situations. Let’s start off by
diving into a useful results. Although these results won’t be proven in this
handout, it is encouraged that the reader look up the proofs by him/herself.

Euler’s Totient Theorem states that, for any integers a and m that sat-
isfy gcd(a, m) = 1, we have aφ(m) ≡ 1 (mod m), where φ(m) denotes the
number of positive integers Z less than m that satisfy gcd(m, Z) = 1.

We can also develop a general identity, (), that states the following:
For any prime number p, φ(p) = p − 1. In general, for any positive integer

5
! ! !
1 1 1
m > 1 that is not prime, φ(m) = m 1 − 1− ... 1 − , where
p1 p2 pi
all variables in the form pnumber are the prime factors of m.

Example 4.1. What is the value of φ(100)?

Note that the prime factorization of 100 is 22 · 52 , so our prime factors


of interest !
are 2 and! 5. The () identity from before gives us φ(100) =
1 1
100 1 − 1− = 40 .
2 5
Because aφ(m) ≡ 1 (mod m), exponentiation by any integer n ≥ 0 gives
anφ(m) ≡ 1n ≡ 1 (mod m). Now, we can multiply both sides of this by ak for
some integer k ≥ 0, giving ak+nφ(m) ≡ ak (mod m). Letting b = k + nφ(m),
we can rewrite this as ab ≡ ab mod φ(m) (mod m), which we will call the ()
identity.

Example 4.2. What is the remainder when 7402 is divided by 1000?

The problem indicates that we need to find the modular residue of 7402
(mod 1000). Note that gcd(7, 1000) = 1, so we can proceed safely by us-
ing the results of Euler’s Totient Theorem. By the () identity, this is
the same as the residue of 7402 mod φ(1000) (mod 1000). By the () identity,
φ(1000) = 400 (calculation of this is left as an exercise to the reader). There-
fore, the given is equivalent to 7402 mod 400 ≡ 72 ≡ 49 (mod 1000), which
leaves a residue of 49 .

For the cases where Euler’s Totient Theorem either does not work or does
not suffice to find the modular residue of a scary exponentiation, we can use
inductive reasoning. In particular, we can find the first few exponents of the
base and then find the cycle of modular residues. We can then induct the
cycle to pin down the modular residue that the problem originally asked for.
We will also introduce another method to solve such problems, namely the
Chinese Remainder Theorem, in a later section.

Example 4.3. What is the units digit of 22018 ?

Note that the units digit of a number is exactly the same as the remainder
when that number is divided by 10. We proceed with inductive reasoning by
looking at the modular residues of the first few powers of 2 modulo 10.

6
• 21 ≡ 2 (mod 10)
• 22 ≡ 4 (mod 10)
• 23 ≡ 8 (mod 10)
• 24 ≡ 6 (mod 10)
• 25 ≡ 2 (mod 10)
Aha! Our residues cycle as 2, 4, 8, 6, 2, 4, 8, 6, .... Because each cycle has
length 4 (four resides are contained inside the cycle), the 2018th residue will
have position 2018 mod 4 = 2 in the cycle. Therefore, because all residues
with position 2 in their respective cycle have value 4, our answer is 4 .

5 Common Modular Residue Rules


Sometimes, it’s useful to be able to find modular residues quickly without
having to use long division every time. As such, we present a few useful
modular residue rules to keep in mind, with an example for each one. For
cases of modular residue 0, these also go hand-in-hand with the divisibility
rules for such numbers:
• A number leaves the same modular residue (mod 2k ) for k ≥ 1 that
the number formed by the last k digits in its decimal representation
leaves (mod 2k ). For example, 9998796 ≡ 96 ≡ 0 (mod 4), where the
considered digits are colored in red.
• A number leaves the same modular residue (mod 5k ) for k ≥ 1 that the
number formed by the last k digits in its decimal representation leaves
(mod 5k ). For example, 1340193782019375 ≡ 375 ≡ 0 (mod 125),
where the considered digits are colored in red.
• A number leaves the same modular residue (mod 3) and (mod 9) that
the sum of its digits leaves (mod 3) and (mod 9) (respectively). For
example, 123456789 ≡ 1 + 2 + 3 + ... + 9 ≡ 9·10
2
≡ 0 (mod 9), while
1723103 ≡ 1 + 7 + 2 + 3 + 1 + 0 + 3 ≡ 17 ≡ 1 + 7 ≡ 2 (mod 3).
• A number leaves the same modular residue (mod 11) that the alternat-
ing difference/sum (strictly from right to left) of the digits in its decimal
representation leaves (mod 11). For example, 1315 ≡ 5 − 1 + 3 − 1 ≡ 6
(mod 11), where the digits are taken from right to left, and correspond-
ing digits have been color-coded.

7
These rules, by themselves, may not seem to be directly useful, but can often
be implemented to simplify things a little. This is important, as simplification
plays a very big role in modular arithmetic, and it is important to keep
expressions and such as simple as possible.

6 Basic ax + by = c Diophantine Equations


We now proceed by introducing a more general type of equation that can
be solved using modular arithmetic, namely a Diophantine equation, an
equation that requests only integer solutions. The most common linear Dio-
phantine on contest problems is of the form ax + by = c, where a, b ≥ 2
are integers. In this section, we will investigate a general method to ap-
proach specific equations of this form. The general method can be outlined,
as follows:

• Consider the equation modulo either a or b.

• Solve the resulting congruence for the variable left.

• Plug in the parametric form of the modular solution for the variable
into the main equation and solve for the remaining variable.

• Write out the general solution parametrically as an ordered pair (x, y)


with any restrictions on the parameter.

Moreover, note that ax + by = c has a solution for integral (x, y) only if


c
gcd(a,b)
is an integer.

As usual, the procedure is best seen with an example and an application


of pure common sense rather than memorized steps.

Example 6.1. Find all integer solutions (x, y) for 3x + 4y = 5.

5
First, note that gcd(3,4) = 5 ∈ Z, so we can proceed. By common sense, we
have two variables, so things don’t look so good from an “isolate the vari-
able by itself on one side” perspective. Let’s use wishful thinking. We want
to be able to isolate, for example y, by itself, so we need some way to get
rid of x. But modular arithmetic is good at that, especially since x and y
have to be integers! We can consider the equation (mod 3) and 3x ≡ 0
(mod 3), so it will vanish. This leaves us with 4y ≡ 5 (mod 3). Now, note
that 4y ≡ 3y + y ≡ y (mod 3) and 5 ≡ 2 (mod 3), so right away, we have
y ≡ 2 (mod 3). Now, since we originally wanted non-modular solutions, let’s

8
convert this to parametric form, which gives y = 3k + 2 (for now, we ignore
the k ∈ Z condition, as this is obvious and will be implemented later). Let’s
plug in y = 3k + 2 into the original equation and solve for x in terms of our
parameter k:

• 3x + 4(3k + 2) = 5

• 3x = −3 − 12k

• x = −1 − 4k

This gives us the general solution (−1 − 4k, 3k + 2); k ∈ Z to the Diophan-
tine, and we’re done.

7 Systems of Linear Congruences


Earlier in this handout, we’ve dealt with standalone linear congruences, so
now it’s time to consider a whole system of them, in order to see how we can
combine two congruences in entirely different moduli (plural form of mod-
ulus). It is important to address that all systems of modular congruences
tested will only have one variable, and it is important for the test taker to
understand how to combine them. Let’s start by establishing a central the-
orem.

The Chinese Remainder Theorem (CRT) states that, for any system of
linear congruences invoking one variable that has at least one solution, it has
exactly one unique solution modulo the LCM of all the moduli in the system.

Similar to ax + by = c Diophantines, it is often fruitful to convert from


modular form to parametric form in order to solve such systems. Let’s look
at an example.

Example 7.1. Solve the following system:

x ≡ 2 (mod 3)
x ≡ 3 (mod 5)

Parametrically, x can be expressed as 3a + 2 and 5b + 3. Hence, we can set


these equal to get 3a + 2 = 5b + 3. This is exactly like the Diophantine situ-
ation we analyzed in the previous section, so we can consider the equation

9
(mod 3). This gives us 2b ≡ 2 (mod 3). Now, we use the (♣) identity to sim-
plify this to b ≡ 1 (mod 3). This can parametrically be written as b = 3c + 1.
Now, recall that x = 5b + 3, so we can plug b into this condition. This gives
x = 5(3c + 1) + 3 = 15c + 5 + 3 = 15c + 8. We can reverse-engineer the
modular form for x from this parametric form to get x ≡ 8 (mod 15) . This
solution is unique, as CRT proclaims that any solution modulo lcm[3, 5] = 15
must be unique.

Let’s end the section by looking at a special example where there is a “trick”
to solve the system much faster.

Example 7.2. Solve the following system:


x ≡ 3 (mod 4)
x ≡ 6 (mod 7)

While we could go by a solution to this the old-fashioned way like the previous
problem, let’s look at a much faster way. Notice that 3 ≡ −1 (mod 4) and
6 ≡ −1 (mod 7). This gives x ≡ −1 (mod 4, 7) =⇒ x + 1 ≡ 0 (mod 4, 7),
where the comma-separated notation denotes the manipulation for each mod-
ulus separately. In other words, x + 1 must be divisible by both 4 and 7, and
by the definition of LCM, it must be divisible by lcm[4, 7] = 28. This gives
us x + 1 ≡ 0 (mod 28) =⇒ x ≡ −1 (mod 28) =⇒ x ≡ 27 (mod 28) .

8 Contest-Level Problems
Thus far, all of the problems established in the examples have been direct ap-
plications of basic concepts that were covered in their respective sections. In
this section, we will consider three example problems that are either directly
from contests or very similar to the types of modular arithmetic questions
on contests, usually invoking more than one fundamental. Without further
ado, let’s begin.

Example 8.1. Let S(n) equal the sum of the digits of positive integer n.
For example, S(1507) = 13. For a particular positive integer n, S(n) = 1274.
Which of the following could be the value of S(n + 1)? (Source: AMC)

(A) 1 (B) 3 (C) 12 (D) 1239 (E) 1265

10
Note that n ≡ S(n) ≡ 1274 ≡ 1 + 2 + 7 + 4 ≡ 14 ≡ 5 (mod 9) =⇒ n + 1 ≡ 6
(mod 9). Therefore, the only choice congruent to 6 (mod 9) is (D) .

Example 8.2. Let N = 123456789101112 . . . 4344 be the 79-digit number


that is formed by writing the integers from 1 to 44 in order, one after the
other. What is the remainder when N is divided by 45? (Source: AMC)

Well, we obviously can’t divide N by 45, as this seems unreasonable for


the amount of time provided per question on an AMC, so it’s probably
a good idea to look at things from a modular point of view. Essentially,
the problem is asking for the modular residue of N (mod 45). However,
45 doesn’t seem like a very nice number, so instead, let’s break things up
(mod 5) and (mod 9) individually, and then combine the individual residues
with a CRT argument. For the (mod 5) residue, we just consider the last
digit of N by our modular residue rules, which gives us 4 (mod 5). Now,
let’s consider the (mod 9) residue. Since any number is congruent to the
sum of its digits (mod 9) (also by the modular residue rules), we have
N ≡ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 + ... + 4 + 3 + 4 + 4
(mod 9). But we can do better than this! We can reuse this property in
reverse to group all of the two-digit number separations back together to-
gether, giving us a nice, simple arithmetic series which we know how to sum:
N ≡ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 + ... + 4 + 3 + 4 + 4 ≡
1 + 2 + 3 + ... + 43 + 44 ≡ 44·45 2
≡ 0 (mod 9). This gives us N ≡ 0
(mod 9) and N ≡ 4 (mod 5), so we solve the system of linear congruences
in the way we covered before, converting from modular to parametric forms.
N = 9a = 5b + 4, so 4a ≡ 4 (mod 5) =⇒ a ≡ 1 (mod 5) =⇒ a = 5k + 1.
Hence N = 9a = 9(5k + 1) = 45k + 9 =⇒ N ≡ 9 (mod 45).

Example 8.2. What are the last two digits in the decimal representation of
2013
20112012 ? (Source: Own)

Clearly, this is impractical by hand, so we seek a modular arithmetic solu-


tion. To find the last two digits, we consider the problem (mod 100). It
seems like a good idea to use results from Euler’s Totient Theorem here, as
gcd(2011anything , 100) = 1. We can use the () identity to see that φ(100) =
40, so we consider the modular residue of 20122013 (mod 40) by the () iden-
tity. Clearly, 2000 ≡ 0 (mod 40), so 20122013 ≡ 122013 (mod 40). Now, we
might just have to use inductive reasoning to do this, but let’s take a step
back. Let’s break down (mod 40) into (mod 5) and (mod 8), combin-
ing with a CRT argument (similar to the previous problem). 122013 ≡ 22013

11
(mod 5), and we can obliterate this quite easily by using Euler’s Totient The-
orem again for φ(5) = 4. This leaves us with 2 (mod 5). Now, let’s consider
(mod 8). 122013 ≡ 42013 ≡ 16 · 42011 ≡ 0 (mod 8). Let’s call 20122013 the
variable N for convenience purposes. We have N ≡ 2 (mod 5) and N ≡ 0
(mod 8), so we write N = 5a + 2 = 8b, for 3b ≡ 2 ≡ 12 (mod 5) =⇒ b ≡ 4
(mod 5) =⇒ b = 5k + 4 =⇒ N = 8b = 8(5k + 4) = 40k + 32 =⇒ N ≡ 32
(mod 40). Therefore, the entire thing boils down to finding the modular
residue of 201132 (mod 100). We reduce gradually (with a little bit of fea-
sible multiplication): 201132 ≡ 1132 ≡ 2116 ≡ 418 ≡ 814 ≡ 612 ≡ 21
(mod 100), and we’re done.

9 End of Notes
Thank you for taking the time to attend this math club and read these lecture
notes! Hopefully you found them useful, as they were developed after 10 + 
hours of effort.

12

You might also like