Modular Arithmetic Basics: 2.1 Motivation
Modular Arithmetic Basics: 2.1 Motivation
2.1 Motivation
In divisibility, we saw that dealing with remainders was at times more useful, for instance
if r = 0 we have divisibility. The main reason for this is that remainders are smaller than
the original numbers. This was the idea on which Euclid’s Algorithm was based. Modular
arithmetic dwells on this idea in much more depth. We start off by a different motivation,
and we would later see the remainder idea is the same.
Consider the following clock face
The clock has only the numbers from 1 to 12. Where to place the other numbers? If we
think about it, we would place 13 over 1. So then 13 ≡ 1 on a clock. (we don’t write 13 = 1
since that’s not true, but ≡ means ”equivalent to”, which fits here).
Also, 26 ≡ 2 and 100 ≡ 4. We would in general write a ≡ b if a, b are the same points on
the clock.
Question 2.1.1. Give a mathematical characterization/formula of when two points are the
same on the clock.
If you said 12 | a − b, then congrats, you got it right. So a ≡ b on the clock when
12 | a − b. But there are more things than a clock in this world. How would the general
” ≡ ” be defined?
45
2. Modular Arithmetic Basics
a ≡ b (mod n)
if n | a − b.
Problem 2.1.2. Let a, n be fixed integers. Show that the set of integers b such that b ≡ a
(mod n) form an arithmetic progression. What is the common difference?
Problem 2.1.3. Show that the set of integers a such that a ≡ 0 (mod n) is the set of
multiples of a.
Question 2.2.1. In the set of integers a such that a ≡ b (mod n) for some fixed n, convince
yourself that the smallest non-negative element is the remainder when b is divided by n.
For example, for a ≡ 9 (mod 5), the smallest element is 4, which is the remainder when
9 is divided by 5.
In general, when we write a ≡ r (mod n), we try and keep the second number as the
remainder for simplicity. Thus,
12 ≡ 2 (mod 10)
120 ≡ 0 (mod 15)
11 ≡ 11 (mod 21)
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 46
2. Modular Arithmetic Basics
Problem 2.3.2. Show that the number of the classes modulo n is exactly n.
Question 2.4.1. We have 10 ≡ 1 (mod 3) and 8 ≡ 2 (mod 3). What is 10 + 8 (mod 3)? Is
it 1 + 2?
The answer is yes! To see why, write a = nx + r and b = ny + s. Then
a + b = n(x + y) + (r + s).
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 47
2. Modular Arithmetic Basics
Question 2.4.2. We have 10 ≡ 1 (mod 3) and 8 ≡ 2 (mod 3). What is 10 × 8 (mod 3)? Is
it 1 × 2?
Theorem 2.4.1 (Properties of Modulus). Let a, b, r, s be integers such that for a given
integer n, we have a ≡ r (mod n) and b ≡ s (mod n). Then
1. a + b ≡ r + s (mod n).
2. ab ≡ rs (mod n).
This is better than Theorem 1.3.1 to find the remainder since we can directly use the
second property here without having to completely multiply a = nx + r and b = ny + s.
Thus, how = helps us in linear equations and all, ≡ helps us in showing divisibility and
related stuff.
Problem 2.4.4. Show that if a ≡ b (mod n), then ka ≡ kb (mod n) for any integer k.
We can now destroy some problems from the last chapter that were challenging back
then:
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 48
2. Modular Arithmetic Basics
Some interesting results can be very easily derived using modular arithmetic:
Problem 2.4.7 (Very Important). Let f be a polynomial with integer coefficients. Show
that a − b | f (a) − f (b) for any integers a, b. This is the same as saying f (a + d) ≡ f (a)
(mod d).
Firstly, test some values of (p, q) and guess the answer. After that let’s try to solve it
systematically.
Directly expanding the right side won’t be so useful. Let’s take the equation modulo
something special. The most obvious choices are mod p and mod q. We can try both. Mod p
gives q ≡ −q 3 (mod p) and so p | q(q 2 + 1). Mod q gives p ≡ p3 (mod q) and so q | p(p2 − 1).
Not very useful.
However, here’s the trick. As p + q = (p − q)3 , we also have (p + q) | (p − q)3 . So,
(p − q)3 ≡ 0 (mod p + q). But p − q ≡ −2q (mod p + q) so (−2q)3 ≡ 0 =⇒ (p + q) | 8q 3 .
Now what we note is that if gcd(p + q, q) 6= 1, then gcd(p, q) = gcd(p + q, q) 6= 1 implies
p = q. In that case, however, we get p + p = (p − p)3 = 0, which is impossible.
So gcd(p+q, q) = 1. Hence in p+q | 8q 3 , we must have p+q | 8 because the q 3 contributes
nothing. So p + q ∈ {1, 2, 4, 8}. We can now manually list positive numbers that add to 8,
which are (1, 7), (2, 6), (3, 5), (4, 4). As p, q are primes, the only possible pair is (p, q) = (3, 5)
or (5, 3). Is this the answer you guessed?
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 49
2. Modular Arithmetic Basics
S = {0, a, 2a, . . . } = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, . . . }.
S = {0, a, 2a, . . . , 6a} = {0, 3, 6, 9, 12, 15, 18} = {0, 3, 6, 2, 5, 1, 4} (mod 7).
But we assumed gcd(a, p) = 1 at the start! Hence, we get p | (i − j) (why?). Is this possible?
Question 2.5.2. Try and find two integers i 6= j between 0, p − 1 such that p | i − j.
If you tried to, you would realize this is not possible. And the reason is simple, as
0 ≤ i 6= j < p, hence 0 < |i − j| < p. Thus, this is impossible, and we are done!
Now note that {0, a, 2a, . . . , (p − 1)a} has p elements, and all these are distinct. However,
since there are only p remainders possible, hence this set must be the set of ALL remainders!
For instance, in our example we saw that
{0, a, 2a, . . . , 6a} = {0, 3, 6, 9, 12, 15, 18} = {0, 3, 6, 2, 5, 1, 4} (mod 7),
and observe that the last set contains all the remainders mod 7. Cool, isn’t it!
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 50
2. Modular Arithmetic Basics
elements, the sum of squares of elements and the product of element, because these operation
don’t depend on the order. Out of all these, sum of elements is the most useful, and is very
strong in its applications. It should be the first thing you do on seeing equal sets!
So, in our context, we basically derived that S = {0, 1, 2 . . . , (p − 1)} modulo p. Note
here that the element 0 in S is 0 × a and in {0, 1, . . . , (p − 1)} is the first element. So we can
delete 0 from both the sets. Then we obtain:
Theorem 2.5.1 (Two Equal Sets). Let p be a prime and consider S = {1, 2, . . . , p − 1} to
be the set of non-zero remainder modulo p. Let a be any integer coprime to p.. Then
aS ≡ S (mod p).
Here, aS means the set obtained on multiplying each element of S by a. This gives us
many interesting results.
This gives
ap−1 · (p − 1)! ≡ (p − 1)! (mod p).
Now, gcd((p − 1)!, p) = 1, hence we can divide both the sides by (p − 1)! by Problem 2.4.8.
Hence ap−1 ≡ 1
Now comes the interesting part: Since a was any number coprime to p, hence we obtain
the famous Fermat’s Little Theorem:
Theorem 2.6.1 (Fermat’s Little Theorem). Let a be any number relatively prime to a prime
p. Then
ap−1 ≡ 1 (mod p).
This is a very powerful result. It helps us compute an for large n. Don’t forget the
”relatively prime” part of the theorem.
In fact, we also have the following:
ap ≡ a (mod p).
You can say we just multiplied both sides by a, so what’s special. The reason is that you
don’t need a, p relatively prime here!
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 51
2. Modular Arithmetic Basics
Problem 2.6.2. Let a, b be integers and p a prime. Show that p divides abp − ap b.
2.7 Inverses
Now let’s look at the definition of ”equal sets”. It means that for any integer 0 < b < p, we
can find an integer x such that
ax ≡ b (mod p).
In particular, if b = 1, then ax ≡ 1 (mod p).
What this means is if gcd(a, p) = 1, then there always exists a multiple of a which is 1
mod p.
Theorem 2.7.1 (Inverses). Let p be a prime and a be an integer coprime to p. Then there
always exists an integer x such that
ax ≡ 1 (mod p).
For instance, let’s try and find the inverse of 3 modulo 7. Write down the first (p − 1)
multiples (why first (p − 1)?) and check:
3 × 1 ≡ 3, 3 × 2 ≡ 6, 3×3≡2 (mod 7)
3 × 4 ≡ 5, 3 × 5 ≡ 1, 3×6≡4 (mod 7)
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 52
2. Modular Arithmetic Basics
Let me clear a possible confusion at this point. No, inverses do not always exist. For
example mod 6, we don’t have an inverse of 2 since:
2 × 1 ≡ 2, 2 × 2 ≡ 4, 2 × 3 ≡ 0 (mod 6)
2 × 4 ≡ 2, 2 × 5 ≡ 4 (mod 6)
We observe that 1 never appears, hence 2 does not have an inverse here. So modulo 6, we
cannot divide by 2.
Thus, the fact that an inverse always exists modulo a prime is very special (and useful).
I hope the above example helps you appreciate Theorem 2.7.1.
Lemma 2.7.1 (Inverses add like Fractions). Let b, d 6≡ 0 (mod p). Then for any a, c, we
have
a c ad + bc
+ ≡ a · b−1 + c · d−1 ≡ (ad + bc) · (bd)−1 ≡ (mod p)
b d bd
just like normal fractions.
If you truely believe that Lemma 2.7.1 is not obvious, then let me prove it for you.
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 53
2. Modular Arithmetic Basics
Lemma 2.7.2 (Inverses multiply like Fractions). Let b, d 6≡ 0 (mod p). Then for any a, c,
we have
a c ac
· ≡ (a · b−1 ) · (c · d−1 ) ≡ (ac) · (bd)−1 ≡ (mod p)
b d bd
just like normal fractions.
2 3 1
· = . (2.3)
3 8 4
Viewing these fractions as inverse, the left side modulo 7 is
2 3
· ≡ (2 · 3−1 ) · (3 · 8−1 ) = 3 · 3 ≡ 2 (mod 7)
3 8
On the other hand, the right side of of 2.3 is
1
≡ 4−1 ≡ 2 (mod 7).
4
Thus, 2.3 holds modulo 7 too.
Proving Lemma 2.7.2 is rather easy and so I leave it as an exercise.
Problem 2.7.2. Find the inverse of all {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} modulo 11.
Problem 2.7.3. Show that 0 does not have an inverse modulo p. What about p?
(If you say this is because 1/0 is not defined or is ∞, then that argument is true for
normal fractions, but not here. This proof fails modulo p because inverses aren’t exactly
division. Find a different proof.)
Problem 2.7.5. Prove that the inverse of an is the nth power of the inverse of a. That is,
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 54
2. Modular Arithmetic Basics
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} =⇒ (1, 1), (2, 6), (3.4), (5, 9), (7, 8), (10, 10). (2.4)
Wait, the pairs (1, 1), (10, 10) have the same elements. So now we ask when is a the inverse
of a. This is the same as saying a · a ≡ 1 (mod p), i.e. p | (a − 1)(a + 1). Hence p | (a − 1) or
p | (a + 1) (why?). This is the same as saying a ≡ 1 or a ≡ −1. So these are the only cases
in which a is the inverse of a.
Thus, if we ignore these bad pairs, then in any pair (a, b) we would have a 6= b. Further
every remainder mod p is in some pair. So, if we multiply ALL of them, we get something
very interesting:
2 · 3 · 4 . . . 9 = (2 · 6) · (3 · 4) · (5 · 9) · (7 · 8)
= 1 · 1 · 1 · 1 · 1 = 1 (mod 11).
Interesting right? This is a special case of a much more general result, the famous Wilson’s
theorem:
This is proved exactly in the same way as we did for 10!, that is pairing up with inverses.
This is a very cool theorem and probably the most used one when it comes to factorials
modulo something.
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 55
2. Modular Arithmetic Basics
Actually, I would be lying to you if I said this was Wilson’s theorem. There’s a bit more
to it. It also says that if n is any natural satisfying (n − 1)! ≡ −1 (mod n), then n must
be a prime. Woah. This is one of the rare criterions/formulas that we have for testing if a
number is prime. So if you want to check if n is a prime, then you just calculate (n − 1)!
and check if it’s −1 modulo n.
Sadly, finding (n − 1)! is hard and not feasible even for a computer for large values of n.
To get an idea of how large factorials get, 100! has 158 digits. Nonetheless, I will write the
full theorem here:
(n − 1)! ≡ −1 (mod n)
Note here the ”if and only if” means two things: if (n − 1)! ≡ −1 (mod n), then n is a
prime, and also if n is a prime, then (n − 1)! ≡ −1 (mod n).
Practice Problems
Problem 2.8.1. Prove that if n is any natural satisfying (n − 1)! ≡ −1 (mod n), then n
must be a prime.
Problem 2.8.2. Let p be a prime. Show that the remainder when (p − 1)! is divided by
p(p − 1) is p − 1.
gcd(n! + 1, (n + 1)!).
Theorem 2.9.1 (General Equal Sets). Let n be any integer. Let S be the set of integers less
than n and relatively prime to n. Let a be any integer coprime to n. Then
aS ≡ S (mod n).
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 56
2. Modular Arithmetic Basics
The proof is exactly the same. Note that S is not {1, 2, 3, . . . , n − 1}. It is only the set
of integers coprime to n. Note that 1 ∈ S always holds (why?).
For example, if n = 15 and a = 4, then S = {1, 2, 4, 7, 8, 11, 13, 14}. So
4S ={4, 8, 16, 28, 32, 44, 52, 56}
≡{4, 8, 1, 13, 2, 14, 7, 11} (mod 15).
Check that the second set is the same as S. Now we will do exactly what we did before to
obtain general Fermat’s Little Theorem and general Inverses. Before we move on, let me
just clarify the following:
Definition 2.9.1. The set S is called a reduced residue system modulo n.
The product here means the product of all numbers between 1, n that are coprime to n.
Again, we can cancel this product from both the sides using Problem 2.4.8 since it is
coprime to n (why?). Thus we get
a|S| ≡ 1 (mod n) for all a coprime to n (2.5)
This is cooler than Fermat’s little theorem since we have a general mod here.
Question 2.10.1. Check that when n = p is a prime, we have S = {1, 2, 3, . . . , p − 1} and
hence |S| = p − 1 in that case. Thus confirm that this result implies Fermat’s Little theorem
and hence is more general.
For instance, when n = 15, we have S = {1, 2, 4, 7, 8, 11, 13, 14}, and so |S| = 8. So, for
any a coprime to 15 we have a8 ≡ 1 (mod 15). For instance check that 118 ≡ 1 (mod 15).
However, this would be better if we had a formula for |S| in general. Turns out, mathe-
maticians left nothing hanging. Here’s what we are looking for:
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 57
2. Modular Arithmetic Basics
Definition 2.10.1. Let n be a positive integer. The function ϕ(n) is called Euler’s totient
function, and it denotes the number of positive integers less than n that are coprime to it.
Question 2.10.2. Find ϕ(2), ϕ(3), ϕ(4), ϕ(5), ϕ(6), ϕ(7), ϕ(8), ϕ(9) and ϕ(10).
Theorem 2.10.1 (Euler’s Totient Function). Let n = pα1 1 pα2 2 . . . pαk k be its prime factoriza-
tion. Then
1 1 1
ϕ(n) = n 1 − 1− ... 1 − .
p1 p2 pk
The way I like to remember it is that for each prime p, reduce the power of p by 1 and
multiply by (p − 1).
This formula is derived by a lemma, that is incredibly useful in itself so I will mention it
here:
ϕ(mn) = ϕ(m)ϕ(n).
People often forget the coprime part. Don’t make the same mistake!
Back to our previous discussion, 2.5 gives us the so called Euler’s Theorem:
Theorem 2.10.2 (Euler’s Theorem). Let n ≥ 2 be an integer and a be any integer coprime
to n. then
aϕ(n) ≡ 1 (mod n).
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 58
2. Modular Arithmetic Basics
Problem 2.10.3. What happens if gcd(a, n) 6= 1? Does there exist any integer m such that
am ≡ 1 (mod n)?
Theorem 2.11.1 (General Inverses). Let n ≥ 2 be any positive integer. Then every number
a with gcd(a, n) = 1 has an inverse, that is a number x such that
ax ≡ 1 (mod n).
We write x = a−1 .
This is an amazing theorem. We can now divide modulo any number, well almost. In
fact, we have only proved that if gcd(a, n) = 1, then a has an inverse. What if gcd(a, n) 6= 1?
Does an inverse exist in that case?
Turns out the answer is no. Let’s take an example. Suppose n = 9 and a = 3. Then
3 × 1 ≡ 3, 3 × 2 ≡ 6, 3 × 3 ≡ 0 (mod 9)
3 × 4 ≡ 3, 3 × 5 ≡ 6, 3 × 6 ≡ 0 (mod 9)
3 × 7 ≡ 3, 3 × 8 ≡ 6, 3 × 9 ≡ 0 (mod 9)
Lemma 2.11.1 (Inverses don’t always exist). If n is a natural number, and a is an integer,
then a has an inverse modulo n if and only if gcd(a, n) = 1. In particular, if gcd(a, n) > 1,
then a does not have an inverse.
Problem 2.11.2. Does there exist an inverse for 5 modulo 10? What about 4?
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 59
2. Modular Arithmetic Basics
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 60
2. Modular Arithmetic Basics
Example 2.12.1
Then,
agcd(m,n) ≡ 1 (mod d).
This is very useful, and is just a modular analgoue of Example 1.10.4 (see comments
below). However, proving this using modular arithmetic is easier. Using Bézout’s lemma,
we find x, y ∈ Z such that mx + ny = gcd(m, n). So
Comment 2.12.1: Let’s try to prove Example 1.10.4 using this. Suppose d =
gcd(am −1, an −1). To show d = agcd(m,n) −1, we show agcd(m,n) −1 | d and d | agcd(m,n) −1.
The former is easier to prove: just note that agcd(m,n) − 1 divides both am − 1, an − 1,
hence it divides their gcd d. Next, since
Example 2.12.2
For example, 6 ≡ 2 (mod 4) =⇒ 3 ≡ 1 (mod 2), not 3 ≡ 1 (mod 4). In other words,
if we want to cancel out a common factor of a, b, we would also have to reduce the thing
inside mod. In particular, note that ka ≡ kb (mod n) implies a ≡ b (mod n) if and only if
gcd(k, n) = 1 (this is Problem 2.4.8).
Proof. We have n | d(a − b). Now, the d contributes only to gcd(d, n) in this divisibility.
Hence, n/ gcd(d, n) divides a − b, as needed.
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 61
2. Modular Arithmetic Basics
A more formal argument would be to write n = gn∗ , d = gd∗ with g = gcd(n, d). Then
n∗ | d∗ (a − b) but since n∗ , d∗ are coprime, hence n∗ | a − b (by Euclid’s lemma, Example
1.8.2).
Now, if you try to take examples, you will observe that p divides all the binomial coefficient
above. This is in fact true and a very useful result:
p
p| ∀1 ≤ k ≤ p − 1.
k
Problem 2.12.1. Use Freshman’s dream and induction to prove Fermat’s Little Theorem.
Comment 2.12.2 (Just some facts): The result (a + b)p = ap + bp is not true over
integers, but is true over integers modulo p. We often denote the set of integers modulo
p by Z/pZ or Fp (we will use the second notation in this book). If we think of Fp as a
”structure”, i.e. a system of certain numbers, then this identity holds over this system.
Turns out there are more interesting and general systems over which this holds true.
However, they are beyond the scope of this book.
Also, if we define a function σ over Fp by saying σ(t) = tp , then we just showed
σ(a + b) = σ(a) + σ(b). The function σ is called a Frobenius endomorphism and is
also defined over general systems about which we talked above.
Since I mentioned a very convenient notation in the comments, I would highlight it here
too:
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 62
2. Modular Arithmetic Basics
Here, it is important to note that p is a prime. Writing F10 is an incorrect use of the
notation. However, you can use Z/10Z to denote the set of integers modulo 10. The notations
might seem obscure at this point, however it is explained better using the notion of fields
and quotient rings from abstract algebra. So if you fight the notation right now, you would
be contradicting yourself a few years later in college. So just use the notation blindly, as it
is convenient.
Example 2.12.4
for any x.
Just see that for any x, one of x, x − 1, . . . , x − (p − 1) is 0 modulo p. Hence, the right
side becomes 0 modulo p. What about the left side? Well, that is zero for any residue
too by Fermat’s Little Theorem! Hence, if we define the polynomials f (x) = xp − x and
g(x) = x(x − 1) . . . (x − (p − 1)), then f (x) ≡ g(x) (mod p) for any x.
If you see carefully, this doesn’t say that the polynomials f (x), g(x) are the same (i.e.
have the same coefficients modulo p), it merely says it would give the same value. For
instance, xp ≡ x (mod p) is true for all x value-wise, but the polynomials xp and x are
obviously different.
So the natural question now is if f (x), g(x) are equal as polynomials too. We answer this
question in the special section of the chapter ”Modular Arithmetic Advanced”.
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 63
2. Modular Arithmetic Basics
And that’s how we get the result modulo p. What about p2 though? We now only need to
show
p−1
2
X 1
≡0 (mod p).
i=1
i(p − i)
Well, at this point we must try something new. First of all, since (p − i) ≡ −i (mod p),
hence each denominator above can be replaced by −i2 . Now, pairing did work well for us,
however it complicated one thing, the number of terms. Luckily, we can multiply by 2 to
restore (why?). So
p−1
2 p−1
X 1 X1
2 ≡ (mod p).
i=1
i(p − i) i=1
i2
Since we are dealing with terms of the form 1/i = i−1 , hence there is no way we can miss
inverses; they can easily help us get rid of the fractions. However, we don’t know what i−1
would be. Let’s fall back to our example of p = 7. We can calculate
1 1 1 1 1 1
2
+ 2 + 2 + 2 + 2 + 2 ≡ 12 + 42 + 52 + 22 + 32 + 62 (mod 7).
1 2 3 4 5 6
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 64
2. Modular Arithmetic Basics
We immediately observe that each residue appears in the sum. Luckily, observing this
fact is harder than proving it. The proof just follows since {1−1 , 2−1 , . . . , (p − 1)−1 } ≡
{1, 2, . . . , p − 1} (mod p) (why? If i−1 ≡ j −1 , then cross multiplying gives i ≡ j (mod p)).
So
1 1 1
2
+ 2
+ 2
≡ −(12 + 22 + · · · + (p − 1)2 )
1 2 (p − 1)
(p − 1)p(2p − 1)
=− (mod p),
6
where we used the sum of first n squares formula. We must show the above is 0 mod p.
But since p > 3, hence gcd(p, 6) = 1. Thus p has no contribution in making p(p−1)(2p−1) 6
an
(p−1)(2p−1)
integer, and hence 6
is also an integer. So the right side is p times an integer, which
is thus 0 (mod p). So we are done!
Too much discussion happened here. Let’s try and neatly summarize our argument into
one equation! (note where we used = and where ≡, denoting where we used algebraic facts
vs number theoretic facts)
p−1
X 1 X 1 1
2 = +
i=1
i i=1
i p−i
p−1
X p
=
i=1
i(p − i)
p−1
X −1
≡p
i=1
i2
p−1
X
≡ −p i2
i=1
2
p (p − 1)(2p − 1)
=− ≡0 (mod p2 ).
6
Can you link each line above with our discussion?
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 65
2. Modular Arithmetic Basics
is eventually constant.
(The tower of exponents is defined by a1 = 2, ai+1 = 2ai . In other terms, we start
22 4
working from the top, for instance 22 = 22 = 216 = 65536.)
For convenience, define ai to be the ith term of the sequence. Firstly, assume n is odd.
Since 2ϕ(n) ≡ 1 (mod n), hence if we can ak ≡ ak+1 ≡ ak+2 ≡ . . . (mod ϕ(n)) for some k,
we would get
2ak ≡ 2| a{z
|{z}
ak+2
} ≡ 2| {z } ≡ . . . (mod n),
k+1
which is what we want. So if we can show if the sequence hai i eventually becomes constant
modulo ϕ(n), we can conclude that it eventually becomes constant modulo n as well. So we
have to prove the same problem for ϕ(n) instead.
The key observation now is ϕ(n) < n. So we have reduced the problem from case n to a
smaller case. Hence, we can use (strong) induction! Here’s how it goes:
Proof. Check the base case of n = 1, and assume the result till some n − 1. If n is odd, we
can reduce the problem to smaller case ϕ(n), for which it is true by the induction hypothesis.
We just have to manage n even now. In this case, we try to eliminate the ”even part” of n
and work. So if n = 2m n∗ with n∗ odd, then pick k large enough such that ak > m. So
and again use the induction hypothesis as n∗ < n (here we used the result from Example
2.12.2).
Example 2.13.2
Given
34! = 295232799039a041408476186096435b0000000,
in decimal representation, find the numbers a and b.
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 66
2. Modular Arithmetic Basics
Proof. We know that 9|34!. Also we know from the rule of divisibility by 9 that for all
naturals n, n ≡ S(n) (mod 9), where S(n) is the sum of digits of n. Hence, 9|a + b + 136
and so a + b ∈ {8, 17}, as 0 ≤ a, b ≤ 9.
We also know that 11|34!. Also, a number modulo 11 is congruent to the alternating sum
of the digits in the number, read from left to right. Therefore, 11|(77+a)−(59+b) = 18+a−b
and so a − b ∈ {−7, 4} as 0 ≤ a, b ≤ 9.
Now, note that a + b and a − b have the same parity. Hence, the only possibilities are
(a + b, a − b) = (8, 4) and (17, −7), and the two cases yield (a, b) = (6, 2) or (a, b) = (5, 12),
respectively. But since a, b ≤ 9, hence the second case is impossible. Thus, (a, b) = (6, 2)
proving the claim.
Proof. Suppose d = gcd(ab+1, bc+1, ca+1). Then ab, bc, ca ≡ −1 (mod d), and so ab−bc =
b(a − c) ≡ 0 (mod d). Now if b, d have a common factor, say p > 1, then p | d | ab + 1.
Combining with p | b, we get p | ab + 1 − b(a) = 1, a contradiction. Hence b, d are coprime
and so d | a − c.
Similarly, d | a − b, b − c. Thus, a ≡ b ≡ c (mod d). Now, assume without loss of
generality that a > b > c (strict inequalities since they are given to be distinct). Hence
a ≥ b + d ≥ c + 2d. So
a+b+c
a + b + c ≥ 3a + 3d ≥ 3d =⇒ ≥ d.
3
Hence we are done.
Not all problems are nice and sweet, some may involve simple ideas with which you work
a lot, typically means a lot of case work. For instance the following:
Find all natural numbers n for which there exist primes p and q such that the following
conditions are satisfied:
1. p + 2 = q, and
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 67
2. Modular Arithmetic Basics
Proof. We will show that such primes exist if and only if n ∈ {1, 3}. By these conditions,
we have that (p, p + 2, p + 2n , p + 2n + 2) are all primes.
Clearly, p 6= 2 because else q = 4, which is not a prime. Let us assume for the moment
that p 6= 3. Thus, p ≥ 5.
1. If n is even, then (p, p + 2, p + 2n , p + 2n + 2) ≡ (p, p + 2, p + 1, p) (mod 3) and so at
least one of p, p + 2, or p + 2n is divisible by 3. This is clearly false since we assumed
these numbers are primes and p ≥ 5.
2. If n is odd, then (p, p + 2, p + 2n , p + 2n + 2) ≡ (p, p + 2, p + 2, p + 1) (mod 3) and so at
least one of p, p + 2, or p + 2n + 2 is divisible by 3, and since they are all primes, one
of them must be 3. This is again a contradiction because we assumed p ≥ 5.
Hence, we must have p = 3 and (3 + 2n , 5 + 2n ) is a pair of primes. It is easy to see that
this condition is satisfied for n = 1 and n = 3 but not for n = 2. We will show that there is
no n > 3 that satisfies this condition. First, notice that if n > 3, then
5 + 2n > 3 + 2n > 13.
If n is even, say n = 2z, then 5 + 2n = 5 + 4z ≡ 5 + 1 ≡ 0 (mod 3), but since 5 + 2n > 3,
this number cannot be a prime, a contradiction. Thus, n is odd. Set n = 2k + 1 for some
integer k.
If k is even, then
3 + 2n = 3 + 2 · 2n−1 = 3 + 2 · 4k
≡ 3 + 2 · (−1)k
≡ 3 + 2 ≡ 0 (mod 5),
which is a contradiction because 3 + 2n > 5. So, k is odd. Set k = 2r + 1 for some integer
r, and so n = 4r + 3.
If r ≡ 1 (mod 3), write r = 3z + 1. Then, we have
5 + 2n = 5 + 24r+3 = 5 + 8 · 16r
≡ 5 + 2r
≡ 5 + 23z+1
≡ 5 + 2 · 8z
≡ 5 + 2 ≡ 0 (mod 7),
and we get a similar contradiction to the ones previous cases.
If r ≡ 2 (mod 3), say r = 3z + 2, then
3 + 2n = 3 + 24r+3 = 3 + 8 · 16r
≡ 3 + 2r
≡ 3 + 23z+2
≡ 3 + 4 · 8z
≡ 3 + 4 ≡ 0 (mod 7),
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 68
2. Modular Arithmetic Basics
a contradiction.
This means that 3|r. Write r = 3s for some integer s so that n = 12s + 3. Thus,
5 + 2n = 5 + 212s+3
s
= 5 + 8 · 212
≡ 5 + 8 ≡ 0 (mod 13),
a contradiction.
Hence, we have exhausted all the possibilities and so n = 1 and n = 3 are the only
possible solutions.
Example 2.13.5
Let P be the set of all prime numbers over naturals. Let M be a subset of P with at
least 3 elements. Choose any proper subset A of M. Consider the number
Y
nA := −1 + p
p∈A
We start by trying to manually show each prime is in M, at least for as many primes as
we can. Firstly, M has at least 3 elements. So choose an odd prime p ∈ M. Then n{p} is
even and so 2 ∈ M.
Next, if a prime p of the form 3k + 1 lies in M, then n{p} is divisible by 3. Otherwise
there exists a prime p of the form 3k + 2 and so n{2,p} = −1 + 2p is divisible by 3. Thus, in
either case we get that 3 ∈ M.
Then n{2,3} = 5 implies that 5 ∈ M. Also, n{3,5} =⇒ 7 ∈ M. The problem has the same
construction as that of Euclid’s. So this problem is screaming at us to try to do what he
did, show that the ”set” is infinite; in case the set of primes, and in our case the set M.
Claim. M is an infinite set
Proof. Assume on the contrary, and set M = {p1 , p2 , · · · pn }. We can’t directly consider nM
since the subset we choose must be a proper subset.
Hence, choose S = {p1 , p2 , · · · , pi−1 , pi+1 , · · · , pn }, i.e. we have removed pi from M. Let
P be the product of the elements of M. Then every factor of nS must be in M, and so we
must have
P
− 1 = pai for some a
pi
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 69
2. Modular Arithmetic Basics
This is true because gcd(nS , pj ) = 1 for all j 6= i. Now this holds for all primes pi ∈ M. We
act greedily and choose pi = 2. Then
P
− 1 = 2a
2
Note that 7|P since 7 ∈ M. Hence considering this equation modulo 7 yields
2a ≡ −1 (mod 7)
nA ≡ pq−1
1 −1≡0 (mod q)
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 70
2. Modular Arithmetic Basics
Problem 2.14.3 (IMO 1979/1). Let a and b be natural numbers such that
a 1 1 1 1 1
= 1 − + − + ··· − + .
b 2 3 4 1318 1319
Prove that a is divisible by 1979. (Note: 1979 is a prime) Hints: 350 407
Problem 2.14.4 (RMO 2016 P62 ). Let {a1 , a2 , a3 , . . . , } be a strictly increasing sequence
of positive integers in an arithmetic progression. Prove that there is an infinite subsequence
of the given sequence whose terms are in a geometric progression. Hints: 288
Problem 2.14.5. Let f (x) be a polynomial with integer coefficients. Show that there does
not exist a N such that f (x) is a prime for all x ≥ N. In other words, f (x) is not eventually
always a prime. This problem shows that prime numbers don’t follow any polynomial pattern
either. Hints: 308
Problem 2.14.6 (IMO 2005/4). Determine all positive integers relatively prime to all the
terms of the infinite sequence
an = 2n + 3n + 6n − 1, n ≥ 1.
Hints: 130
Problem 2.14.7 (IMO 1986/1). Let d be any positive integer not equal to 2, 5, or 13.
Show that one can find distinct a and b in the set {2, 5, 13, d} such that ab − 1 is not a perfect
square. Hints: 379 84 9
Problem 2.14.8. Let a and b be two relatively prime positive integers, and consider the
arithmetic progression a, a + b, a + 2b, a + 3b, . . .
1. (G. Polya) Prove that there are infinitely many terms in the arithmetic progression
that have the same prime divisors. Hints: 265 156 349
2. Prove that there are infinitely many pairwise relatively prime terms in the arithmetic
progression. Hints: 487 37
2
Regional Mathematical Oolympiad (the AIME of India)
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 71
2. Modular Arithmetic Basics
Problem 2.14.11 (Sierpiński). Prove that for any positive integer s, there is a positive
integer n whose sum of digits is s and s | n. Hints: 200 397 197 Sol: pg. 280
Problem 2.14.12 (IMO Shortlist 2001 N4). Let p ≥ 5 be a prime number. Prove that
there exists an integer a with 1 ≤ a ≤ p − 2 such that neither ap−1 − 1 nor (a + 1)p−1 − 1 is
divisible by p2 . Hints: 204 218 467 66
Problem 2.14.14 (Balkan 2016/3). Find all monic polynomials f with integer coefficients
satisfying the following condition: there exists a positive integer N such that p divides
2(f (p)!) + 1 for every prime p > N for which f (p) is a positive integer. (A monic polynomial
has a leading coefficient equal to 1.) Hints: 341 321 67 436
Problem 2.14.15 (Iran 3rd round 2017 Numbers theory final exam P1). Let x and
y be integers and let p be a prime number. Suppose that there exist relatively prime positive
integers m and n such that
xm ≡ y n (mod p)
Prove that there exists an unique integer z modulo p such that
x ≡ zn (mod p) and y ≡ z m (mod p).
Hints: 20 365 108 Sol: pg. 281
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 72
2. Modular Arithmetic Basics
Problem 2.14.16 (IMO Shortlist 2015 N3). Let m and n be positive integers such that
m > n. Define
m+k
xk =
n+k
for k = 1, 2, . . . , n + 1. Prove that if all the numbers x1 , x2 , . . . , xn+1 are integers, then
x1 x2 . . . xn+1 − 1 is divisible by an odd prime. Hints: 104 328 192 471 Sol: pg. 281
Problem 2.14.17 (ELMO 2019/5). Let S be a nonempty set of positive integers such
that, for any (not necessarily distinct) integers a and b in S, the number ab + 1 is also in S.
Show that the set of primes that do not divide any element of S is finite. Hints: 233 30 10 480
Sol: pg. 282
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 73
2. Modular Arithmetic Basics
Another interesting property is Problem 2.14.2. In this special section, we discuss more.
Lucas’s Theorem
This theorem is very useful in understanding how binomial coefficients behave modulo primes.
But first, we do a problem:
Example 2.14.1
Show that the coefficients of a binomial expansion (a+b)n where n is a positive integer,
are all odd, if and only if n is of the form 2k − 1 for some positive integer k.
if and only if n = 2k − 1. (In other words, this tells us excatly which rows of the Pascal
triangle have all terms odd).
n
Suppose that n = 2k − 1. We want to show m is always odd. Since k is arbitrary,
our first bets should be on induction. Assume the result till k − 1 and let’s prove it for k.
Since we want to look at all the binomial coefficients at once, it is best to use the binomial
theorem, and the simplest expression is (X + 1)n . So
X n
X m = (X + 1)n
0≤m≤n
m
k −1
= (X + 1)2
k−1
= (X + 1)2·(2 −1)+1
k−1
2
= (X + 1)2 −1 · (X + 1).
So, if we let t = 2k−1 −1, then (X +1)t = X t +at−1 X t−1 +· · ·+a1 X +1, where a1 , a2 , . . . , at−1
are all odd (why?). So
2 2
2k−1 +1
(X + 1) · (X + 1) = X t + at−1 X t−1 + · · · + 1 (X + 1)
At this stage, we can simply look at the coefficient of X m (for any m) in the above expansion
and check it will be odd. For instance, the coefficient of X n−1 = X 2t would be (1 + 2at−1 ) (of
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 74
2. Modular Arithmetic Basics
course, you would need to elaborate more on why the coefficients wouldbe odd in a proper
n
proof, but I leave the details to the interested reader). However, since m was the coefficient
m
of X , hence this binomial coefficient is odd.
Thus, we have proven one direction of the problem. The other direction is more tricky,
n
which asks us to show that m is always odd for 1 ≤ m ≤ n implies n is of the form 2k + 1.
So I will just give the theorem’s statement:
Theorem 2.14.1 (Lucas’s Theorem). For non-negative integers m and n and a prime p,
the following congruence relation holds:
Y k
n ni
≡ (mod p)
m i=0
mi
where
n = nk pk + nk−1 pk−1 + · · · + n1 p + n0
and
m = mk pk + mk−1 pk−1 + · · · + m1 p + m0
n
are the base p expansions of m and n respectively. This uses the convention that m = 0 if
n < m.
ni
Note that this finishes the problem above easily, since we must have m i
must always
be odd impliying that ni = 1, which corresponds to n be of the form 11 . . . 1(2) = 2k − 1 for
some k.
The cleanest proof for this uses generating functions, with a method slightly similar to
the one we found for the previous problem. However, there are some technical details you
need to know to fully appreciate the proof, so you can find it in the special section of the
chapter: Integer Polynomials.
This theorem is very useful in proving some binomial identities. Here are some problems
to try:
Problem 2.14.19. Find a formula for the number of entries in the nth row of Pascal’s
triangle that are not divisible by p, in terms of the base-p expansion of n.
2 Interesting Lemmas
In this part, we talk about two interesting properties related to the binomial coefficients.
p k
They give nice formulas for k and p .
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 75
2. Modular Arithmetic Basics
where b•c represents the floor function (aka the greatest integer function).
This is not very tricky to prove, and very easy if you use Lucas’s theorem. So the proof
is left as an exercise. Another useful property is
Problem 2.14.20 (ELMO 2009/6). Let p be an odd prime and x be an integer such that
p | x3 − 1 but p - x − 1. Prove that
x2 x3 xp−1
p | (p − 1)! x − + − ··· − .
2 3 p−1
Problem 2.14.21 (IMO Shortlist 2011 N7). Let p be an odd prime number. For every
integer a, define the number
a a2 ap−1
Sa = + + ··· + .
1 2 p−1
Let m, n ∈ Z, such that
m
S3 + S4 − 3S2 = .
n
Prove that p divides m.
c
Aditya Khurmi 2020. All rights reserved. (Published on 11/2020) 76