Modular Arithmetic Handout
Modular Arithmetic Handout
x ≡ b1 (mod a1 ),
x ≡ b2 (mod a2 ),
··· ,
x ≡ bn (mod an )
Note that this is an existence theorem: it does not tell us how to find k. For example,
the theorem tells us that
is equivalent to x ≡ k (mod 30) for some integer k, but it does not tell us what k is. (We
can manually find that k = 23.)
1
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Solution. The p-safe condition translates nicely into modular arithmetic. In order for n
to be 7-safe, it has to be more than two away from any multiple of 7. This means it has
to be congruent to 3 or 4 modulo 7. Conversely, if n is congruent to 3 or 4 modulo 7
then it is more than 2 away from the nearest multiple of 7.
• n ≡ 3, 4 (mod 7),
• n ≡ 3, 4, 5, 6, 7, 8 (mod 11),
• n ≡ 3, 4, 5, 6, 7, 8, 9, 10 (mod 13).
Moreover, 7, 11, and 13 are pairwise relatively prime (they are all prime), so we can use
the Chinese Remainder Theorem to splice together these conditions. For example, the
congruences
• n ≡ 4 (mod 7),
• n ≡ 8 (mod 11),
• n ≡ 3 (mod 13).
have a unique solution modulo 7 · 11 · 13 = 1001. To find all possible solutions modulo
1001, we need to pick a residue modulo 7, a residue modulo 11, and a residue modulo 13;
each such choice results in exactly one residue modulo 1001. With our list above, we find
that this can be done in 2 · 6 · 8 = 96 ways. So there are 96 residues modulo 1001 that
are 7-safe, 11-safe, and 13-safe.
We now want to find the number of positive integers at most 10, 000 that work. From
our work above, we see that:
• ...,
So there are 960 working integers from 1 to 10010, inclusive. We just need to examine
the integers 10001, . . . , 10010 and subtract any overcounts to find the final answer.
Note that 10010 is a multiple of 7, 11, and 13. The next smallest multiples of 7, 11,
and 13 are 10003, 9999, and 9997, respectively. From this, we see that the only working
integers from 10001 to 10010 are 10006 and 10007. So we have two overcounts, and the
number of positive integers less than or equal to 10, 000 which are 7-safe, 11-safe, and
13-safe is 960 − 2 = 958 .
2
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Solution. We’re given three different values of m to use; let’s investigate them separately.
For m = 2, the condition implies that
Since the ai need to be a permutation of (1, 2, . . . , 30), with 15 odd numbers and 15 even
numbers, this implies that a1 , a3 , . . . , a29 are all of the opposite parity as a2 , a4 , . . . , a30 .
Thus we can choose that either a1 , a3 , . . . , a29 are all even, or a1 , a3 , . . . , a29 are all odd.
There are 2 ways to do this.
Again, since the ai are a permutation of (1, 2, . . . , 30), 10 of them congruent are to each
of 0, 1, and 2 modulo 3. So we can choose what a1 , a4 , . . . , a28 are modulo 3, then pick a
different residue for a2 , a5 , . . . , a29 , and use the last residue for a3 , a6 , . . . , a30 . We have
3! = 6 ways to do this.
We can choose the residues of a1 , a6 , . . . , a26 modulo 5, then pick a different residue for
a2 , a7 , . . . , a27 , and so on up to a5 , a10 , . . . , a30 . There ae 5! = 120 ways to assign residues
modulo 5.
We have now chosen a residue for each ai modulo 2, 3, and 5; by the Chinese Remainder
Theorem this results in a unique residue modulo 30, and hence a unique element of
{1, 2, . . . , 30}. So our choices uniquely define the sequence (a1 , a2 , . . . , a30 ). We’re not
done yet; we need to check if this sequence is a permutation. That is, we need to check
that no two elements are equal.
What happens if ai = aj ? This means that ai ≡ aj (mod 2), so that i ≡ j (mod 2).
We see this because
3
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Definition 2.1. The totient of n, denoted φ(n), is the number of positive integers less
than or equal to n which are relatively prime to it.
We won’t go over it’s proof in this class, but the totient is easily computable via the
closed form
1 1 1
φ(n) = n 1 − 1− ··· 1 −
p1 p2 pk
where p1 , p2 , . . . , pk are the primes dividing n, listed without repetition. The proof can
be done using Chinese Remainder Theorem on mods p1 , . . . , pk and is left as an exercise
to the reader.
Proof. Denote S the set of residues modn which are coprime with n. By definition,
|S| = φ(n). Now, consider the set T = {as (mod n)|s ∈ S}, which is the set of the
residues multiplied by a. As a is coprime with n, all elements of T are coprime with n as
well. Furthermore, no two elements in T are the same, since as ≡ as0 (mod n) =⇒ s ≡ s0
(mod n). So, T is a set of numbers coprime to n with magnitude |T | = |S| = φ(n). Hence,
T = S.
Of course, as they are the same set, the product of all the elements should be the
same too. Hence,
Y Y Y Y Y
s≡ t (mod n) =⇒ s≡ as ≡ aφ(n) s (mod n)
s∈S t∈T s∈S s∈S s∈S
=⇒ aφ(n) ≡ 1 (mod n)
as desired.
Corollary 2.3
Fermat’s Little Theorem For any prime p and a coprime to p, we have ap−1 ≡ 1
(mod p)
4
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
is divided by p.
Solution. First, let’s try to get rid of the floors. From FLT, we note ap ≡ a (mod p), so
p
a ap − a
= .
p p
Therefore, it suffices to determine
p−2 p
X a −a
(mod p)
p
a=1
since every remaining term in the sum is divisible by p2 (The last term in the expansion
of (p − a)p is negative because p is an odd prime). Now, we have
Therefore, we have
p−2 p p(p+3)
X a −a 2 p+3
≡ ≡ (mod p).
p p 2
a=1
5p − 2p ≡ 3 (mod p)
5q − 2q ≡ 3 (mod q)
5
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Now, if q = 3, then we see that we can also have p = 3. If q = 3 and p > 3, then
p|(53 − 23 ) = 117. This implies p = 13. Therefore, with permutations, we obtain the
pairs (3, 3), (3, 13), (13, 3).
Now, let’s assume p ≥ q > 3. In this case, clearly gcd(q, 5q − 2q ) = 1, so 5p − 2p ≡ 0
(mod q). This implies 5p ≡ 2p (mod q). From FLT again, we know 5q−1 ≡ 2q−1 ≡ 1
(mod q). Now, since gcd(p, q − 1) = 1, there exist integers a, b such that
ap + b(q − 1) = gcd(p, q − 1) = 1.
Note that
5ap+b(q−1) = (5p )a · (5q−1 )b = (2p )a · (2q−1 )b = 2ap+b(q−1) (mod q).
However, this implies 5 ≡ 2 (mod q), which can only happen when q = 3, so we are done.
The only solutions are (3, 3), (3, 13), (13, 3) .
§3 Orders
From Fermat’s Little Theorem, we know that ap−1 ≡ 1 (mod p) for p - a. But, often
times we don’t need to raise a to that high of a power in order to get it to 1 (mod p).
For example, 26 ≡ 1 (mod 7), but 23 ≡ 1 as well.
This motivates us to define the order of a (mod p), which is the minimum e > 0
such that ae ≡ 1 (mod p), denoted e = ordp (a). Note that order exists, since it is at
most p − 1.
Theorem 3.1
If ak ≡ 1 (mod p) for prime p and p - a, then ordp (a)|k.
Proof. Denote e = ordp (a). By definition, ae ≡ 1 (mod p), so ane ≡ 1 (mod p) for any n
by raising both sides to the nth power. So, choose n such that ne ≤ k < (n + 1)e. This
way, by dividing the relations, we get ar ≡ 1 (mod p), where r = k − ne is the remainder
when k is divided by e.
Corollary 3.2
ordp (a) divides p − 1
Proof. We know ap−1 ≡ 1 (mod p), and applying Theorem 3.1 gives the desired result.
As orders can be really small, a natural follow-up question might be whether or not FLT
is actually tight. In fact, there do exist numbers with order p − 1 for all primes p, and
such numbers are known as primitive roots.
6
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Proof. We first begin with Lagrange’s Theorem which is obviously true for the reals but
also holds mod p:
Proof. We prove this with induction on deg P . The result is clear when deg P = 1. Now,
suppose that we have the claim for polynomials of degree deg P − 1.
If P does not have any roots modulo p, then we are already done. Otherwise, sup-
pose it has root r. We can perform synthetic division on P to get P (x) ≡ (x − r)Q(x) + k
(mod p). However, since r is a root, k = 0. So, P (x) ≡ (x − r)Q(x) (mod p). Now, Q(x)
has degree 1 less than P , so it has at most deg P − 1 roots. Hence, P has at most deg P
roots.
Now, note that if k|p − 1 for some k, then we can write
xp−1 − 1 = (xk − 1)Q(x)
for some Q. xp−1 − 1 admits p − 1 roots by FLT, so the RHS should also have p − 1 roots.
However, xk − 1 has at most k roots and Q(x) has at most p − 1 − k, by Lagrange’s
Theorem. So, in order for equality to hold, xk − 1 actually has exactly k roots.
Now, we are ready to finish. Let N (e) denote the number of residues modulo p with
order e. By Theorem 3.1, we have that
X
k= N (e)
e|k
for any k|p − 1, since the residues which satisfy xk ≡ 1 are precisely those whose orders
divide k. However, we’ve seen this type of summation a long time ago, we can compute
N (e) explicitly with Mobius Inversion! In fact, we get that
X e
N (e) = iµ = φ(e)
i
i|e
(if you are unfamiliar with Mobius Inversion, you can also note that the function N (e) is
uniquely defined by the summation relation, and φ(e) works by Dirichlet Convolution, so
it has to be the correct function.)
Remark 3.5. The existence of a primitive root actually allows us to “generate” all elements
of the residue class. To see this, let ζ be a primitive root modulo p. Since ζ k 6= 1 (mod p)
for any k < p − 1, this means that the sequence ζ, ζ 2 , . . . , ζ p−1 does not cycle, and hence
must cover every single nonzero residue.
In fact, if we can express a number a as ζ k (mod p), this gives us a very efficient way
to compute its order. We have that ae ≡ ζ ke , and the order of ζ is known to be p − 1. Hence,
e p−1
a ≡ 1 (mod p) ⇐⇒ p − 1|ke ⇐⇒ e
gcd(k, p − 1)
7
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
p−1
Hence, the order of a is gcd(k,p−1) . Using ζ as a generator, see if you can rederive the fact
that N (e) = φ(e).
Solution. Note that cn is basically just the “order” of cn mod 210.. Unfortunately, many
of the theorems we have proven above only apply to primes. So, in order to compute cn ,
we instead consider the orders of n mods 2, 3, 5, 7. If we denote them as wn , xn , yn , zn
respectively, then we have that cn = lcm(wn , xn , yn , zn ).
Let’s see how this applies to zn , for example. 7 has 6 nonzero residues, and by the above
we know 2 of them have order 6, 2 have order 3, 1 has order 2 and 1 has order 1. So, the
orders (1, 2, 3, 6) occur in ratio 1 : 1 : 2 : 2.
Now, note that the lcm of all possible values of wn , xn , yn , zn is 12, so c|12. Now,
it suffices to find out how often each divisor of 12 occurs.
The only way 4|cn is if yn = 4, which has probability 12 , and the only way it is not
divisible by 2 is if zn = 1, 3, and everything else is 1, which happens with probability
1 1 1 1 1 7
2 · 4 · 2 = 16 . Hence, cn has a 16 chance of being odd, 16 chance of being divisible by 2
but not 4, and a 12 chance of being divisible by 4.
8
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
On the other hand, factors of 3 only come from zn . It’s easy to see that it has probability
2
3 of being divisible by 3. Now, as the powers of 2, 3 in cn are independent, we can get
the expected value of cn just by multiplying the expected value of its powers of 2,3. In
particular:
1 2·7 4 1 2·3 329
E(cn ) = + + + =
16 16 2 3 3 48
329
There are φ(210) = 48 nonzero values of cn , so our answer is 48 · 48 = 329 .
Solution. To start, we see that all odd m work. Set n = m−1 so that nn+1 ≡ (−1)m ≡ −1
(mod m), as desired.
Now, we will find which even m work. Clearly, n must be odd in this case. When
n = 2k + 1,
Now, since nn+1 + 1 is the sum of two perfect squares, all odd prime factors must be
1 mod 4. Therefore, all odd prime factors of m are 1 mod 4. Since m is even and m ≤ 50,
all prime factors are less than 25. The possible prime factors are 5, 13, 17. Therefore, the
only possible even m that could work are m = 2, 10, 26, 34, 50. We can find a construction
in each of these cases. Note that if n2 ≡ −1 (mod m 2 ) and n ≡ 1 (mod 4), then n + 1 ≡ 2
(mod 4), so
m
nn+1 ≡ (−1)odd ≡ −1 (mod ).
2
Also nn+1 + 1 ≡ 1 (mod 2), so we can conclude m divides nn+1 + 1 by the Chinese
Remainder Theorem. The existence of such an n is because all odd prime factors of m
are 1 mod 4 (We will discuss more about this in next week’s handout!). From this, we
can complete our constructions:
• For m = 2, let n = 1.
9
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Lemma 3.10
For any prime p, we have p | M if and only if every prime factor of p − 1 divides M .
Proof. We will first show that this condition is necessary. Suppose otherwise, that we
have primes p, q such that p|M , q|p − 1, q - M . Let k be the largest positive integer such
pk−1 p−1
that pk |M . Consider an arbitrary primitive root g mod pk . Let a ≡ g q (mod pk )
so that aq ≡ 1 (mod pk ) and a 6≡ 1 (mod p). Now, we let n be a value such that n ≡ 1
(mod M pk
), n ≡ a (mod pk ), and n ≡ 0 (mod q). Because these modulos are relatively
prime, such an n exists by the Chinese Remainder Theorem. Now, we note that n satisfies
M |nn − 1 and M - n − 1, a contradiction.
Now, we can easily show that this condition is sufficient. Suppose that nn ≡ 1 (mod M ).
We will show n ≡ 1 (mod M ). It suffices to show this for every prime power dividing M .
Consider a prime p dividing M , and let pk be the largest power dividing M . Since, nn ≡ 1
(mod M ), we have gcd(n, M ) = 0, which implies gcd(n, pk−1 (p − 1)) = 1 because every
prime factor of p − 1 divides M . Since nn ≡ 1 (mod pk ), we see ordpk n | n. However,
we also have ordpk (n) | φ(pk ) = pk−1 (p − 1). Therefore, because n and pk−1 (p − 1) are
relatively prime, we can conclude that ordpk (n) = 1, as desired. Thus, our lemma is
proven.
Now, since 3 divides M = 2013m, we must have 2 | M . Also, since 11 divides 2013m,
we must have 5 | m. Therefore, 10 | m. Let m = 10k so that
M = 20130k = 2 · 3 · 5 · 11 · 61 · k.
From the given range, k can go from 1 to 30. We can check the condition in the lemma
for all of the prime from 1 to 30 first (If p1 , p2 , . . ., pi , satisfy the condition separately,
then p1 · p2 · · · pi clearly satisfies it too). We see that the only prime number that does
not satisfy the condition is 29. Therefore, every value of k from 1 to 30 works, except 29.
Our final answer is
10(1 + 2 + 3 + · · · + 28 + 30) = 4360 .
Solution. First’s, let’s consider the case p = q. Then, we have p2 | 2 · 5p . Since 2 · 5p only
has the prime divisors 2, 5 and is not a multiple of 4, we must have p = q = 5.
We now consider p 6= q. Let’s consider if either of p, q is 5. WLOG, let q = 5. Then, we
have 5p | 5p + 55 . We need 5p + 55 ≡ 0 (mod p), but by FLT, 5p + 55 ≡ 5 + 55 (mod p).
10
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
If p 6= 2, we have ordp (5) | 2(q − 1) and ordp (5) - q − 1 (In the case p = 2, we see 5q−1 ≡ 1
(mod p) as well). Define ν2 (n) to be the largest positive integer k such that 2k | n. We
must have ν2 (ordp (5)) = 1+ν2 (q −1). However, from FLT, we know ordp (5) | φ(p) = p−1.
Therefore,
1 + ν2 (q − 1) = ν2 (ordp (5)) ≤ ν2 (p − 1).
We similarly have
11
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
§4 Problems
Problem 4.1 (2020 HMMT Combinatorics #2). How many positive integers at most
420 leave different remainders when divided by each of 5, 6, and 7?
Problem 4.2 (2017 AMC 12 #21). Last year Isabella took 7 math tests and received
7 different scores, each an integer between 91 and 100, inclusive. After each test she
noticed that the average of her test scores was an integer. Her score on the seventh test
was 95. What was her score on the sixth test?
Problem 4.3 (Fermat Christmas Theorem). Let p ≡ 3 (mod 4) be a prime, and let a
and b be positive integers such that p divides a2 + b2 . Show that p | a and p | b.
Problem 4.4 (Wilson). Prove that (p − 1)! ≡ −1 (mod p).
Problem 4.5. Prove that if p ≡ 1 (mod 4), then there is an element x ∈ Z/pZ such
that x2 ≡ −1 (mod p).
Problem 4.6 (2013 AIME I #11). Ms. Math’s kindergarten class has 16 registered
students. The classroom has a very large number, N , of play blocks which satisfies the
conditions:
(a) If 16, 15, or 14 students are present, then in each case all the blocks can be
distributed in equal numbers to each student, and
(b) There are three integers 0 < x < y < z < 14 such that when x, y, or z students
are present and the blocks are distributed in equal numbers to each student, there are
exactly three blocks left over.
Find the sum of the distinct prime divisors of the least possible value of N satisfying
the above conditions.
Problem 4.7 (2018 AIME I #11). Find the least positive integer n such that when 3n
is written in base 143, its two right-most digits in base 143 are 01.
Problem 4.8 (2001 AIME I #10). How many positive integer multiples of 1001 can be
expressed in the form 10j − 10i , where i and j are integers and 0 ≤ i < j ≤ 99?
Problem 4.9 (2011 AIME I #11). Let R be the set of all possible remainders when a
number of the form 2n , n a nonnegative integer, is divided by 1000. Let S be the sum of
all elements in R. Find the remainder when S is divided by 1000.
Problem 4.10. Find all integers n ≥ 1 such that n divides 2n − 1.
Problem 4.11. Let p be a prime and n a positive integer. Determine the remainder
when 1n + 2n + · · · + (p − 1)n is divided by p, as a function of n,and p.
Problem 4.12. Find all integers n ≥ 1 such that n divides 2n−1 + 1.
nn n
Problem 4.13 (Balkan). Let n be a positive integer with n ≥ 3. Show that nn − nn
is divisible by 1989.
n
Problem 4.14 (Euler). Prove that all factors of 22 + 1 are of the form k · 2n+1 + 1.
Problem 4.15 (2000 IMO Shorlist N1). Determine all positive integers n ≥ 2 that
satisfy the following condition: for a and b relatively prime to n we have a ≡ b (mod n)
if and only if ab ≡ 1 (mod n).
Problem 4.16 (2006 China TST). Find all positive integers a and n for which n divides
(a + 1)n − an .
12
Adithya B., Brian L., William W., Daniel X. (9/2) Modular Arithmetic
Problem 4.17 (2005 IMO Shorlist N6). Let a, b be positive integers such that bn + n is
a multiple of an + n for all positive integers n. Prove that a = b.
Problem 4.18 (USA TST 2003/3). Find all ordered prime triples (p, q, r) such that
p | q r + 1, q | rp + 1, and r | pq + 1.
13