ECM3704 Number Theory 2015-2016: 1 Divisibility and Primes
ECM3704 Number Theory 2015-2016: 1 Divisibility and Primes
ECM3704 Number Theory 2015-2016: 1 Divisibility and Primes
Henri Johnston
H.Johnston@exeter.ac.uk
10th December 2015
Abstract
These notes are based on the lecture notes and handouts of Dr
Robin Chapman who gave this course in 2013–2014. The lectures
were originally typed up by Oliver Bond who sat in on the course that
year. The notes have since been completely rewritten, but consider-
able thanks is clearly due to both Robin and Oliver. All errors are my
own; please do email me if you find any.
1
(iv) a | b implies an | bn (multiplication property),
(v) an | bn and n 6= 0 implies a | b (cancellation property),
(vi) 1 | n (1 divides every integer),
(vii) n | 0 (every integer divides 0),
(viii) 0 | n implies n = 0 (zero divides only zero),
(ix) a | b and b 6= 0 implies |a| ≤ |b| (comparison property),
(x) a | b and b | a implies |a| = |b|, i.e., a = ±b.
Proof. Checking the properties is straightforward. We check (iii) and leave
the others as an exercise. Since a | b and a | c there exist m, n ∈ Z such that
b = an and c = am. For any x, y ∈ Z we have
xb ± yc = xan ± yam = a(xn ± ym).
So we have found an integer q = xn ± ym such that xb ± yc = aq. Thus
a | (xb ± yc), as desired.
2
However, since 0 ≤ r, r0 < b we must have |r0 − r| < b, which gives a
contradiction.
3
Combining Theorem 1.6, Corollary 1.7 and Definition 1.8, we have:
Bezout’s Identity. Given a, b ∈ Z there exist (non-unique) x, y ∈ Z such
that gcd(a, b) = ax + by.
Proposition 1.9. Let a, b, c ∈ Z. The gcd has the following properties:
(i) gcd(a, b) = gcd(b, a) (commutative law),
(ii) gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) (associative law),
(iii) gcd(ac, bc) = |c| gcd(a, b) (distributive law),
(iv) gcd(a, 1) = gcd(1, a) = 1,
(v) gcd(a, 0) = gcd(0, a) = |a|,
(vi) c | gcd(a, b) if and only if c | a and c | b,
(vii) gcd(a + cb, b) = gcd(a, b).
Proof. Checking properties (i),(ii),(iv),(v) & (vi) is straightforward and is
left as an exercise. (For property (vi) use Bezout’s Identity and the linearity
property of divisibility).
We prove (iii). Let d = gcd(a, b) and let e = gcd(ac, bc). We wish to show
that e = |c|d. By property (vi), cd | e = gcd(ac, bc) since cd | ac and cd | bc.
By Bezout’s Identity, there exist x, y ∈ Z such that d = ax + by. Then
cd = acx + bcy.
But e | ac and e | bc and so by linearity of divisibility we have e | cd.
Therefore |e| = |cd|, i.e., e = |c|d.
Finally, we prove (vii). Let e = gcd(a + bc, b) and f = gcd(a, b). Then
e | (a + bc) and e | b. Thus by linearity of divisibility e | a. Hence e | a and
e | b so by property (vi), we have e | f . Similarly, f | a and f | b so again by
linearity of divisibility f | (a + bc). Thus f | (a + bc) and f | b and so again
by property (vi), we have f | e. Therefore e | f and f | e and f, e ≥ 0 so we
conclude that e = f .
Remark 1.10. Note that gcd(a, b) = 0 if and only if a = b = 0. Otherwise
gcd(a, b) ≥ 1.
Theorem 1.11 (Euclid’s Lemma). Let a, b, c ∈ Z. If a | bc and gcd(a, b) = 1
then a | c.
Proof. Suppose that a | bc and gcd(a, b) = 1. By Bezout’s Identity there
exist x, y ∈ Z such that 1 = ax + by. Hence c = acx + bcy. But a | acx and
a | bcy, so a | c by the linearity property of divisibility.
Theorem 1.12 (Solubility of linear equations in the integers). Let a, b, c ∈ Z.
The equation
ax + by = c
is soluble with x, y ∈ Z if and only if gcd(a, b) | c.
4
Proof. Let d = gcd(a, b). Then d | a and d | b so if there exist x, y ∈ Z such
that c = ax + by then d | c by linearity of divisibility. Now suppose that d | c.
Then we can write c = qd for some q ∈ Z. By Bezout’s Identity there exist
x0 , y 0 ∈ Z such that d = ax0 + by 0 . Hence c = qd = aqx0 + bqy 0 and so x = qx0
and y = qy 0 gives a suitable solution.
r0 = r1 q1 + r2 , 0 < r2 < r1 ,
r1 = r2 q2 + r3 , 0 < r3 < r2 ,
..
.
rn−2 = rn−1 qn−1 + rn , 0 < rn < rn−1 ,
rn−1 = rn qn + rn+1 , rn+1 = 0.
gcd(ri , ri+1 ) = gcd(ri+1 qi+1 + ri+2 , ri+1 ) = gcd(ri+2 , ri+1 ) = gcd(ri+1 , ri+2 ).
841 = 160 × 5 + 41
160 = 41 × 3 + 37
41 = 37 × 1 + 4
37 = 4×9+1
4 = 1 × 4 + 0.
5
Hence gcd(841, 160) = 1 (i.e. they are coprime) and working backwards gives:
1 = 37 × 1 − 4 × 9
= 37 × 1 − (41 − 37) × 9
= 37 × 10 − 41 × 9
= (160 − 3 × 41) × 10 − 41 × 9
= 160 × 10 − 41 × 39
= 160 × 10 − (841 − 160 × 5) × 39
= −39 × 841 + 205 × 160.
Note that such a solution is not unique. For example, we will also have
1 = (160 − 39) × 841 + (205 − 841) × 160 = 121 × 841 − 636 × 160.
Thus we set xi := xi−2 − xi−1 qi−1 and yi := yi−2 − yi−1 qi−1 . In other words,
for i ≥ 2 we define (xi , yi ) recursively by
6
Example 1.16. We compute gcd(841, 160) and express it as a linear combin-
ation of 841 and 160 using the Extended Euclidean Algorithm.
1.6 Primes
Definition 1.17. Prime and composite numbers in N:
(i) A number p ∈ N with p > 1 is prime if and only if its only divisors are
1 and p (i.e. if n ∈ N and n | p then n = 1 or n = p).
(ii) A number n ∈ N with n > 1 is composite if and only if it is not prime
(i.e. n = ab for some a, b ∈ N with a, b > 1).
Note that n = 1 is neither prime nor composite.
Proposition 1.18. If n ∈ N with n > 1 then n has a prime factor.
Proof. We use strong induction, i.e., we prove that if for all m ∈ N with
1 < m < n, m has a prime factor, then n has a prime factor.
Case (i): if n is prime, then n is a prime factor of n.
Case (ii): If n is composite then n = ab where a, b ∈ N with a, b > 1. So
1 < a < n. By the induction hypothesis, there is a prime p with p | a. Hence
p | a and a | n, so by the transitivity property of divisibility p | n.
Proposition 1.19. If n ∈ N with n > 1 then we can write n = p1 p2 · · · pk
where k ∈ N and p1 , . . . , pk are (not necessarily distinct) primes.
Proof. If n is prime then the result is clear. So suppose that n is composite.
Then by Proposition 1.18 n has a prime factor, i.e., n = p1 n1 where p1
is prime and n1 ∈ N with n1 > 1. If n1 is prime, we are done. If n1 is
composite, it has a prime factor p2 and we can write n1 = p2 n2 where n2 ∈ N
with n2 > 1. If n2 is prime, we are done, otherwise we take out another
prime factor and keep on going. The process does eventually terminate since
n > n1 > n2 > · · · > 1. Hence after at most n steps we obtain a prime
factorisation of n.
7
Example 1.20. We have 666 = 3 × 222 = 3 × 2 × 111 = 3 × 2 × 3 × 37.
Theorem 1.21. There are infinitely many primes.
Euclid’s proof. For a contradiction, assume {p1 , p2 , . . . , pn } is a complete list
of primes. Consider N := 1 + p1 p2 . . . pn ∈ N. Then N > 1 so by Proposition
1.18, N has a prime factor p. However, every prime is supposedly one of
p1 , . . . , pn , so p = pi for some i. Then p = pi | (p1 . . . pn ), so p | (N − 1).
However, we also have p | N and we can write 1 = N − (N − 1), so p | 1,
which is a contradiction.
8
Proof. Let k = vp (m) and ` = vp (n). Then we can write m = pk m0 where
p - m0 and similarly n = p` n0 where p - n0 . Then mn = pk+` m0 n0 . By Euclid’s
Lemma for Primes, p - m0 n0 . Therefore vp (mn) = k + `.
Theorem 1.28 (The Fundamental Theorem of Arithmetic). Let n ∈ N with
n > 1. Then
(i) (Existence) The number n can be written as a product of primes.
(ii) (Uniqueness) Suppose that
n = p1 · · · pr = q 1 · · · qs
where each pi and qj is prime. Assume further that
p1 ≤ p2 ≤ · · · ≤ pr and q1 ≤ q2 ≤ · · · ≤ qs .
Then r = s and pi = qi for all i.
Proof. The existence of a factorisation into primes is just Proposition 1.19.
Thus it remains to show uniqueness. Let ` be any prime. Then by Lemma
1.27 we have
v` (n) = v` (p1 · · · pr ) = v` (p1 ) + · · · + v` (pr ).
However,
1 if ` = pi ,
v` (pi ) =
0 6 pi .
if ` =
Therefore
v` (n) = # of i for which ` = pi
= # of times ` appears in the factorisation n = p1 · · · pr .
Similarly,
v` (n) = # of times ` appears in the factorisation n = q1 · · · qs .
Thus every prime ` appears the same number of times in each factorisation,
giving the desired result.
Remark 1.29. Another way of interpreting this result is to say that for n ∈ N
v (n) vp2 (n)
n = p1p1 p2 · · · pvrpr (n)
where p1 , . . . , pr are the distinct prime factors of n. Note that we take the
empty product to be 1, which covers the case n = 1.
Lemma 1.30. Let n = ri=1 pai i where each ai ∈ N ∪ {0} and the pi ’s are
Q
distinct
Q primes. The set of positive divisors of n is the set of numbers of the
form ri=1 pci i where 0 ≤ ci ≤ ai for i = 1, . . . , r.
Proof. Exercise.
9
2 Modular Arithmetic
2.1 Congruences
Definition 2.1. Suppose a, b ∈ Z and n ∈ N. We write a ≡ b mod n (or
a ≡ b (mod n)), and say ‘a is congruent to b mod n’, if and only if n | (a − b).
If n - (a − b) we write a 6≡ b mod n and say that ‘a and b are incongruent
mod n’.
Proof. The proof follows at once from the following properties of divisibility:
(i) n | 0.
(ii) If n | (a − b) then n | (b − a).
(iii) If n | (a − b) and n | (b − c) then n | (a − b) + (b − c) = (a − c).
10
Example 2.6. Let n ∈ N and write n in decimal notation
k
X
n= ai × 10i where 0 ≤ ai ≤ 9 and ai ∈ N ∪ {0} for all i.
i=0
Define f (x) by
k
X
f (x) = ai x i .
i=0
Then, since 10 ≡ −1 mod 11, we see that n = f (10) ≡ f (−1) mod 11,
whence 11 | n ⇐⇒ 11 | f (−1) ⇐⇒ 11 | (a0 − a1 + a2 − a3 + . . . + (−1)k ak ).
This gives an easy way to test integers for divisibility by 11.
Example 2.7. Does the equation x2 − 3y 2 = 2 have a solution with x, y ∈ Z?
Let x, y ∈ Z. Note that x2 − 3y 2 ≡ x2 mod 3. Now x ≡ 0, 1 or 2 mod 3, so
x2 ≡ 0, 1 or 4 mod 3. But 4 ≡ 1 mod 3 so in fact x2 ≡ 0 or 1 mod 3. Hence
x2 − 3y 2 ≡ x2 6≡ 2 mod 3 and so x2 − 3y 2 6= 2.
11
Proof. (i) follows from the transitivity property of divisibility; (ii) follows
from the multiplication and cancellation properties.
Proof. Since d = gcd(c, n), we may write n = dn0 and c = dc0 where n0 , c0 ∈ Z.
Suppose ac ≡ bc mod n. Then n | c(a − b) and hence the cancellation
property of divisibility gives n0 | c0 (a − b). However, gcd(n0 , c0 ) = 1 and so
n0 | (a − b) by Euclid’s Lemma. Thus a ≡ b mod n0 .
Suppose conversely that a ≡ b mod n0 . Then n0 | (a−b) and so n | d(a−b).
But d | c so d(a − b) | c(a − b) and thus n | c(a − b) by the transitive property
of divisibility. Thus ac ≡ bc mod n.
12
Example 2.16. Consider the case n = 2. Then
{0, 1, . . . , n − 1},
{1, . . . , n},
{1, n + 2, 2n + 3, 3n + 4, . . . , n2 },
{x ∈ Z | −n/2 < x ≤ n/2},
13
Example 2.22. The set {0, 1, 2, 3, 4} is a complete residue system mod 5. Now
gcd(2, 5) = 1 so {2 × 0, 2 × 1, 2 × 2, 2 × 3, 2 × 4} = {0, 2, 4, 6, 8} is also a
complete residue system mod 5.
ax ≡ b mod n
to be solved for x. When the modulus n is small we can just use brute force,
i.e., just try every possible value of x mod n. However, this quickly becomes
impractical as n increases.
Theorem 2.23 (Linear congruences with exactly one solution). Let a, b ∈ Z
and let n ∈ N. Suppose that a and n are coprime. Then the linear congruence
ax ≡ b mod n (1)
(We did this calculation in Example 1.16.) Thus by Theorem 2.23 there is
exactly one solution. Moreover, reducing equation (3) mod 841 gives
14
Theorem 2.25 (Solubility of a linear congruence). Let a, b ∈ Z and let
n ∈ N. Then the linear congruence
ax ≡ b mod n (4)
ax + ny = b
for some x, y ∈ Z. Therefore the result now follows from Theorem 1.12
(solubility of linear equations in the integers).
Theorem 2.26. Let a, b ∈ Z and let n ∈ N. Let d = gcd(a, n). Suppose that
d | b and write a = da0 , b = db0 and n = dn0 . Then the linear congruence
ax ≡ b mod n (5)
a0 x ≡ b0 mod n0 . (7)
Proof. Every solution of (5) is a solution of (7) and vice versa by Proposition
2.10. Since a0 and n0 are coprime, (7) has exactly one solution t modulo n0
by Theorem 2.23. Thus the d numbers in (6) are solutions of (7) and hence
of (5). No two of these are congruent modulo n since the relations
imply
rn0 ≡ sn0 mod n, and hence r ≡ s mod d,
where the last implication follows from Proposition 2.10 (note n/n0 = d).
But 0 ≤ |r − s| < d so r = s by Proposition 2.14.
It remains to show that (5) has no solutions other than those listed in (6).
If y is a solution of (5) then ay ≡ b mod n. But we also have at ≡ b mod n
and so ay ≡ at mod n. Thus y ≡ t mod n0 by the cancellation law for
congruences (Theorem 2.11). Hence y = t + kn0 for some k ∈ Z. But
15
r ≡ k mod d for some r ∈ Z such that 0 ≤ r < d. Therefore by Proposition
2.10 we have
ax ≡ b mod n. (8)
a0 x0 + n0 y 0 = 1.
Therefore
16
Thus there are exactly 3 solutions. Moreover, we have a0 = 11, b0 = 7 and
n0 = 18 and we may take x0 = 5 and y 0 = −3. Hence
1 = a0 x0 + n0 y 0 = 11 × 5 + 18 × (−3).
Reducing mod n0 = 18 gives
11 × 5 ≡ 1 mod 18
and multiplying through by b0 = 7 gives
11 × (7 × 5) ≡ 7 mod 18.
Hence t ≡ 7 × 5 ≡ 35 ≡ 17 mod 18 is the unique solution to
11x ≡ 7 mod 18.
Therefore the set of solutions to (10) modulo 54 is
{17, 17 + (1 × 18), 17 + (2 × 18)} = {17, 35, 53}.
17
Definition 2.33. Let n ∈ N and let a ∈ Z such that gcd(a, n) = 1. Then
the unique solution to ax ≡ 1 mod n is called the multiplicative inverse of a
modulo n and is denoted [a]−1 −1
n = [a ]n or a
−1
mod n.
Z ×
Example 2.34. 12Z = {[1]12 , [5]12 , [7]12 , [11]12 }.
18
Theorem 2.38 (Chinese Remainder Theorem). Let n1 , n2 , . . . , nt ∈ N with
gcd(ni , nj ) = 1 whenever i 6= j, (i.e. the ni are “coprime in pairs”) and let
a1 , a2 , . . . , at ∈ Z be given. Then the system of congruences
x ≡ a1 mod n1
..
.
x ≡ at mod nt
x ≡ 2 mod 3,
x ≡ 3 mod 5,
x ≡ 2 mod 7.
Note that the 3, 5 & 7 are indeed coprime in pairs because they are distinct
primes. Following the proof, we put N := 3 × 5 × 7 = 105, N1 = 35, N2 = 21,
N3 = 15 and we have
(Note that in more complicated situations we can use the Extended Euclidean
Algorithm to compute multiplicative inverses modulo n.) Therefore
19
2.6 Euler ϕ-function
Definition 2.40. For n ∈ N, we define Euler’s totient function, or the ϕ-
function, by
20
Proof. For all m ∈ N, either gcd(pr , m) = 1 or p | m (but not both). Thus
ϕ(pr ) = #{m ∈ N : m ≤ pr , p - m}
= #{m ∈ N : m ≤ pr } − #{m ∈ N : m ≤ pr , p | m}
= pr − pr−1 = pr−1 (p − 1).
21
P
Proposition 2.48. For any n ∈ N we have d|n ϕ(d) = n.
2.7 Exponentiation
Example 2.50. What is 3k mod 19 as k varies?
k 0 1 2 3 4 5 6 7 8 9 10
3k mod 19 1 3 9 8 5 15 7 2 6 18 16
k 11 12 13 14 15 16 17 18 19 20
k
3 mod 19 10 11 14 4 12 17 13 1 3 9
Notice that the sequence repeats after a certain point. We can use the fact
that 318 ≡ 1 mod 19 to simplify calculations. For example, by the Division
Algorithm we have 100 = 5 × 18 + 10 so
22
many possible values of ak mod n so there exist i, j ∈ N with i < j such that
ai ≡ aj mod n. Since gcd(a, n) = 1 we may apply the cancellation law for
congruences (Theorem 2.10) i times to obtain aj−i ≡ 1 mod n. Thus we may
take r = j − i.
Proof. Let k = ordn (a). Then ak ≡ 1 mod n. Assume without loss of gener-
ality that r > s. Suppose that r ≡ s mod k. Then there exists t ∈ N such
that r = s + tk. Hence
and so au ≡ 1 mod n. But 0 ≤ u < k and k is the least positive integer such
that ak ≡ 1 mod n and so we must have u = 0. Therefore k | (r − s), i.e.,
r ≡ s mod k.
23
2.8 Euler-Fermat Theorem
Definition 2.56. Let n ∈ N. A subset R of Z is said to be a reduced residue
system modulo n if
(i) R contains ϕ(n) elements,
(ii) no two elements of R are congruent modulo n, and
(iii) for every r ∈ R we have gcd(r, n) = 1.
Proof. If kai ≡ kaj mod n then by the cancellation law for congruences (The-
orem 2.11) we have ai ≡ aj mod n since gcd(k, n) = 1. Therefore no two ele-
ments in the set {ka1 , . . . , kaϕ(n) } are congruent modulo n. Moreover, since
gcd(ai , n) = gcd(k, n) = 1 we have gcd(kai , 1) = 1 (check this!) so each kai
is coprime to n.
24
Corollary 2.62. Let p be prime and let a ∈ Z such that p - a. Then
ap−1 ≡ 1 mod p.
499 = 28 + 243
= 28 + 27 + 115
= 28 + 27 + 26 + 51
= 28 + 27 + 26 + 25 + 19
= 28 + 27 + 26 + 25 + 24 + 3
= 28 + 27 + 26 + 25 + 24 + 21 + 20 .
25
So the binary expansion of 499 is 111110011. Now by squaring the previous
term each time, we have
1
32 ≡ 9 (mod 997)
22
3 ≡ 92 ≡ 81 (mod 997)
23
3 ≡ 812 ≡ 6561 ≡ 579 ≡ −418 (mod 997)
24
3 ≡ (−418)2 ≡ 4182 ≡ 174724 ≡ 249 (mod 997)
25
3 ≡ 2492 ≡ 62001 ≡ 187 (mod 997)
26
3 ≡ 1872 ≡ 34969 ≡ 74 (mod 997)
27
3 ≡ 742 ≡ 5476 ≡ 491 (mod 997)
28
3 ≡ 4912 ≡ 804 ≡ −193 (mod 997).
Therefore
0 1 4 5 6 7 8
3499 ≡ 32 × 32 × 32 × 32 × 32 × 32 × 32 (mod 997)
≡ 3 × 9 × 249 × 187 × 74 × 491 × (−193) (mod 997)
≡ 27 × 46563 × 36334 × (−193) (mod 997)
≡ 27 × 701 × 442 × (−193) (mod 997)
≡ 18927 × (−85306) (mod 997)
≡ (−16) × 436 (mod 997)
≡ −6976 (mod 997)
≡ 3 (mod 997).
Note that the advantage of this method is that it minimizes the number of
multiplications we need to perform and that each integer we consider has at
most twice the number of digits as the modulus.
f (x) = a0 + a1 x + · · · + ad xd ∈ Z[x]
and let p be a prime with p - ad . Then f (x) ≡ 0 mod p has at most d solutions
mod p.
26
Proof. The proof is by induction on d. When d = 1 the congruence is linear:
a1 x + a0 ≡ 0 mod p.
27
Example 2.72. Let p and q be distinct odd primes. Consider the congruence
x2 ≡ 1 mod pq.
It is clear that x ≡ ±1 mod pq are solutions, but are there other solutions?
By the Chinese Remainder Theorem we have
x2 ≡ 1 mod pq
⇐⇒ both x2 ≡ 1 mod p and x2 ≡ 1 mod q
⇐⇒ both x ≡ ±1 mod p and x ≡ ±1 mod q.
Thus there are four solutions modulo pq. Note that
x ≡ 1 mod p
x ≡ 1 mod pq ⇐⇒
x ≡ 1 mod q
and
x ≡ −1 mod p
x ≡ −1 mod pq ⇐⇒
x ≡ −1 mod q
which are the “easy” solutions we already mentioned. It remains to solve the
two pairs of congruences
x ≡ 1 mod p x ≡ −1 mod p
and
x ≡ −1 mod q x ≡ 1 mod q.
Note that we can use a trick here to save work: if x is the solution to one of
these pairs of congruences then −x is the solution to the other congruence.
Consider the following explicit example. We wish to find all solutions to
x2 ≡ 1 mod 145.
Thus it is clear that x ≡ ±1 mod 145 gives two solutions, but we also want
to find the other two solutions. Note that 145 = 5 × 29 and that both 5 and
29 are prime. Thus we want to solve
x ≡ 1 mod 5
x ≡ −1 mod 29.
By the Extended Euclidean Algorithm, we have
gcd(5, 29) = 1 = 6 × 5 − 1 × 29
Thus using the construction of the Chinese Remainder Theorem we may take
x ≡ (−1) × 6 × 5 + 1 × (−1) × 29 ≡ −59 mod 145.
Check that this really is a solution:
(−59)2 = 3481 = 1 + 24 × 145 ≡ 1 mod 145.
Therefore the solutions of x2 ≡ 1 mod 145 are x ≡ ±1, ±59 mod 145.
28
2.11 Hensel Lifting
The Chinese Remainder Theorem shows that the problem of solving a poly-
nomial congruence
f (x) ≡ 0 mod n
can be reduced to solving a system of congruences
where n = pe11 · · · perr is the prime factorisation of n. We show that this can
be further reduced to congruences with prime moduli together with a set of
linear congruences.
Theorem 2.73 (Hensel’s Lemma). Let p be prime. Let f (x) ∈ Z[x] and let
f 0 (x) ∈ Z[x] be its formal derivative. If a ∈ Z satisfies
29
But instead we can take a short-cut as follows. Write a4 = 7 + 52 t3 and try
to solve mod 54 directly. Then we have
f (7 + 52 t3 ) ≡ 0 mod 54 ⇐⇒ (7 + 52 t3 )2 + 1 ≡ 0 mod 54
⇐⇒ 49 + (14 × 25)t3 + 54 t23 + 1 ≡ 0 mod 54
⇐⇒ 50 + (14 × 25)t3 ≡ 0 mod 54
⇐⇒ 2 + 14t3 ≡ 0 mod 52
⇐⇒ 1 + 7t3 ≡ 0 mod 52
⇐⇒ 7t3 ≡ −1 mod 52
⇐⇒ t3 ≡ 7 mod 52 .
30
The problem is that the terms Taylor’s formula have factorials in the denom-
inator, which can cause problems when reducing modulo powers of p: think
about f 00 (x)/2! mod 2, for example.
Proof of Hensel’s Lemma. We will prove by induction that for each n ∈ N
there exists a an ∈ Z satisfying (12) that is unique mod pn . The case n = 1
is trivial using a1 = a. We now suppose the inductive hypothesis holds for
n = k and show it holds for n = k + 1. The idea is to consider ak + pk tk and
see if tk ∈ Z can be chosen in such a way that ak + pk tk satisfies the required
properties of ak+1 .
By Lemma 2.76 with X = ak and Y = pk tk there exists zk ∈ Z such that
f (ak + pk tk ) = f (ak ) + f 0 (ak )pk tk + zk p2k t2k ≡ f (ak ) + f 0 (ak )pk tk mod pk+1
where the congruence follows since k + 1 ≤ 2k. In f 0 (ak )pk tk mod pk+1 the
factors f 0 (ak ) and tk only matter mod p since it already contains a factor
of pk and the modulus is pk+1 . Thus recalling that ak ≡ a mod p we have
f 0 (a)pk tk ≡ f 0 (ak )pk tk mod pk+1 . Therefore we have
where the ratio −f (ak )/pk is in Z since we have f (ak ) ≡ 0 mod pk by the
induction hypothesis, and the last equivalence follows from Proposition 2.10.
But f 0 (a) 6≡ 0 mod p so gcd(f 0 (a), p) = 1 and thus by Theorem 2.23 (linear
congruences with exactly one solution) the last congruence (mod p) has a
solution tk , which is unique mod p.
We set ak+1 = ak + pk tk . Then we have f (ak+1 ) ≡ 0 mod pk+1 and
ak+1 ≡ ak mod pk , so in particular ak+1 ≡ a mod p. It remains to show
uniqueness. Suppose there exists bk+1 ∈ Z with f (bk+1 ) ≡ 0 mod pk+1 and
bk+1 ≡ a mod p. Then we also have f (bk+1 ) ≡ 0 mod pk and so by the
uniqueness of ak we must have bk+1 ≡ ak mod pk . Thus bk+1 = ak + pk sk
for some sk ∈ Z. But (13) and the proceeding discussion shows that sk ≡
tk mod p and thus we must have ak+1 ≡ bk+1 mod pk+1 , as desired.
Remark 2.78. An adaptation of the above proof shows that under the as-
sumptions of Hensel’s Lemma, in principle one can always lift from a solution
mod pk to a solution mod p2k . Moreover, for m ≥ n ≥ 1 we always have
am ≡ an mod pn .
31
2.12 Primitive Roots
Recall Corollary 2.60: if n ∈ N , a ∈ Z with gcd(a, n) = 1 then ordn (a) | ϕ(n).
In this section, we shall be interested in the case that ordn (a) = ϕ(n).
[2]0 = [1], [2]1 = [2], [2]2 = [4], [2]3 = [8] = [3], [2]4 = [16] = [1].
Proof. Let f = ordn (a). The integer ordn (ak ) is the least d ∈ N such that
adk ≡ 1 mod n. By Corollary 2.54 this is also the least d ∈ N such that
dk ≡ 0 mod f . But by the cancellation law for congruences (Theorem 2.11)
this last congruence is equivalent to the congruence d ≡ 0 mod fh where h =
gcd(f, k). But it is clear that the least positive solution to this congruence
is d = fh and so ordn (ak ) = fh , as asserted.
Example 2.84. We saw in Example 2.50 that 3 is a primitive root mod 19,
i.e., ord19 (3) = ϕ(19) = 18. Thus ord19 (33 ) = ord19 (8) = 18/ gcd(18, 3) =
18/3 = 6.
32
Proof. Fix a prime p and for any d ∈ N such that d | (p − 1) define
Therefore if we can show that ψ(d) ≤ ϕ(d) for all d | (p−1) then
P ψ(d) = ϕ(d)
for all such d. (Otherwise, if ψ(d0 ) < ϕ(d) for some d0 , then d|(p−1) ψ(d) <
P
d|(p−1) φ(d) - contradiction.)
If ψ(d) = 0 then ψ(d) ≤ ϕ(d) and so we are done. So we are reduced to
considering the case ψ(d) ≥ 1. Then A(d) 6= ∅ and so a ∈ A(d) for some a.
Hence ordp (a) = d and so ad ≡ 1 mod p. Then (ai )d ≡ 1 mod p for all i ∈ Z.
In particular, the d numbers
a, a2 , . . . , ad (14)
xd − 1 ≡ 0 mod p. (15)
By Corollary 2.55 the numbers listed in (14) are incongruent mod p since
ordp (a) = d. Moreover, (15) has at most d solutions by Lagrange’s polyno-
mial congruence theorem (Theorem 2.68). Therefore the d numbers in (14)
must be all the solutions of (15) mod p. Hence each number in A(d) must be
congruent to ak mod p for some k = 1, . . . , d. By Lemma 2.83 ordp (ak ) = d if
and only if gcd(k, d) = 1. In other words, among the d numbers in (14) there
are ϕ(d) which have order d modulo p. Thus we have shown that ψ(d) = ϕ(d)
if ψ(d) 6= 0, as required.
Example 2.86. There are ϕ(19 − 1) = ϕ(18) = 6 primitive roots mod 19.
Thus there are ϕ(19) − 6 = 12 elements of (Z/19Z)× that are not primitive
roots.
Corollary 2.87. Let p be prime. Then there exists a primitive root g mod-
ulo p (note that g need not be unique). In other words, (Z/pZ)× is cyclic.
Moreover, for any a ∈ Z with p - a there exists k ∈ Z such that a ≡ g k mod p.
33
Proof. The existence of a primitive root follows from Theorem 2.85 since
ϕ(p − 1) ≥ 1. By definition of primitive root, ordp (g) = p − 1 and so
1, g, g 2 , . . . , g p−2 are congruent modulo p, in some order, to 1, 2, . . . , p − 1
(use Corollary 2.55), which gives the last claim.
Theorem 2.88 (The primitive root test). Let n ∈ N and let a ∈ Z with
gcd(a, n) = 1. Then a is a primitive root modulo n if and only if
aϕ(n)/q 6≡ 1 mod n
for every prime q dividing ϕ(n).
Proof. If aϕ(n)/q ≡ 1 mod n for some prime q dividing ϕ(n) then ordn (a) ≤
ϕ(n)/q < ϕ(n) and so a cannot be a primitive root mod n.
Suppose conversely that aϕ(n)/q 6≡ 1 mod n for every prime q dividing
ϕ(n). Write ϕ(n) = q1r1 · · · qsrs where the qi ’s are distinct primes and each ri ∈
N. Let m = ordn (a). Then m | ϕ(n) by Corollary 2.60 and so m = q1t1 · · · qsts
where for each i we have 0 ≤ ti ≤ ri . Suppose m < ϕ(n). Then there exists
r −1
a j such that tj < rj . Hence m divides q1r1 · · · qj j · · · qsrs = (ϕ(n)/qj ). But
am ≡ 1 mod n and so aϕ(n)/qj ≡ 1 mod n, contradicting our hypothesis.
Example 2.89. Find a primitive root modulo 31. Since 31 is prime, we have
ϕ(31) = 31 − 1 = 30 = 2 × 3 × 5. Thus given a ∈ Z with 31 - a we need to
check that
a15 6≡ 1 mod 31, a10 6≡ 1 mod 31, and a6 6≡ 1 mod 31
Try a = 2. Then 210 ≡ (25 )2 ≡ 322 ≡ 12 ≡ 1 mod 31. Thus 2 is not a
primitive root mod31.
Try a = 3. First note that 35 = 243 ≡ −5 mod 31. Then we have
• 36 = 35 × 3 ≡ −5 × 3 ≡ −15 ≡ 16 6≡ 1 mod 31.
• 310 = (35 )2 ≡ (−5)2 ≡ 25 6≡ 1 mod 31.
• 315 = 35 × 310 ≡ −5 × 25 ≡ −125 ≡ −1 6≡ 1 mod 31.
Therefore 3 is a primitive root modulo 31.
Theorem 2.90. Let p be an odd prime. If g is a primitive root mod p then
g is also a primitive root mod pe for all e ≥ 1 if and only if g p−1 6≡ 1 mod p2 .
Proof. Not examinable (but statement is examinable). See Apostal, Intro-
duction to Analytic Number Theory, Chapter 10, for example.
Theorem 2.91. Let n ∈ N. Then (Z/nZ)× is cyclic ⇔ there exists a prim-
itive root modulo n ⇔ n = 1, 2, 4, pe , 2pe where e ∈ N and p is an odd prime.
Proof. Not examinable (but statement is examinable). See Apostal, Intro-
duction to Analytic Number Theory, Chapter 10, for example.
34
2.13 Wilson’s Theorem
Theorem 2.92 (Wilson’s Theorem). An integer p ≥ 2 is prime if and only
if (p − 1)! ≡ −1 mod p.
Example 2.93. For p = 5, we have (5 − 1)! = 4! = 24 ≡ −1 mod 5; but for
p = 6, we have (6 − 1)! = 5! = 120 ≡ 0 mod 6.
Proof. Suppose n is composite. Then there exists d dividing n with 1 <
d < n. Therefore d | (n − 1)! and d | n. So if (n − 1)! ≡ −1 mod n then
n | ((n−1)!+1) and so d | ((n−1)!+1). Hence d | 1 = ((n−1)!+1)−(n−1)!.
Contradiction. Hence (n − 1)! 6≡ −1 mod n.
Suppose p is prime. The case p = 2 is easy, so we can and do assume
that p is odd. Each a in {1, 2, . . . , p − 1} is coprime to p and therefore has
a unique inverse a−1 ∈ {1, 2, . . . , p − 1} modulo p, that is aa−1 ≡ 1 mod p.
Note that (a−1 )−1 ≡ a mod p. If a = a−1 then 1 ≡ aa−1 = a2 mod p. By
Corollary 2.71 we then have a ≡ ±1 mod p and so a = 1 or a = p − 1. In the
product
(p − 1)! = 1 × 2 × 3 × · · · × (p − 2) × (p − 1)
we pair off each term, save for 1 and p − 1, with its inverse modulo p. We
thus have (p − 1)! ≡ 1 × (p − 1) ≡ −1 mod p.
Example 2.94. As an illustration, consider the case p = 11. Then
10! = 1 × 2 × 3 × 4 × 5 × 6 × 7 × 8 × 9 × 10
= 1 × (2 × 6) × (3 × 4) × (5 × 9) × (7 × 8) × 10
≡ 1 × 1 × 1 × 1 × 1 × 10 = 10 ≡ −1 mod 11.
35
Since p is odd we have p = 2k + 1 for some k ∈ N. As k < 2k = p − 1
then g k 6≡ 1 (mod p) but g 2k = g p−1 ≡ 1 (mod p) because ordp (g) = p − 1 by
definition of g (or use Fermat’s little theorem). Since (g k )2 = g 2k ≡ 1 mod p
we have g k ≡ ±1 mod p by Corollary 2.71. Hence g k ≡ −1 mod p. We finally
conclude that
(p − 1)! ≡ g (p−2)(p−1)/2 = g (2k−1)k = (g k )2k−1 ≡ (−1)2k−1 = −1 mod p.
3 Quadratic Residues
3.1 Quadratic Residues
We shall study the theory of quadratic congruences modulo an odd prime p.
By the familiar technique of completing the square one can reduce any such
congruence to the form
x2 ≡ a mod p.
Example 3.1. Consider the case p = 11.
x 0 1 2 3 4 5 6 7 8 9 10
2
x mod 11 0 1 4 9 5 3 3 5 9 4 1
Notice the symmetry in this table. This is because for any odd prime p and
any k ∈ Z we have (p − k)2 ≡ (−k)2 ≡ k 2 mod p. For example, 32 ≡ (−3)2 ≡
(11 − 3)2 ≡ 82 mod 11. Also notice that
one solution if a ≡ 0 mod 11,
2
x ≡ a mod 11 has two solutions if a ≡ 1, 3, 4, 5, 9 mod 11,
no solutions if a ≡ 2, 6, 7, 8, 10 mod 11.
36
Definition 3.3. Let p be an odd prime, and suppose we have a ∈ Z such
that p - a. Then a is a Quadratic Residue of p if there exists x ∈ Z such that
x2 ≡ a mod p, and a is Quadratic Non-Residue if not.
Proposition 3.4. Let p be an odd prime. Then every reduced residue system
mod p contains exactly (p − 1)/2 quadratic residues and (p − 1)/2 quadratic
non-residues mod p. The quadratic residues belong to the residue classes
containing the numbers
12 , 22 , 32 , . . . , ((p − 1)/2)2 . (17)
Proof. First we show that the list of numbers in (17) are distinct mod p.
Indeed, if x2 ≡ y 2 mod p with 1 ≤ x ≤ (p − 1)/2 and 1 ≤ y ≤ (p − 1)/2 then
(x − y)(x + y) ≡ 0 mod p.
But 1 < (x + y) < p so (x + y) is coprime to p. So by the Cancellation
Law for Congruences (Theorem 2.11) we must have (x − y) ≡ 0 mod p, hence
x ≡ y mod p and so x = y (by Proposition 2.14). The remaining squares are
((p + 1)/2)2 , ((p + 3)/2)2 , . . . , (p − 2)2 , (p − 1)2 .
Since (p − k)2 ≡ (−k)2 ≡ k 2 mod p for every k ∈ Z with 1 ≤ k ≤ (p − 1)/2,
these are congruent to
((p − 1)/2)2 , ((p − 3)/2)2 , . . . , 22 , 12 .
These are precisely the numbers in (17). Hence there are precisely (p − 1)/2
quadratic residues mod p, and so there are (p − 1) − (p − 1)/2 = (p − 1)/2
quadratic non-residues mod p.
37
3.3 Euler’s Criterion
Lemma 3.9. Let p be an odd prime and let g be a primitive root mod p. Let
a ∈ Z with p - a. Then a ≡ g k mod p for some k ∈ Z and a is a quadratic
residue mod p if and only if k is even.
Proof. First note that a primitive root g mod p exists by Corollary 2.87, so
a ≡ g k mod p for some k ∈ Z. Suppose k is even. Then k = 2j for some
j ∈ Z and so a ≡ (g j )2 mod p. Thus a is a quadratic residue mod p. Suppose
conversely that a is quadratic residue mod p. Then a ≡ b2 mod p for some
b ∈ Z with p - b. Then b ≡ g r for some r ∈ Z and so g k ≡ (g r )2 ≡ g 2r mod p.
Thus k ≡ 2r mod p − 1 by Proposition 2.53 since ordp (g) = ϕ(p) = p − 1. So
k ≡ 2r mod 2 since 2 | (p − 1). Hence k ≡ 0 mod 2, i.e., k must be even.
f (x) = x(p−1)/2 − 1.
f (x) ≡ 0 mod p
38
has at most (p − 1)/2 solutions. But we have shown that the quadratic
residues mod p are solutions, and Proposition 3.4 says there are (p − 1)/2
of them. Hence none of the quadratic non residues are solutions and so
a(p−1)/2 6≡ 1 mod p. But by Fermat’s Little Theorem (Corollary 2.62) we have
a(p−1) ≡ 1 mod p and so by Corollary 2.71 a(p−1)/2 ≡ ±1 mod p. Therefore
(p−1)/2 a
a ≡ −1 ≡ mod p.
p
and both sides are 1 or −1. If they were different, we would have +1 ≡
−1 mod p and so p | 2, which gives a contradiction as p is odd.
and both sides are +1 or −1. If they were different, we would have +1 ≡
−1 mod p and so p | 2, which gives a contradiction as p is odd.
39
Example 3.13. Can we solve x2 ≡ 13 mod 17?
13 −4
= by periodicity (Remark 3.7)
17 17
−1 2 2
= by multiplicativity (Theorem 3.11)
17 17 17
−1
= as (±1)2 = 1
17
= (−1)(17−1)/2 since 17 ≡ 1 mod 4 (use Theorem 3.12)
= (−1)8 = 1
Hence the congruence is soluble! Note that this proof that a solution exists
cannot be adapted to provide a concrete solution. It is purely an existence
argument.
Proof. It suffices to show that for any N ∈ N there exists a prime p with
p > N and p ≡ 1 mod 4. Let M = (2(N !))2 + 1. If p is a prime with p ≤ N
then M ≡ 1 mod p and so p - M . Let p be a prime factor of M . Then p > N .
As M is odd, p is also odd. Then we have (2(N !))2 ≡ −1 mod p and so the
congruence x2 ≡ −1 mod p is soluble. Therefore p ≡ 1 mod 4 by Theorem
3.12.
Theorem 3.16 (Gauss’ Lemma). Let p be an odd prime and let a ∈ Z with
p - a. Then
a
= (−1)Λ where Λ := #{j ∈ N : 1 ≤ j ≤ p−1
2
, λ(aj, p) > p2 }.
p
40
If j = 4 then λ(aj, p) = λ(20, 13) = 7 > 13/2.
If j = 5 then λ(aj, p) = λ(25, 13) = 12 > 13/2.
If j = 6 then λ(aj, p) = λ(30, 13) = 4< 13/2.
5
Hence Λ = #{2, 4, 5} = 3 and so 13 = (−1)3 = −1.
p−1
Proof. Let Sa := {aj : 1 ≤ j ≤ 2
} and define
r1 r2 · · · rm (p − s1 ) · · · (p − sn ) = 1 × 2 × · · · × p−1 = p−1
2 2
!
n
≡ r1 r2 · · · rm s1 s2 · · · sn (−1) mod p.
Therefore p−1
p−1 p−1
! ≡ (−1)n a 2
2 2
! mod p.
41
p−1
Now, since p - 2
!, the cancellation law for congruences shows that
p−1
1 ≡ (−1)n a 2 mod p.
p−1
Thus a 2 ≡ (−1)n mod p and so ap ≡ (−1)n mod p by Euler’s Criterion
(Theorem 3.10). Both sides are +1 or −1 and if they were different, we would
have +1 ≡−1 mod p and so p | 2, which gives a contradiction as p is odd.
Therefore ap = (−1)n = (−1)Λ as required.
Definition 3.18. For any x ∈ R we set bxc := max{n ∈ Z : n ≤ x}. For
example, b3c = 3, bπc = 3 and b−πc = −4.
Corollary 3.19. If p is an odd prime then
2 (p2 −1)/8 +1, p ≡ ±1 mod 8,
= (−1) =
p −1, p ≡ ±3 mod 8.
Proof. We shall apply Gauss’ Lemma (Theorem 3.16) for a = 2, so that
2
= (−1)Λ where Λ = #{1 ≤ j ≤ p−12
: λ(2j, p) > p2 }.
p
Note that for 1 ≤ j ≤ p−1 2
we have 2 ≤ 2j ≤ p − 1 and so λ(2j, p) = 2j.
Moreover, 2j < 2 if and only if j < p4 , and p2 < 2j < p if and only if
p
p
4
< j < p2 . It follows that Λ = #{j ∈ N : p4 < j < p2 }. We have
# j : p4 < j < p2 = # j ≤ p−1 − # j < p4 = p−1 − p4 .
2 2
42
2
Example 3.20. Since 1009 ≡ 1 mod 8 we have 1009
= 1. Since 1997 ≡
2
−3 mod 8 we have 1997 = −1. (Note that 1009 and 1997 are both prime.)
Proof. It suffices to show that for any N ∈ N there exists a prime p with
p > N and p ≡ −1 mod 8. Let M = 8(N !)2 − 1. If p is a prime with p ≤ N
then M ≡ −1 mod p and so p - M .
Let p be a prime factor of M . Then p is odd and p > N . Moreover,
Lemma 3.22. Let p be an odd prime and let a ∈ Z with a odd and p - a.
Then
(p−1)/2
a t
X
= (−1) where t = bak/pc .
p k=1
Proof. Recall the notation from the proof of Gauss’ Lemma (Theorem 3.16).
For any j ∈ Z we have λ(aj, p) ≡ aj mod p, with 0 ≤ λ(aj, p) < p. Here
λ(aj, p) = aj − pk for some k ∈ Z such that j 0k ≤ aj − pk < p. It follows
that k ≤ p < k + 1, and hence that k = ajp . We therefore deduce that
aj
j k
λ(aj, p) = aj − p ajp . Using this expression we now have
m n (p−1)/2 (p−1)/2
X X X X aj
ri + si = λ(aj, p) = aj − p .
i=1 i=1 j=1 j=1
p
43
Thus
m n (p−1)/2
X X X
ri + np + si ≡ j mod 2,
i=1 i=1 j=1
and hence
m n (p−1)/2
X X X
ri + si ≡ n + j mod 2.
i=1 i=1 j=1
in two different ways. First note that since p and q are odd, we have
We now find another expression for #R. If a point (x, y) were on the line
from (0, 0) to ( p2 , 2q ) we would have y = qx p
and hence py = qx. However,
then we would have p | qx, which is impossible by Euclid’s lemma for primes,
since p - q and p - x (recall that 0 < x < p/2). Thus there are no points
(x, y) of R on the line from (0, 0) to ( p2 , 2q ).
44
How many points (x, y) of R are there below (or on) the diagonal? For
each value of x with 1 ≤ x ≤ p−12
, the pairs (x, y) below the diagonal must
satisfy 1 ≤ y ≤ p x. However, there are b qx
q
p
c such values of y. It follows that
the total number of points below (or on) the line y = qx/p is
(p−1)/2
X qk
.
k=1
p
Similarly, there are
(q−1)/2
X pk
k=1
q
points above (or on) the line. It follows that
(p−1)/2
X qk (q−1)/2 X pk
#R = + .
k=1
p k=1
q
Comparing the two expressions for #R gives the result.
29
Example 3.24. What is 53 ? In other words, can we solve x2 ≡ 29 mod 53?
Note that 29 and 53 are both prime. Use LQR:
29 53
= (by LQR since 29 ≡ 1 mod 4)
53 29
24
= (by periodicity since 53 ≡ 24 mod 29)
29
2×2×2×3
=
29
3
2 3
= (by multiplicativity).
29 29
45
Example 3.25. Recall that in Example 2.67 we used the binary powering
algorithm to show that 3499 ≡ 3 mod 997.
We now perform this computation in a different way. Note that 997 is a
prime and that 997 ≡ 1 mod 4. Moreover, 997 ≡ 1 mod 3. Hence by LQR
we have
3 997 1
= = = 1.
997 3 3
However, Euler’s Criterion (Theorem 3.10) gives
3
≡ 3(997−1)/2 ≡ 3498 mod 997.
997
Hence 3498 ≡ 1 mod 997 and so 3499 ≡ 3 mod 997.
Note that we were “lucky” with the choice of exponent here in that is was
close to (997−1)/2. In general, if p is prime and a ∈ Z with p - a then we can
use LQR and Euler’s Criterion to compute a(p−1)/2 mod p. (In particular, we
must have a(p−1)/2 ≡ ±1 mod p.)
Example 3.26. Determine p3 where p ≥ 5 is a prime.
By LQR we have
3 p (p−1)(3−1)/4 (p−1)/2 p
= (−1) = (−1) .
p 3 3
To determine p3 we need to know the value of p mod 3. To determine
46
Summarising our results, we have
3 +1, if p ≡ ±1 mod 12,
=
p −1, if p ≡ ±5 mod 12.
a r ei
Y a
= ,
n i=1
pi
a
where the symbols on the right are Legendre symbols. We also define 1
= 1.
Proof. These properties are easily deduced from the corresponding properties
of the Legendre Symbol.
Remark 3.29. Let n be an odd positive integer with prime factorisation n =
pe11 · · · perr . If the congruence
x2 ≡ a mod n
has a solution then pai = 1 for each i and hence na = 1. However, the
47
Proof. Write n = p1 p2 · · · pr where the odd prime factors pi are not necessar-
ily distinct. Then we have
r
Y r
X X
n= (1 + pi − 1) = 1 + (pi − 1) + (pi − 1)(pj − 1) + · · · .
i=1 i=1 i6=j
But each factor pi − 1 is even so each sum after the first is divisible by 4.
Hence r
X
n≡1+ (pi − 1) mod 4,
i=1
which gives
r
1 X1
(n − 1) ≡ (pi − 1) mod 2.
2 i=1
2
Therefore
Y r Y r
−1 −1 Pr
= = (−1)(pi −1)/2 = (−1) i=1 (pi −1)/2 = (−1)(n−1)/2 ,
n i=1
pi i=1
Proof. Write n = p1 p2 · · · pr where the odd prime factors pi are not necessar-
ily distinct. Then we have
r
Y r
X X
n2 = (1 + p2i − 1) = 1 + (p2i − 1) + (p2i − 1)(p2j − 1) + · · · .
i=1 i=1 i6=j
hence r
1 2 X1
(n − 1) ≡ (p2i − 1) mod 8.
8 i=1
8
48
This also holds modulo 2, hence
Y r r
2 2 Y 2 2
= = (−1)(pi −1)/8 = (−1)(n −1)/8 .
n i=1
pi i=1
However, the same argument as in the proof of Theorem 3.31 shows that
r
1 X1
(n − 1) ≡ (pi − 1) mod 2
2 i=1
2
49
Example 3.34. Determine whether 888 is a quadratic residue or nonresidue
of the prime 1999. We have
3
888 2 111 111
= =
1999 1999 1999 1999
111
since 1999 ≡ −1 mod 8. To calculate 1999 using Legendre symbols, we
would write
111 3 37
=
1999 1999 1999
and apply the quadratic reciprocity law to each factor on the right. However,
the calculation is much simpler with the Jacobi symbol since we have
111 1999 1
=− =− = −1
1999 111 111
since 111 ≡ 1999 ≡ 3 mod 4 and 1999 ≡ 1 mod 111. Therefore 888 is a
quadratic nonresidue of 1999.
Example 3.35. Determine whether −104 is a quadratic residue or nonresidue
of the prime 997. Since 104 = 23 × 13 we have
3
−104 −1 2 13
=
997 997 997 997
3
2 13
= since 997 ≡ 1 mod 4
997 997
13
=− since 997 ≡ −3 mod 8
997
997
=− since 997 ≡ 1 mod 4
13
9
=− since 997 ≡ 9 mod 13
13
= −1 since 9 is a square.
Therefore −104 is a quadratic nonresidue of 997.
4 Sums of Squares
4.1 Pythagorean triples
Definition 4.1. A Pythagorean triple (x, y, z) is a triple of positive integers
satisfying
x2 + y 2 = z 2 .
50
If gcd(x, y, z) = 1 then (x, y, z) is called a primitive Pythagorean triple.
51
Proof. Let (x, y, z) be a primitive Pythagorean triple with x odd. Then y is
even and z is odd. Let a = 12 (z − x), b = 21 (z + x) and c = y/2. Then a, b,
c ∈ N. Also
(z − x)(z + x) z 2 − x2 y2
ab = = = = c2 .
4 4 4
Let g = gcd(a, b). Then g | (a + b) and g | (b − a); that is g | z and g | x. As
gcd(x, z) = 1, by Theorem 4.3, then g = 1, that is gcd(a, b) = 1.
Let p be a prime factor of a. Then p - b, so vp (b) = 0. Hence
x4 + y 4 = z 4 . (18)
52
We use Fermat’s method of descent. Given a solution (x, y, u) of (19) we
produce another solution (x0 , y 0 , u0 ) with u0 < u. This is a contradiction if we
start with the solution of (19) minimizing u.
Let (x, y, u) be a solution of (19) over N with minimum possible u. We
claim first that gcd(x, y) = 1. If not, then p | x and p | y for some prime p.
Then p4 | (x4 + y 4 ), that is, p4 | u2 . Hence p2 | u. Then (x0 , y 0 , u0 ) =
(x/p, y/p, u/p2 ) is a solution of (19) in N with u0 < u. This is a contradiction.
Hence gcd(x, y) = 1.
As gcd(x, y) = 1 then gcd(x2 , y 2 ) = 1, and so (x2 , y 2 , u) is a primitive
Pythagorean triple by (19). By the symmetry of x and y we may assume
that x2 is odd and y 2 is even, that is, x is odd and y is even. Hence by
Theorem 4.5 there are r, s ∈ N with gcd(r, s) = 1
x2 = r 2 − s 2 , y 2 = 2rs, u = r 2 + s2 .
x = a2 − b 2 , s = 2ab, r = a2 + b 2
u02 = a2 + b2 = x04 + y 04
u0 ≤ u02 = a2 + b2 = r ≤ r2 < r2 + s2 = u.
53
Theorem 4.8. The sets S2 and S4 are closed under multiplication. That is:
(i) If m, n ∈ S2 then mn ∈ S2 .
(ii) If m, n ∈ S4 then mn ∈ S4 .
Proof. Let m, n ∈ S2 . Then m = a2 + b2 and n = r2 + s2 where a, b, r,
s ∈ Z. By the two-square formula,
it is immediate that mn ∈ S2 .
Let m, n ∈ S4 . Then m = a2 + b2 + c2 + d2 and n = r2 + s2 + t2 + u2
where a, b, c, d, r, s, t, u ∈ Z. By the four-square formula,
(a2 + b2 + c2 + d2 )(r2 + s2 + t2 + u2 )
= (ar − bs − ct − du)2 + (as + br + cu − dt)2
+ (at − bu + cr + ds)2 + (au + bt − cs + dr)2 ,
it is immediate that mn ∈ S4 .
Remark 4.9. The two-square formula comes from complex numbers:
Similarly the four-square formula comes from the theory of quaternions (if
you know what they are).
54
Let n ∈ S2 and k = vp (n). We have seen that if k > 0 then k ≥ 2 and
n/p2 ∈ S2 . Note that vp (n/p2 ) = k − 2. Similarly if k − 2 > 0 (that is if
k > 2) then k −2 ≥ 2 (that is k ≥ 4) and n/p4 ∈ S2 . Iterating this argument,
we find that if k = 2r + 1 is odd, then n/p2r ∈ S2 and vp (n/p2r ) = 1, which
is impossible. We conclude that k is even.
Remark 4.11. If n ∈ N, we can write n = rm2 where m2 is the largest square
dividing n and r is squarefree, that is either r = 1 or r is a product of
distinct primes. If any prime factor p of r is congruent to 3 modulo 4 then
vp (n) = 1 + 2vp (m) is odd, and n ∈ / S2 . Hence, if n ∈ S2 , the only possible
prime factors of r are p = 2 and the p congruent to 1 modulo 4. Obviously
2 = 12 + 12 ∈ S2 . It would be nice if all primes congruent to 1 modulo 4 were
also in S2 . Fortunately, this is the case.
a2 + b2 ≡ a2 + (−ua)2 ≡ a2 (1 + u2 ) ≡ 0 (mod p)
55
The idea is as follows. Given a representation a2 + b2 = mp, with 1 ≤
m < p, use this to find another representation c2 + d2 = m0 p with 1 ≤ m0 <
m. Then repeat this process until it terminates (as it must) with m0 = 1,
giving the desired solution. Note that the starting point is the representation
u2 + 12 = mp of the first paragraph.
So suppose that a2 + b2 = mp for some m ∈ N with 1 < m < p. (If m = 1
then we are already done.) Then there exist a0 , b0 ∈ Z with a ≡ a0 mod m,
0 +bb0 0 −ba0
|a0 | ≤ m2 and b ≡ b0 mod m, |b0 | ≤ m2 . Let c = aa m and d = ab m . Now
a02 + b02 m
0 ≤ m0 = ≤ < m < p.
m 2
If m0 = 0 then a0 = b0 = 0. Thus m | a and m | b and so m2 | (a2 + b2 ) = mp.
Thus m | p. But p is prime and 1 < m < p, so m - p - contradiction.
Therefore 1 ≤ m0 < m.
Remark 4.13. In order to make this into an algorithm for finding an expres-
sion p = ab + b2 when p is a prime with p ≡ 1 mod 4, we need to solve the
equation u2 ≡ −1 mod p. (This is the hard part.) Write p = 4k + 1 where
k ∈ N. Let g be a primitive root mod p. Then ordp (g) = ϕ(p) = p − 1 = 4k
and g 0 , g 1 , g 2 , . . . , g 4k−1 are congruent to 1, 2, . . . , p − 1 in some order. Now
x = g 2k is a solution to x2 ≡ 1 mod p and x 6≡ 1 mod p, so g 2k ≡ −1 mod p
by Corollary 2.71. If t ≡ g r mod p where r is odd then tk is a solution of
x2 ≡ −1 (mod p) since 2kr ≡ 2k mod 4k (note that 4k = ordp (g) and use
Proposition 2.53). Thus if we pick t ∈ {1, . . . , p − 1} at random, there is a
50% chance that t ≡ g r mod p with r odd. Given such an t, we set u = tk .
Example 4.14. Let p = 1997. Note that p is prime and p ≡ 1 mod 4. Writing
p = 4k + 1 we have k = (p − 1)/4 = (1997 − 1)/4 = 499. Try t = 2. Then
2499 ≡ 1585 ≡ −412 mod 1997 (one can use the binary powering algorithm
to do this). Note that we chose −412 instead of 1585 because | − 412| =
412 < 1997/2. Check that (−412)2 ≡ −1 mod 1997. Set a = 412 and b = 1.
Then a2 + b2 = 169745 = 85 × 1997, so m = 85.
56
Now 412 ≡ −13 mod 85. So take a0 = −13 and b0 = 1. Set
aa0 + bb0 412 × (−13) + 1 × 1
c= = = −63
m 85
ab0 − ba0 412 × 1 − 1 × (−13)
d= = = 5.
m 85
Now we have 632 + 52 = 3994 = 2 × 1997. Now let a = 63, b = 5 and m = 2.
Then 63 ≡ 1 mod 2 and 5 ≡ 1 mod 2. So we take a0 = b0 = 1 and
aa0 + bb0 63 × 1 + 5 × 1
c= = = 34
m 2
ab0 − ba0 63 × 1 − 5 × 1
d= = = 29.
m 2
Now we have 342 + 292 = 1997, so we are done.
Remark 4.15. In the above example, we need to compute 2499 mod 1997 effi-
ciently. The way to do this is to use the binary powering algorithm that was
introduced in §2.9.
We now give the computation of 2499 mod 1997 (note that in Example 2.67
we worked mod 997 rather than 1997). First we find the binary expansion of
499 as follows:
499 = 28 + 243
= 28 + 27 + 115
= 28 + 27 + 26 + 51
= 28 + 27 + 26 + 25 + 19
= 28 + 27 + 26 + 25 + 24 + 3
= 28 + 27 + 26 + 25 + 24 + 21 + 20 .
So the binary expansion of 499 is 111110011. (This part is exactly the same
as in Example 2.67). Now by squaring the previous term each time, we have
1
22 ≡ 4 (mod 1997)
22
2 ≡ 42 ≡ 16 (mod 1997)
23
2 ≡ 162 ≡ 256 (mod 1997)
24
2 ≡ 2562 ≡ 65536 ≡ 1632 ≡ −365 (mod 1997)
25
2 ≡ (−365)2 ≡ 2663424 ≡ 1423 ≡ −574 (mod 1997)
26
2 ≡ (−574)2 ≡ 329476 ≡ 1968 ≡ −29 (mod 1997)
27
2 ≡ (−29)2 ≡ 841 (mod 1997)
28
2 ≡ 8412 ≡ 707281 ≡ 343 (mod 1997).
57
Therefore
0 1 4 5 6 7 8
2499 ≡ 22 × 22 × 22 × 22 × 22 × 22 × 22 (mod 1997)
≡ 2 × 4 × (−365) × (−574) × (−29) × 841 × 343 (mod 1997)
≡ (−2920) × 16646 × 288463 (mod 1997)
≡ 1074 × 670 × 895 (mod 1997)
≡ 719580 × 670 × 895 (mod 1997)
≡ 660 × 895 (mod 1997)
≡ 1585 (mod 1997).
We can now characterize the elements of S2 .
Theorem 4.16 (Two-square theorem). Let n ∈ N. Then n ∈ S2 if and only
if vp (n) is even whenever p is a prime congruent to 3 modulo 4.
Proof. If n ∈ S2 , p is prime and p ≡ 3 (mod 4) then vp (n) is even by
Theorem 4.10.
If vp (n) is even whenever p is a prime congruent to 3 modulo 4 then
n = rm2 where each prime factor p of r is either 2 or congruent to 1 modulo 4.
By Theorem 4.12 all primes p with p ≡ 1 mod 4 lie in S2 . Moreover, 2 =
12 + 12 ∈ S2 . Hence by Theorem 4.8 r ∈ S2 . Hence r = a2 + b2 where a,
b ∈ Z and so n = rm2 = (am)2 + (bm)2 ∈ S2 .
The representation of a prime as a sum of two squares is essentially unique.
Theorem 4.17. Let p be a prime. If p = a2 + b2 = c2 + d2 with a, b, c,
d ∈ N then either a = c and b = d or a = d and b = c.
Proof. Consider
(ac + bd)(ad + bc) = a2 cd + abc2 + abd2 + b2 cd
= (a2 + b2 )cd + ab(c2 + d2 )
= pcd + pab
= p(ab + cd).
As p | (ac+bd)(ad+bc) then by Euclid’s lemma for primes either p | (ac+bd)
or p | (ad + bc). Assume the former — the latter case can be treated by
reversing the rôles of c and d. Now ac + bd > 0 so that ac + bd ≥ p. Also
(ac + bd)2 + (ad − bc)2 = a2 c2 + 2abcd + b2 d2 + a2 d2 − 2abcd + b2 c2
= a2 c2 + b2 d2 + a2 d2 + b2 c2
= (a2 + b2 )(c2 + d2 )
= p2 .
58
As ac + bd ≥ p, the only way this is possible is if ac + bd = p and ad − bc = 0.
Then ac2 + bcd = cp and ad2 − bcd = 0, so adding gives a(c2 + d2 ) = cp, that
is ap = cp, so that a = c. Then c2 + bd = p = c2 + d2 so that bd = d2 , so that
b = d.
Example 4.18. Find two “essentially different” ways of writing 629 = 17 ×
37 as the sum of two squares. First note that 17 and 37 are both primes
congruent to 1 mod 4, and thus each can be written as the sum of two squares
in a unique way. In fact, 17 = 42 + 12 and 37 = 62 + 12 . Then
629 = |4 + i|2 |6 + i|2 = |(4 + i)(6 + i))|2 = |23 + 10i|2 = 232 + 102
629 = |4 + i|2 |6 − i|2 = |(4 + i)(6 − i))|2 = |25 + 2i|2 = 252 + 22 .
59
(ai , bi ) ≡ (aj , bj ) (mod p). This happens for the vectors ψ(m) with m ∈ B as
|B| > p2 . Thus there are distinct m, n ∈ B with ψ(m) ≡ ψ(n) (mod p). Let
a = m − n. Then ψ(a) = ψ(m) − ψ(n) ≡ (0, 0) (mod p). Let a = (a, b, c, d).
√ √
Then a = m1 − n1 where 0 ≤ m1 , n1 < p so that |a| < p. Similarly |b|,
√
|c|, |d| < p. Then a2 + b2 + c2 + d2 < 4p. As m 6= n then a 6= (0, 0, 0, 0)
and so a2 + b2 + c2 + d2 > 0.
Now (0, 0) ≡ ψ(a) = (ua + vb + c, −va + ub + d) (mod p). Hence c ≡
−ua − vb (mod p) and d ≡ va − ub (mod p). Then
where the last equality holds because we previously showed that 1+u2 +v 2 ≡
0 mod p. As a2 + b2 + c2 + d2 is a multiple of p, and 0 < a2 + b2 + c2 + d2 < 4p,
then we must have a2 + b2 + c2 + d2 ∈ {p, 2p, 3p}.
When a2 + b2 + c2 + d2 = p then certainly p ∈ S4 . Alas, we need to
consider the bothersome cases where a2 + b2 + c2 + d2 = 2p or 3p.
Suppose that a2 + b2 + c2 + d2 = 2p. Then a2 + b2 + c2 + d2 ≡ 2 (mod 4)
so that two of a, b, c, d are odd and the other two even. Without loss of
generality a and b are odd and c and d are even. Then 21 (a + b), 12 (a − b),
1
2
(c + d) and 12 (c − d) are all integers, and a simple computation gives
2 2 2 2
a2 + b2 + c2 + d2
a+b a−b c+d c−d
+ + + = =p
2 2 2 2 2
so that p ∈ S4 .
Finally suppose that a2 + b2 + c2 + d2 = 3p. Then a2 + b2 + c2 + d2 is a
multiple of 3 but not 9. As a2 ≡ 0 or 1 (mod 3) then either exactly one or all
four of a, b, c and d are multiples of 3. But the latter case is impossible (for
then a2 + b2 + c2 + d2 would be a multiple of 9), so without loss of generality
3 | a and b, c, d ≡ ±1 (mod 3). By replacing b by −b etc., if necessary, we
may assume that b ≡ c ≡ d ≡ 1 (mod 3). Then 31 (b + c + d), 13 (a + b − c),
1
3
(a + c − d), 13 (a + d − b), are all integers, and a simple computation gives
2 2 2 2
b+c+d a+b−c a+c−d a+d−b
+ + +
3 3 3 3
2 2 2 2
a +b +c +d
= =p
3
so that p ∈ S4 .
We can now prove Lagrange’s four-square theorem.
60
Theorem 4.20 (Lagrange). If n ∈ N then n ∈ S4 .
61