Number theory begins with the simplest object in mathematics — the integer — and from it builds a subject of extraordinary depth. This treatise is written for a reader who already trusts their own logical machinery: someone preparing for JEE Advanced, the ISI/CMI entrance examinations (B.Stat, B.Math, M.Stat), INMO/IMO-level olympiad work, or the number-theoretic corners of competitive programming. Consequently we do not linger on motivation for its own sake; we state axioms, prove theorems in full wherever the proof teaches a reusable technique, and reserve extended commentary for the moments where a trick, once seen, becomes part of the reader's permanent toolkit.
Where a fully self-contained proof would require machinery well outside elementary number theory — the analytic apparatus behind the Prime Number Theorem, the structure theory of units in rings of integers behind Pell's equation, or the classical (if elementary) but lengthy lattice-point argument behind Quadratic Reciprocity — we say so explicitly, state the result precisely, name the method by which it is proved, and then use the result freely. A working mathematician relies on facts they have not personally re-derived; competition mathematics rewards the same discipline.
Each Part closes with one or two Challenge Problems: genuinely hard problems, worked in full, chosen because a single idea in that Part completely dissolves them. The aim is not merely to display a solution but to show the moment a technique becomes decisive.
Table of Contents
Preface
Conventions and Notation
Part I. Divisibility, Primes, and the Euclidean Algorithm
1.1 The Division Algorithm
1.2 Divisibility: Basic Properties
1.3 GCD, LCM, and the Euclidean Algorithm
1.4 Bézout's Identity and the Extended Euclidean Algorithm
1.5 The Fundamental Theorem of Arithmetic
1.6 The Distribution of Primes
1.7 Challenge Problems
Part II. Modular Arithmetic and Classical Theorems
2.1 Congruences and Residue Systems
2.2 Linear Congruences
2.3 The Chinese Remainder Theorem
2.4 Fermat's Little Theorem and Euler's Theorem
2.5 Wilson's Theorem
2.6 Modular Exponentiation: Tricks and Traps
2.7 Challenge Problems
Part III. Multiplicative Functions and Möbius Inversion
3.1 Multiplicative Functions
3.2 The Classical Functions: τ,σ,φ
3.3 Dirichlet Convolution
3.4 The Möbius Function
3.5 Möbius Inversion
3.6 Challenge Problems
Part IV. Diophantine Equations
4.1 Linear Diophantine Equations
4.2 Pythagorean Triples
4.3 Pell's Equation
4.4 The Method of Infinite Descent
4.5 Interlude: Gaussian Integers and Sums of Two Squares
4.6 Challenge Problem
Part V. Advanced Tools: LTE, Primitive Roots, and Quadratic Residues
5.1 The Lifting the Exponent Lemma
5.2 Orders, Primitive Roots, and Indices
5.3 Quadratic Residues and Euler's Criterion
5.4 The Law of Quadratic Reciprocity
5.5 Challenge Problems
Appendix A. Master Formula Sheet
Closing Remark: A Unifying Theme
Conventions and Notation
Throughout, unless otherwise stated, lowercase Latin letters a,b,c,d,m,n,k denote integers, and p,q denote primes.
the p-adic valuation of n: the exponent of p in the prime factorization of n
⌊x⌋,⌈x⌉
floor and ceiling of a real number x
φ(n)
Euler's totient function
τ(n) or d(n)
number of positive divisors of n
σ(n)
sum of positive divisors of n
μ(n)
the Möbius function
ω(n)
number of distinct prime divisors of n
Ω(n)
number of prime divisors of ncounted with multiplicity
(pa)
the Legendre symbol
ordm(a)
the multiplicative order of a modulo m
[P]
Iverson bracket: 1 if statement P is true, 0 otherwise
f∗g
Dirichlet convolution of arithmetic functions f,g
ε(n)
the Dirichlet-convolution identity: ε(1)=1, ε(n)=0 for n>1
∏p∣n, ∑d∣n
product/sum over primes dividing n / over positive divisors of n
■
end of proof
We take for granted the Well-Ordering Principle (every non-empty subset of Z+ has a least element) and the principle of mathematical induction (in fact these are logically equivalent, together with the Peano axioms, and we treat them as our starting axioms for Z). All rings encountered are commutative with unity unless stated otherwise.
Part I — Divisibility, Primes, and the Euclidean Algorithm
1.1 The Division Algorithm
Theorem 1.1 (Division Algorithm).Let a∈Z and b∈Z+. There exist unique integers q (the quotient) and r (the remainder) such that a=bq+r,0≤r<b.
Proof.Existence. Consider the set S={a−bq:q∈Z}∩N. This set is non-empty: taking q sufficiently negative (e.g. q=−∣a∣) makes a−bq arbitrarily large and positive. By the Well-Ordering Principle, S has a least element r=a−bq0 for some q0. We claim r<b. If not, r≥b, so r−b=a−b(q0+1)≥0 is a smaller element of S, contradicting minimality of r. Hence 0≤r<b.
Uniqueness. Suppose a=bq1+r1=bq2+r2 with 0≤r1,r2<b. Then b(q1−q2)=r2−r1, so b∣(r2−r1). But ∣r2−r1∣<b, so the only multiple of b in this range is 0; hence r1=r2 and consequently q1=q2. ■
Remark. This single theorem is the seed of everything that follows: the Euclidean Algorithm, modular arithmetic, and unique factorization are all downstream consequences of the ability to divide with remainder.
1.2 Divisibility: Basic Properties
Definition. For integers a,b, we say a∣b ("a divides b") if there exists k∈Z with b=ak.
Proposition 1.2.For integers a,b,c,x,y:
(Reflexivity)a∣a.
(Transitivity) If a∣b and b∣c, then a∣c.
(Linearity) If a∣b and a∣c, then a∣(bx+cy) for all integers x,y.
If a∣b and b=0, then ∣a∣≤∣b∣.
If a∣b and b∣a, then a=±b.
Proof. All are immediate from the definition. For (2): b=ak1,c=bk2⇒c=ak1k2. For (3): b=ak1,c=ak2⇒bx+cy=a(k1x+k2y). For (4): if b=ak with k=0 then ∣b∣=∣a∣∣k∣≥∣a∣. For (5): combine (4) both ways. ■
Trick. Property (3) — linearity — is the single most used divisibility fact in olympiad problem-solving. Whenever you know d divides two quantities, you instantly know d divides any integer linear combination of them. This is the seed from which Bézout's identity and the entire Euclidean algorithm grow.
1.3 GCD, LCM, and the Euclidean Algorithm
Definition. For integers a,b, not both zero, gcd(a,b) is the largest positive integer dividing both a and b. We set gcd(0,0)=0 by convention. The least common multiple lcm(a,b) is the smallest positive integer divisible by both a and b.
Theorem 1.3 (Euclidean Algorithm).For a,b∈Z+, gcd(a,b)=gcd(b,amodb), with gcd(a,0)=a. Repeated application terminates and computes gcd(a,b).
Proof. Write a=bq+r with 0≤r<b (Division Algorithm). Any common divisor d of a,b divides r=a−bq (linearity), so d is a common divisor of b,r; conversely any common divisor of b,r divides a=bq+r, so is a common divisor of a,b. Hence a,b and b,r have exactly the same set of common divisors, so in particular the same greatest one: gcd(a,b)=gcd(b,r). Since remainders strictly decrease and are non-negative integers, the process terminates (Well-Ordering) at gcd(d,0)=d. ■
Proof. Let d=gcd(a,b) and write a=da′, b=db′ with gcd(a′,b′)=1 (if a common factor g>1 divided both a′,b′, then dg would be a larger common divisor of a,b than d, contradiction). We claim lcm(a,b)=da′b′. Indeed da′b′=ab′=a′b is a common multiple of a,b. If M is any common multiple, write M=at=da′t. Also b∣M⇒db′∣da′t⇒b′∣a′t; since gcd(a′,b′)=1, b′∣t, so t≥b′, giving M≥da′b′. Hence da′b′ is the least common multiple. Therefore gcd(a,b)lcm(a,b)=d⋅da′b′=(da′)(db′)=ab. ■
Remark (Complexity). The Euclidean Algorithm on inputs a>b terminates in O(logb) steps. This is sharp: Lamé's Theorem shows the worst case occurs precisely on consecutive Fibonacci numbers Fn+1,Fn, since each step reduces the pair by the smallest possible factor.
1.4 Bézout's Identity and the Extended Euclidean Algorithm
Theorem 1.5 (Bézout's Identity).For integers a,b not both zero, there exist integers x,y such that ax+by=gcd(a,b). Moreover gcd(a,b) is the smallest positive integer expressible as ax+by.
Proof. Let S={ax+by:x,y∈Z}∩Z+. Since a2+b2∈S (not both zero), S=∅, so by Well-Ordering it has a least element d=ax0+by0. We show d=gcd(a,b).
(d∣a and d∣b): By the Division Algorithm, a=dq+r, 0≤r<d. Then r=a−dq=a−q(ax0+by0)=a(1−qx0)+b(−qy0),
so r is itself of the form ax+by. If r>0, then r∈S and r<d, contradicting minimality of d. So r=0, i.e. d∣a. Symmetrically d∣b.
(Maximality): If c is any common divisor of a,b, then c∣(ax0+by0)=d by linearity, so c≤d. Hence d is the greatest common divisor. ■
Corollary 1.6.gcd(a,b)=1if and only if there exist integers x,y with ax+by=1.
The Extended Euclidean Algorithm. This is simply the Euclidean Algorithm run forward, then unwound by back-substitution to express gcd(a,b) as an explicit combination ax+by.
Worked Example 1.1. Compute gcd(240,46) and find x,y with 240x+46y=gcd(240,46).
240=5⋅46+10,46=4⋅10+6,10=1⋅6+4,6=1⋅4+2,4=2⋅2+0.
So gcd(240,46)=2. Back-substitute: 2=6−1⋅4 =6−1⋅(10−1⋅6)=2⋅6−10 =2(46−4⋅10)−10=2⋅46−9⋅10 =2⋅46−9(240−5⋅46)=47⋅46−9⋅240.
Check: 47⋅46=2162, 9⋅240=2160, difference =2. So (x,y)=(−9,47). ■
Application: Modular Inverses. If gcd(a,m)=1, the extended Euclidean algorithm produces x,y with ax+my=1, i.e. ax≡1(modm) — so x is the modular inversea−1modm. This runs in O(logm) time and, unlike Fermat-based inversion (Section 2.4), works for any modulus, prime or not, as long as gcd(a,m)=1.
1.5 The Fundamental Theorem of Arithmetic
Lemma 1.7 (Euclid's Lemma).If p is prime and p∣ab, then p∣a or p∣b.
Proof. Suppose p∤a. Since the only positive divisors of p are 1 and p, and p∤a, we get gcd(p,a)=1. By Bézout, there exist x,y with px+ay=1. Multiplying by b: pbx+aby=b.
Now p∣pbx trivially, and p∣ab⇒p∣aby. So p divides the left side, hence p∣b. ■
Corollary 1.8.If prime p∣a1a2⋯ak, then p∣ai for some i. (Immediate induction on k.)
Theorem 1.9 (Fundamental Theorem of Arithmetic).Every integer n>1 can be written as a product of primes, and this factorization is unique up to the order of the factors.
Proof.Existence, by strong induction on n. If n is prime, done. Otherwise n=ab with 1<a,b<n; by the inductive hypothesis a and b are each products of primes, hence so is n.
Uniqueness. Suppose n=p1p2⋯pr=q1q2⋯qs are two prime factorizations. Since p1∣q1⋯qs, Corollary 1.8 gives p1∣qj for some j; as qj is prime, p1=qj. Cancel p1 from both sides and induct on the (smaller) product n/p1. By induction the remaining multisets of primes agree, so r=s and the pi are a rearrangement of the qj. ■
Theorem 1.10 (Euclid — Infinitude of Primes).There are infinitely many primes.
Proof. Suppose not: let p1,…,pn be all the primes. Let N=p1p2⋯pn+1. Since N>1, it has some prime factor p (Theorem 1.9). But N≡1(modpi) for every i, so p∈/{p1,…,pn} — a new prime, contradiction. ■
Trick. The Fundamental Theorem of Arithmetic converts multiplicative statements about integers into additive statements about exponent vectorsn=∏piai↔(a1,a2,…). In particular: gcd(a,b)=∏ppmin(vp(a),vp(b)),lcm(a,b)=∏ppmax(vp(a),vp(b)),
which immediately re-proves Theorem 1.4, since min(x,y)+max(x,y)=x+y for all reals x,y.
1.6 The Distribution of Primes
Beyond mere infinitude, one asks: how dense are the primes? Let π(x) denote the number of primes ≤x.
Theorem 1.11 (Prime Number Theorem, statement only). π(x)∼lnxxas x→∞, meaning limx→∞π(x)lnx/x=1.
This was proved independently by Hadamard and de la Vallée Poussin in 1896, via the complex-analytic theory of the Riemann zeta function ζ(s)=∑n−s and the fact that ζ(s)=0 on the line Re(s)=1. The proof requires complex analysis well beyond our scope here; we state the result and use it only for orientation.
Theorem 1.12 (Bertrand's Postulate).For every integer n>1, there is a prime p with n<p<2n.
This has a genuinely elementary (if intricate) proof due to Erdős, using careful estimates on binomial coefficients (n2n); we omit the details but note it is a frequently-cited existence tool in olympiad combinatorics–number-theory hybrids.
1.7 Challenge Problems
Challenge Problem 1.1.For integer a>1 and positive integers m,n, prove that gcd(am−1,an−1)=agcd(m,n)−1.
Solution. Let d=gcd(m,n).
Step 1: ad−1 is a common divisor. Since d∣m, write m=dk. Then am−1=(ad)k−1 is divisible by ad−1, using the factorization xk−1=(x−1)(xk−1+⋯+1) with x=ad. Similarly ad−1∣an−1.
Step 2: the "Euclidean algorithm" for exponents. Suppose m>n and write m=qn+r (0≤r<n, Division Algorithm). Then am−1=aqn+r−1=ar(aqn−1)+(ar−1).
Since an−1∣aqn−1 (by Step 1's argument applied to exponent qn, a multiple of n), we get am−1≡ar−1(modan−1).
Hence gcd(am−1,an−1)=gcd(ar−1,an−1)=gcd(an−1,ar−1).
Step 3: induction mirroring Theorem 1.3. The pair of exponents (m,n) has stepped to (n,r) — exactly the update rule of the Euclidean Algorithm applied to m,n themselves. Iterating, the sequence of exponent pairs terminates at (gcd(m,n),0) after finitely many steps, and correspondingly gcd(am−1,an−1)=⋯=gcd(agcd(m,n)−1,a0−1)=gcd(ad−1,0)=ad−1. ■
Remark. This is the prototype for a whole family of "exponential GCD" identities (e.g. for Fibonacci numbers, gcd(Fm,Fn)=Fgcd(m,n), provable by an entirely analogous descent).
Challenge Problem 1.2 (IMO 1959, Problem 1).Prove that the fraction 14n+321n+4 is irreducible for every natural number n.
Solution. It suffices to show gcd(21n+4,14n+3)=1. Consider 3(14n+3)−2(21n+4)=42n+9−42n−8=1.
This exhibits 1 as an integer combination of 21n+4 and 14n+3; by Corollary 1.6, gcd(21n+4,14n+3)=1 for every integer n. ■
Trick. This is the archetypal "GCD by inspection" move: to prove gcd(A,B)=1, it is often faster to guess a small integer combination xA+yB=1 than to run the full Euclidean algorithm.
Part II — Modular Arithmetic and Classical Theorems
2.1 Congruences and Residue Systems
Definition. For m∈Z+, a≡b(modm) means m∣(a−b).
Proposition 2.1.Congruence modulo m is an equivalence relation on Z, and it is compatible with addition and multiplication: a≡b,c≡d(modm)⟹a+c≡b+d,ac≡bd(modm).
Proof. Reflexivity, symmetry, transitivity are immediate from properties of divisibility. For compatibility: m∣(a−b) and m∣(c−d) give m∣(a−b)+(c−d)=(a+c)−(b+d). For products: ac−bd=ac−bc+bc−bd=c(a−b)+b(c−d), and m divides both terms. ■
Definition. A complete residue system (CRS) modulo m is a set of m integers, pairwise incongruent mod m (e.g. {0,1,…,m−1}). A reduced residue system (RRS) modulo m is a set of φ(m) integers, each coprime to m, pairwise incongruent mod m (e.g. the integers in {1,…,m} coprime to m).
Proposition 2.2.If {r1,…,rm} is a CRS mod m and gcd(a,m)=1, then {ar1,…,arm} is also a CRS mod m; similarly a RRS maps to a RRS under multiplication by a unit. This is because x↦ax(modm) is a bijection on Z/mZ precisely when gcd(a,m)=1 (it has inverse x↦a−1x).
2.2 Linear Congruences
Theorem 2.3.The congruence ax≡b(modm) has a solution if and only if d=gcd(a,m) divides b. When solvable, it has exactly d pairwise incongruent solutions modulo m.
Proof. If ax≡b(modm) has a solution, then m∣(ax−b), i.e. ax−my=b for some y — a linear Diophantine equation, solvable in integers x,y iff d∣b (Section 4.1; equivalently, since d∣a,m, we need d∣b). Conversely if d∣b, divide through: gcd(a/d,m/d)=1, so a/d has an inverse mod m/d (Corollary 1.6), giving a unique solution x≡x0(modm/d). This single residue class mod m/d splits into exactly d residue classes mod m: x≡x0,x0+dm,x0+2dm,…,x0+(d−1)dm(modm). ■
2.3 The Chinese Remainder Theorem
Theorem 2.4 (CRT).Let m1,…,mk be pairwise coprime positive integers and M=m1m2⋯mk. For any integers a1,…,ak, the system x≡ai(modmi),i=1,…,k has a solution x, unique modulo M.
Proof (constructive). For each i, let Mi=M/mi. Since m1,…,mk are pairwise coprime, gcd(Mi,mi)=1, so by the Extended Euclidean Algorithm there is yi with Miyi≡1(modmi). Define x0=∑i=1kaiMiyi. Verification: fix j. For i=j, mj∣Mi (since Mi contains mj as a factor), so the i-th term vanishes mod mj. The j-th term gives ajMjyj≡aj⋅1=aj(modmj). So x0≡aj(modmj) for every j — existence.
Uniqueness mod M: if x,x′ both solve the system, then mi∣(x−x′) for every i; since the mi are pairwise coprime, their product M divides x−x′ (an easy induction using Euclid's Lemma repeatedly), so x≡x′(modM). ■
Worked Example 2.1. Solve x≡2(mod3),x≡3(mod5),x≡2(mod7).
Here M=105. M1=35≡2(mod3), and 2−1≡2(mod3) (since 2⋅2=4≡1), so y1=2: term =2⋅35⋅2=140. M2=21≡1(mod5), y2=1: term =3⋅21⋅1=63. M3=15≡1(mod7), y3=1: term =2⋅15⋅1=30.
Sum =140+63+30=233≡233−210=23(mod105).
Check: 23=3⋅7+2≡2(mod3); 23=4⋅5+3≡3(mod5); 23=3⋅7+2≡2(mod7). ✓ So x≡23(mod105). ■
2.4 Fermat's Little Theorem and Euler's Theorem
Theorem 2.5 (Fermat's Little Theorem).If p is prime and p∤a, then ap−1≡1(modp). Equivalently, ap≡a(modp) for every integer a.
Proof. Consider the p−1 numbers a⋅1,a⋅2,…,a⋅(p−1)(modp). Since gcd(a,p)=1, multiplication by a is a bijection on the nonzero residues mod p (Proposition 2.2), so this list is simply a permutation of 1,2,…,p−1(modp). Multiplying all p−1 terms in each list: ap−1⋅(p−1)!≡(p−1)!(modp).
Since p is prime and none of 1,…,p−1 is divisible by p, gcd((p−1)!,p)=1; cancel (p−1)! from both sides (multiply by its inverse) to get ap−1≡1(modp). ■
Theorem 2.6 (Euler's Theorem).If gcd(a,n)=1, then aφ(n)≡1(modn).
Proof. Identical strategy, using a reduced residue system {r1,…,rφ(n)} instead of {1,…,p−1}. Since gcd(a,n)=1, multiplication by a permutes this RRS (Proposition 2.2), so aφ(n)∏iri≡∏iri(modn).
Each ri is coprime to n, so ∏ri is coprime to n and cancellable, yielding aφ(n)≡1(modn). ■
Remark. Fermat's Little Theorem is the special case n=p (prime), since φ(p)=p−1.
2.5 Wilson's Theorem
Theorem 2.7 (Wilson's Theorem).For prime p: (p−1)!≡−1(modp).
Proof. For p=2: 1!=1≡−1(mod2). For p=3: 2!=2≡−1(mod3). Now let p>3. Every a∈{1,…,p−1} has a unique inverse a−1 in the same set (since gcd(a,p)=1). We ask: when is a its own inverse, i.e. a2≡1(modp)? This factors as (a−1)(a+1)≡0(modp), so (Euclid's Lemma) a≡1 or a≡−1≡p−1(modp). So among {2,3,…,p−2}, every element is paired with a distinct partner whose product is 1(modp) — these p−3 elements split into 2p−3 pairs, each multiplying to 1. Hence (p−1)!=1⋅(p−1)⋅∏pairs(a⋅a−1)≡1⋅(p−1)⋅1≡−1(modp). ■
Remark (Converse). If n>1 is composite, then (n−1)!≡0(modn) (for n>4; check n=4 separately: 3!=6≡2(mod4)). Hence Wilson's theorem, together with its converse, gives a (computationally useless, but theoretically clean) primality characterization: n is prime iff (n−1)!≡−1(modn).
2.6 Modular Exponentiation: Tricks and Traps
Trick (Fast/Binary Exponentiation). To compute aemodm, write e in binary and repeatedly square: ae=∏i:biti(e)=1a2i(modm),
computable in O(loge) modular multiplications, each keeping intermediate values reduced mod m.
Trick (Reducing huge exponents). If gcd(a,m)=1, Euler's theorem lets us reduce the exponent modulo φ(m): ae≡aemodφ(m)(modm).
Trap. This reduction is only valid when gcd(a,m)=1. If gcd(a,m)>1, na"ively reducing the exponent mod φ(m) can give a wrong answer (e.g. 2φ(4)=22=4≡0(mod4), but 20=1≡0). The correct general statement, useful for towers of exponents, is:
Lemma 2.8 (Generalized Euler / "lifting the exponent bound").For any integers a,m and e≥log2m, ae≡aφ(m)+(emodφ(m))(modm).
This holds regardless of gcd(a,m), and is typically proved via the Chinese Remainder Theorem by splitting m into prime-power factors and analyzing each factor separately (on each prime power pk∣m, once the exponent exceeds k, all higher powers of a divisible by p collapse predictably). We state it and use it freely.
Worked Example 2.2 (Tower exponentiation). Compute 777⋯(mod100) (a tower of height ≥3).
Here φ(100)=40. Since the exponent (itself a tower of 7's) vastly exceeds log2100≈6.6, Lemma 2.8 lets us reduce: we need 77⋯mod40 (one level down). Now φ(40)=16, and again the remaining tower exceeds log240, so reduce further: need 77⋯mod16. Now φ(16)=8; 7≡−1(mod8), so 7odd≡−1≡7(mod8) (the remaining tower height is odd), giving exponent ≡7(mod16) hence effectively 77mod16: 72=49≡1(mod16), so 77=76⋅7≡1⋅7=7(mod16). Climbing back up: 77mod40: since 72=49≡9,74≡81≡1(mod40), so 77=74⋅72⋅7≡1⋅9⋅7=63≡23(mod40). Finally 723mod100: using repeated squaring, 72=49,74=492=2401≡1,78≡1,716≡1(mod100) (since 74≡1(mod100) — check: 2401=24⋅100+1, yes). So 723=720⋅73≡1⋅343≡43(mod100). The tower converges to ≡43(mod100). ■
2.7 Challenge Problems
Challenge Problem 2.1 (IMO 2005, Problem 4).Determine all positive integers relatively prime to every term of the infinite sequence an=2n+3n+6n−1,n≥1.
Solution. We claim the answer is n=1 only (i.e. only 1 is coprime to every term). We show every prime p divides some term an; this forces any integer >1 (having some prime factor p) to share a factor with an for that p, leaving only 1.
p=2: a1=2+3+6−1=10, and 2∣10.
p=3: a2=4+9+36−1=48, and 3∣48.
p>3: Take n=p−2. Since p∤2,3,6, Fermat's Little Theorem gives xp−1≡1(modp) for x∈{2,3,6}, so xp−2≡x−1(modp). Hence ap−2≡2−1+3−1+6−1−1(modp).
Since 2−1+3−1+6−1=63+2+1=1 as a rational identity that persists mod p (as p∤6, so 6 is invertible and the identity 6⋅(2−1+3−1+6−1)≡3+2+1=6(modp) forces 2−1+3−1+6−1≡1), we get ap−2≡1−1=0(modp).
So every prime divides some an, and the answer is n=1. ■
Remark. This is a beautiful demonstration that Fermat's Little Theorem is not just about computing powers, but about inverting elements when the exponent is exactly p−2 — a trick worth memorizing: xp−2≡x−1(modp).
Challenge Problem 2.2.Show that for a prime p≡1(mod4), the congruence x2≡−1(modp) has a solution, namely x=(2p−1)!.
Solution. Split (p−1)! into two halves and pair k↔p−k: (p−1)!=[∏k=1(p−1)/2k]⋅[∏k=(p+1)/2p−1k]=[(2p−1)!]⋅∏j=1(p−1)/2(p−j),
relabeling the second product via k=p−j for j=1,…,2p−1. Since p−j≡−j(modp), ∏j=1(p−1)/2(p−j)≡∏j=1(p−1)/2(−j)=(−1)(p−1)/2(2p−1)!(modp).
Therefore (p−1)!≡(−1)(p−1)/2[(2p−1)!]2(modp).
By Wilson's Theorem, the left side is ≡−1(modp). Since p≡1(mod4), 2p−1 is even, so (−1)(p−1)/2=1, giving [(2p−1)!]2≡−1(modp).
So x=(2p−1)! satisfies x2≡−1(modp). ■
Remark (forward reference). If instead p≡3(mod4), the same computation gives [(2p−1)!]2≡1(modp), and in fact x2≡−1(modp) has no solution at all: if it did, any solution x would satisfy x4≡1 but x2≡−1≡1, so x has multiplicative order exactly 4; but every order divides p−1 (Section 5.2), and 4∤(p−1) when p≡3(mod4) — contradiction. This dichotomy ("−1 is a QR mod p iff p≡1(mod4)") will reappear as the first supplementary law of Quadratic Reciprocity in Part V, proved there more slickly via Euler's Criterion — and again in Part IV, where it determines exactly which primes are sums of two squares.
Part III — Multiplicative Functions and Möbius Inversion
3.1 Multiplicative Functions
Definition. An arithmetic function is any f:Z+→C. We say f is:
multiplicative if f(1)=1 and f(mn)=f(m)f(n) whenever gcd(m,n)=1;
completely multiplicative if f(1)=1 and f(mn)=f(m)f(n) for allm,n (no coprimality needed).
Proposition 3.1.A multiplicative function is completely determined by its values on prime powers: if n=∏piai, then f(n)=∏f(piai).
Proof. Immediate induction using multiplicativity, since distinct prime powers piai are pairwise coprime. ■
Examples.
id(n)=n and 1(n)=1 (the constant function) are completely multiplicative.
The Liouville function λ(n)=(−1)Ω(n) is completely multiplicative.
φ,τ,σ,μ (below) are multiplicative but not completely multiplicative — e.g. μ(p2)=0=μ(p)2=1.
Proof.For τ: since τ is multiplicative (divisors of coprime numbers correspond bijectively to pairs of divisors, one of each factor), it suffices to compute τ(pa): the divisors of pa are 1,p,…,pa, giving a+1 of them.
For σ: similarly multiplicative; σ(pa)=1+p+⋯+pa=p−1pa+1−1 (geometric series).
For φ: multiplicative (proved via CRT in the exercises, or directly: the map Z/mnZ→Z/mZ×Z/nZ for coprime m,n is a ring isomorphism, restricting to a bijection between units, so φ(mn)=φ(m)φ(n)). We compute φ(pa): among 1,…,pa, exactly the multiples of p (there are pa−1 of them) fail to be coprime to pa, so φ(pa)=pa−pa−1=pa(1−1/p). Multiplying over prime power factors gives the stated product formula. ■
3.3 Dirichlet Convolution
Definition. For arithmetic functions f,g, their Dirichlet convolution is (f∗g)(n)=∑d∣nf(d)g(n/d).
Proposition 3.3.Dirichlet convolution is commutative and associative, with identity element ε(n)={10n=1n>1,f∗ε=f for all f.
Proof. Commutativity: reindex d↦n/d in the sum. Associativity: both (f∗g)∗h and f∗(g∗h) evaluated at n equal ∑abc=nf(a)g(b)h(c), a symmetric triple sum. The identity property is immediate since the only divisor pair contributing when g=ε is (d,n/d)=(n,1). ■
Theorem 3.4.If f,g are multiplicative, so is f∗g.
Proof. Let h=f∗g and gcd(m,n)=1. Every divisor e of mn factors uniquely as e=d1d2 with d1∣m, d2∣n (and then gcd(d1,d2)=1, gcd(m/d1,n/d2)=1 too). Hence h(mn)=∑d1∣m∑d2∣nf(d1d2)g(d1m⋅d2n)=∑d1∣m∑d2∣nf(d1)f(d2)g(d1m)g(d2n)
(using multiplicativity of f,g on the coprime factors) =[∑d1∣mf(d1)g(m/d1)][∑d2∣nf(d2)g(n/d2)]=h(m)h(n). ■
Key identities. Since τ(n)=∑d∣n1, σ(n)=∑d∣nd, we have τ=1∗1,σ=id∗1.
Theorem 3.5.φ∗1=id, i.e.d∣n∑φ(d)=n.
Proof. Consider the n fractions n1,n2,…,nn. Reduce each to lowest terms dk, where necessarily d∣n. For a fixed divisor d∣n, the fractions reducing to have denominator exactlyd correspond to numerators k∈{1,…,d} with gcd(k,d)=1 — there are φ(d) of these. Since every one of the original n fractions falls into exactly one such class, ∑d∣nφ(d)=n.■
Dirichlet inverses. Every f with f(1)=0 has a unique Dirichlet inverse f−1 satisfying f∗f−1=ε, computable recursively: f−1(1)=f(1)1,f−1(n)=f(1)−1∑d∣nd>1f(d)f−1(n/d)(n>1).
(Derived directly by demanding (f∗f−1)(n)=0 for n>1 and isolating the d=1 term.)
3.4 The Möbius Function
Definition.μ(1)=1; if n is squarefree with k distinct prime factors, μ(n)=(−1)k; if n has a repeated prime factor, μ(n)=0.
Proposition 3.6.μis multiplicative (immediate from the definition, since squarefreeness and the prime-factor count both behave multiplicatively over coprime arguments).
Proof. The case n=1 is trivial. For n>1, the function g(n):=∑d∣nμ(d)=(μ∗1)(n) is multiplicative by Theorem 3.4, so by Proposition 3.1 it suffices to check g(pa)=0 for every prime power pa, a≥1: g(pa)=μ(1)+μ(p)+μ(p2)+⋯+μ(pa)=1+(−1)+0+⋯+0=0.
Since n>1 has at least one prime-power factor pa in its factorization, and g is multiplicative with g(pa)=0 there, g(n)=0. ■
Corollary.μis the Dirichlet inverse of 1.
3.5 Möbius Inversion
Theorem 3.8 (Möbius Inversion Formula).If g(n)=∑d∣nf(d) for all n (i.e. g=f∗1), then f(n)=∑d∣nμ(d)g(n/d)for all n.
Proof. Convolve both sides of g=f∗1 with μ on the right: g∗μ=(f∗1)∗μ=f∗(1∗μ)=f∗ε=f,
using associativity/commutativity of Dirichlet convolution (Proposition 3.3) and 1∗μ=ε (Theorem 3.7). ■
Worked Example 3.1. Since id=φ∗1 (Theorem 3.5), Möbius inversion gives φ(n)=∑d∣nμ(d)⋅dn,
recovering Theorem 3.2's product formula upon expansion — a second, purely combinatorial derivation of φ.
Trick (a clean corollary). For all N≥1, ∑d=1Nμ(d)⌊dN⌋=1.
Indeed, ∑d=1Nμ(d)⌊N/d⌋=∑d=1Nμ(d)∑m≤Nd∣m1=∑m=1N∑d∣mμ(d)=∑m=1Nε(m)=ε(1)=1, using Theorem 3.7. This identity underlies fast (O(N2/3) or better) computations of Mertens-function-type sums in competitive programming.
3.6 Challenge Problems
Challenge Problem 3.1.Evaluate in closed form: S(n)=∑i=1n∑j=1ngcd(i,j).
Solution. The key is the identity id=φ∗1 from Theorem 3.5, applied to gcd(i,j) itself: for any positive integer m=gcd(i,j), m=∑d∣mφ(d).
Hence S(n)=∑i=1n∑j=1n∑d∣gcd(i,j)φ(d)=∑d=1nφ(d)⋅#{(i,j):1≤i,j≤n,d∣i,d∣j},
having swapped the order of summation (for each d, we count all pairs (i,j) for which d divides both, since such pairs are exactly those contributing a φ(d) term). The count of multiples of d in {1,…,n} is ⌊n/d⌋, so the count of valid pairs is ⌊n/d⌋2. Therefore: S(n)=d=1∑nφ(d)⌊dn⌋2.
Verification for n=2: direct computation gives gcd(1,1)+gcd(1,2)+gcd(2,1)+gcd(2,2)=1+1+1+2=5. The formula: φ(1)⌊2/1⌋2+φ(2)⌊2/2⌋2=1⋅4+1⋅1=5. ✓■
Remark. This closed form is the standard tool for computing such sums for large n in O(n) time in competitive programming, by grouping equal values of ⌊n/d⌋ into O(n) blocks (the "divisor block" or "⌊n/d⌋ has only O(n) distinct values" trick) combined with a precomputed prefix sum of φ.
Challenge Problem 3.2.Prove that for every n≥1, φ(n)n=∑d∣nφ(d)μ(d)2.
Solution. Both sides are multiplicative functions of n (the left side because n/φ(n)=∏p∣np/(p−1) depends only on the set of distinct primes dividing n, which is a multiplicative-type quantity across coprime factors; the right side is a sum over squarefree divisors — nonzero only when d is squarefree, since μ(d)2=0 otherwise — and is a Dirichlet convolution μ2∗(1/φ)... more directly, it is multiplicative as a divisor-sum of the multiplicative function d↦μ(d)2/φ(d), by the same style of argument as Theorem 3.4 applied to functions valued in Q). By Proposition 3.1 it suffices to check equality on prime powers pa, a≥1.
Left side at pa: φ(pa)pa=pa−pa−1pa=p−1p.
Right side at pa: the squarefree divisors of pa are only 1 and p (any p2∣d makes μ(d)=0), so ∑d∣paφ(d)μ(d)2=φ(1)μ(1)2+φ(p)μ(p)2=1+p−11=p−1p.
The two sides agree on every prime power, hence (both being multiplicative) they agree for all n. ■
Part IV — Diophantine Equations
4.1 Linear Diophantine Equations
Theorem 4.1.The equation ax+by=c (in integers x,y, with a,b not both zero) has a solution if and only if gcd(a,b)∣c. When solvable, writing d=gcd(a,b) and (x0,y0) for any one solution, the complete solution set is x=x0+dbt,y=y0−dat,t∈Z.
Proof.Necessity: d∣a,b⇒d∣(ax+by)=c. Sufficiency: Bézout (Theorem 1.5) gives x1,y1 with ax1+by1=d; if d∣c, write c=dk and scale: x0=kx1,y0=ky1 solves ax+by=c.
General solution: if (x,y) is any other solution, subtracting gives a(x−x0)=−b(y−y0), so da(x−x0)=−db(y−y0). Since gcd(a/d,b/d)=1, Euclid's Lemma gives db∣(x−x0), say x−x0=dbt; substituting back gives y−y0=−dat. Conversely every such pair is easily checked to solve the equation. ■
4.2 Pythagorean Triples
Definition. A triple of positive integers (x,y,z) with x2+y2=z2 is a Pythagorean triple; it is primitive if gcd(x,y,z)=1 (equivalently, x,y,z are pairwise coprime).
Theorem 4.2.Every primitive Pythagorean triple with y even is given by x=m2−n2,y=2mn,z=m2+n2 for coprime integers m>n>0 of opposite parity, and every such choice of m,n yields a primitive triple.
Proof.(Necessity.) First, x,y cannot both be even (else gcd(x,y,z)≥2). They cannot both be odd either: odd squares are ≡1(mod4), so x2+y2≡2(mod4) would force z2≡2(mod4), impossible since squares are ≡0,1(mod4). So exactly one of x,y is even; by hypothesis it's y, and x,z are both odd (since z2=x2+y2 with x odd, y even forces z odd).
Since z,x are odd, z−x and z+x are both even; write z−x=2a, z+x=2b. From y2=z2−x2=(z−x)(z+x)=4ab, we get (y/2)2=ab. Any common divisor of a,b divides both a+b=z and b−a=x, hence divides gcd(x,z)=1 (primitivity), so gcd(a,b)=1. Since ab is a perfect square and a,b are coprime, unique factorization forces each of a,b to be a perfect square individually: a=n2, b=m2 for positive integers m>n (as b>a since x=b−a>0). Then: z=a+b=m2+n2,x=b−a=m2−n2,y=2ab=2mn.
Since gcd(a,b)=1⇒gcd(m,n)=1. And since x=m2−n2 is odd, m,n must have opposite parity (same parity would make m2−n2 even).
(Sufficiency.) Direct substitution: (m2−n2)2+(2mn)2=m4−2m2n2+n4+4m2n2=(m2+n2)2. Primitivity: any common prime p of x,y,z would (since z±x∈{2m2,2n2} and z,x odd, p odd) divide both m2 and n2, contradicting gcd(m,n)=1. ■
Worked Example 4.1.(m,n)=(2,1): (x,y,z)=(3,4,5). (m,n)=(3,2): (x,y,z)=(5,12,13). (m,n)=(4,1): (15,8,17).
4.3 Pell's Equation
Definition. For a non-square positive integer d, Pell's equation is x2−dy2=1, sought in positive integers x,y.
Motivation (continued fractions). The continued fraction expansion of d is eventually periodic, say d=[a0;a1,a2,…,aℓ]. Its convergents pk/qk satisfy pk2−dqk2=(−1)k+1⋅(something bounded), and in fact one always finds among the convergents a fundamental solution(x1,y1) — the smallest positive solution. We state this existence result (a consequence of Dirichlet's Approximation Theorem and the periodicity of the continued fraction of a quadratic irrational) and use it freely, rather than re-deriving the theory of continued fractions here.
Theorem 4.3.Let (x1,y1) be the fundamental solution of x2−dy2=1. Then every positive-integer solution (xk,yk) is given by xk+ykd=(x1+y1d)k,k=1,2,3,…
Proof idea. Solutions to x2−dy2=1 correspond exactly to elements α=x+yd of the ring Z[d] of normN(α):=ααˉ=x2−dy2 equal to 1, i.e. to units of norm 1. The norm is multiplicative, N(αβ)=N(α)N(β), so products and powers of norm-1 elements again have norm 1 — this shows every power (x1+y1d)k gives a solution. That these exhaust all positive solutions (i.e. that x1+y1dgenerates the full group of norm-1 units greater than 1) is a consequence of the general structure theorem for units in real quadratic orders (a special case of Dirichlet's Unit Theorem); we cite this rather than re-derive it. ■
Worked Example 4.2 (d=2). The fundamental solution of x2−2y2=1 is (x1,y1)=(3,2) (check: 9−8=1). Since (3+22)2=17+122, the next solution is (17,12): 172−2⋅122=289−288=1. ✓ Equivalently, one has the linear recurrence xk+1=3xk+4yk,yk+1=2xk+3yk.
4.4 The Method of Infinite Descent
Example 4.1 (warm-up descent).The equation x2+y2=3z2 has no solution in positive integers.
Proof. Squares mod 3 are 0 or 1. If x2+y2≡0(mod3), checking all combinations from {0,1} shows this forces x2≡y2≡0(mod3), i.e. 3∣x and 3∣y. Write x=3x1,y=3y1: then 9x12+9y12=3z2⇒3(x12+y12)=z2⇒3∣z2⇒3∣z, say z=3z1. Substituting: 3(x12+y12)=9z12⇒x12+y12=3z12 — the same equation, with strictly smaller positive integers (assuming (x,y,z) was a minimal positive solution, e.g. minimizing z). This contradicts minimality unless no positive solution exists at all. ■
Theorem 4.4 (Fermat).The equation x4+y4=z2 has no solution in positive integers.
Proof. Suppose a solution exists; choose one with z minimal. First, gcd(x,y)=1: if a prime p divided both, then p4∣z2, so p2∣z, and (x/p)4+(y/p)4=(z/p2)2 would be a smaller solution, contradicting minimality of z.
So (x2,y2,z) is a primitive Pythagorean triple: (x2)2+(y2)2=z2, and gcd(x2,y2)=1. WLOG (by symmetry of the equation in x,y) y2 is even. By Theorem 4.2, x2=m2−n2,y2=2mn,z=m2+n2,
for coprime m>n>0 of opposite parity.
From x2=m2−n2, i.e. x2+n2=m2: this is another primitive Pythagorean triple (as gcd(m,n)=1). Since x is odd (it is the "odd leg" of the first triple, as established in Theorem 4.2's proof) and exactly one of x,n must be even, n is even. Apply Theorem 4.2 again: x=p2−q2,n=2pq,m=p2+q2,
for coprime p>q>0 of opposite parity.
Now recall y2=2mn=2m(2pq)=4mpq, so (y/2)2=mpq. We check m,p,q are pairwise coprime: gcd(p,q)=1 by construction; and since m=p2+q2, any common prime factor of m and p would (as p2≡−q2) divide q2 too, contradicting gcd(p,q)=1 — similarly for m,q. Since mpq is a perfect square and m,p,q are pairwise coprime, unique factorization forces each to be a perfect square individually: m=r2,p=s2,q=t2
for positive integers r,s,t. Substituting into m=p2+q2: r2=s4+t4.
This is a new solution of the original equation X4+Y4=Z2, with Z=r. We now check r<z, completing the descent: since n≥1, z=m2+n2>m2≥m (as m≥1), and since r2=m, if r≥1 then r≤r2=m<z. So r<z — a strictly smaller solution, contradicting the minimality of z. ■
Corollary 4.5 (Fermat's Last Theorem, exponent 4).x4+y4=z4 has no solution in positive integers (since such a solution would give x4+y4=(z2)2, contradicting Theorem 4.4).
4.5 Interlude: Gaussian Integers and Sums of Two Squares
The ring of Gaussian integersZ[i]={a+bi:a,b∈Z} carries a multiplicative norm N(a+bi)=a2+b2. One shows (via an explicit division algorithm using nearest-lattice-point rounding) that Z[i] is a Euclidean domain, hence a UFD — we state this structural fact and name the method (norm-Euclidean division) rather than re-derive it.
Theorem 4.6 (Fermat, sum of two squares).An odd prime p can be written as p=a2+b2 (for integers a,b) if and only if p≡1(mod4).
Proof sketch.(p≡3(mod4) is impossible.) Squares mod 4 are 0,1, so a2+b2(mod4)∈{0,1,2}, never 3.
(p≡1(mod4)⇒ possible.) By Challenge Problem 2.2, there exists x with x2≡−1(modp), i.e. p∣x2+1=(x+i)(x−i) in Z[i]. If p were prime in Z[i], it would (by the UFD property) divide x+i or x−i — but then p would divide both the real and imaginary parts of x±i, in particular p∣1, absurd. So p is not prime in Z[i]: it factors as p=ππˉ for some non-unit π=a+bi (a genuine factorization, since Z[i] is a UFD and p is reducible). Taking norms: p2=N(p)=N(π)N(πˉ)=N(π)2, so N(π)=p, i.e. p=a2+b2.■
Remark. This is a striking instance of the treatise's recurring theme: the modular-arithmetic fact "−1 is a QR mod p" (Part II) becomes, via the arithmetic of Z[i], a statement about which primes split in a larger ring — a first glimpse of algebraic number theory, to which we return in the Closing Remark.
4.6 Challenge Problem
Challenge Problem 4.1.Find all integer solutions of y2+2=x3.
Solution. We work in Z[−2]={a+b−2:a,b∈Z}, with norm N(a+b−2)=a2+2b2. As with Z[i], one shows via an explicit division algorithm that Z[−2] is a Euclidean domain (hence a UFD) with respect to this norm; we state and use this fact, naming the method (norm-Euclidean division), without re-deriving it. The only units are ±1 (the only elements of norm 1).
Step 1: parity of y. If y were even, y2+2≡2(mod4), but x3(mod4) is always 0,1, or 3 (never 2, checking x even or odd) — contradiction. So y is odd, and hence x3=y2+2 is odd, so x is odd too.
Step 2: factor.y2+2=(y+−2)(y−−2)=x3.
Step 3: the two factors are coprime. Suppose a non-unit π divides both y+−2 and y−−2. Then π divides their difference 2−2=−(−2)3 (since (−2)2=−2, so (−2)3=−2−2). The prime 2ramifies in this ring: 2=−(−2)2, so the only prime (up to units) dividing 2−2 is −2 itself. Hence π is an associate of −2, meaning −2∣y+−2, which forces −2∣y — impossible in Z unless 2∣y, contradicting Step 1 (y odd). So the two factors are coprime.
Step 4: extract a cube. In a UFD, if a product of two coprime elements is a perfect cube, each factor is (up to a unit) itself a cube. Since the only units are ±1=(±1)3, we may absorb any sign and write y+−2=(a+b−2)3
for some integers a,b. Expanding, using (−2)2=−2,(−2)3=−2−2: (a+b−2)3=a3+3a2b−2+3ab2(−2)+b3(−2−2)=(a3−6ab2)+(3a2b−2b3)−2.
Matching real and −2-coefficients: y=a(a2−6b2),1=b(3a2−2b2).
Step 5: solve the Diophantine constraint. Since b(3a2−2b2)=1 with a,b∈Z, we need b=±1.
If b=1: 3a2−2=1⇒a2=1⇒a=±1.
If b=−1: −(3a2−2)=1⇒3a2=1, no integer solution.
So b=1, a=±1. Then y=a(a2−6)=a(1−6)=−5a=∓5, i.e. y=±5, and x3=y2+2=25+2=27⟹x=3.
Conclusion: the only integer solutions are (x,y)=(3,±5). ■
Remark. This is the classical showcase (due originally to Euler, made rigorous once unique factorization in Z[−2] is established) of solving a Mordell-type equation y2+k=x3 by factoring over a quadratic ring of algebraic integers — the direct generalization of the Gaussian-integer technique of Section 4.5, and the natural cousin of Fermat's infinite-descent method in Theorem 4.4: both replace an intractable equation over Z with a tractable factorization in a larger UFD.
Part V — Advanced Tools: LTE, Primitive Roots, and Quadratic Residues
5.1 The Lifting the Exponent Lemma
Theorem 5.1 (LTE, odd prime case).Let p be an odd prime and let x,y be integers with p∤x, p∤y, and p∣(x−y). Then for every positive integer n, vp(xn−yn)=vp(x−y)+vp(n).
Proof.Step 1 (the case n=p). Write a=vp(x−y)≥1, so x=y+pam with p∤m. Factor xp−yp=(x−y)S(xp−1+xp−2y+⋯+yp−1).
We must show vp(S)=1 exactly. Modulo pa+1, using x≡y+pam: xiyp−1−i≡yp−1+iyp−2pam(modpa+1)
(expanding xi=(y+pam)i≡yi+iyi−1pam(modp2a), and since a≥1, certainly mod pa+1 too). Summing over i=0,…,p−1: S≡pyp−1+pamyp−2⋅2p(p−1)(modpa+1).
Since p is odd, 2p(p−1)=p⋅2p−1 is itself a multiple of p, so the second term is a multiple of pa+1 and vanishes mod pa+1. Hence S≡pyp−1(modpa+1); since p∤y, vp(pyp−1)=1, and as a+1≥2>1, this pins down vp(S)=1 exactly. Therefore vp(xp−yp)=a+1=vp(x−y)+1, confirming the theorem for n=p.
Step 2 (p∤n). If p∤n, factor xn−yn=(x−y)(xn−1+⋯+yn−1); the second factor is ≡nyn−1(modp) (since x≡y), and p∤nyn−1, so this factor contributes vp=0. Hence vp(xn−yn)=vp(x−y) whenever p∤n.
Step 3 (general n). Write n=pbn′ with p∤n′. By Step 2, vp(xn′−yn′)=vp(x−y), and xn′≡yn′(modp) still holds (in fact mod pa). Applying Step 1 exactly b times to the pair (xn′,yn′), each application raising the valuation by exactly 1: vp(xpbn′−ypbn′)=vp(xn′−yn′)+b=vp(x−y)+vp(n).■
Corollary 5.2 (odd-power sum case).If additionally p∣(x+y) (with p∤x,y), then for everyoddpositive integer n, vp(xn+yn)=vp(x+y)+vp(n).
(Apply Theorem 5.1 to x and −y: p∣x−(−y)=x+y, and (−y)n=−yn for odd n, so xn−(−y)n=xn+yn.)
Theorem 5.3 (LTE, p=2 case — statement).Let x,y be odd integers.
If n is odd:v2(xn−yn)=v2(x−y).
If n is even:v2(xn−yn)=v2(x−y)+v2(x+y)+v2(n)−1.
The proof follows the same binomial-expansion strategy as Theorem 5.1 but requires extra casework specific to the prime 2 (since, e.g., x−y and x+y are both even when x,y are odd, unlike the odd-prime case where only one factor need be divisible by p). We state the result and use it freely.
Worked Example 5.1. Find v3(2100+1). Since 2≡−1(mod3), we have 3∣(2+1); and 100 is even so we cannot directly apply the odd-sum corollary (which needs n odd). Instead write 2100+1... actually since 100 is even, use xn−yn form: note 2100−1100 doesn't involve +1. Let's instead compute v3(2100−1): since 3∣(2−1)=1? No — 3∤1. Reconsider: apply LTE with x=4,y=1 (since 2100=450): 3∣(4−1)=3. So v3(450−150)=v3(3)+v3(50)=1+0=1. But we want 2100+1, not 2100−1. Since 22≡1(mod3) (order 2), and 100 is even, 2100≡1(mod3), so 2100+1≡2(mod3), i.e. 3∤(2100+1) at all — v3(2100+1)=0. This example is a useful caution: always check the hypotheses of LTE (p∣x±y) before applying it — here they simply fail, and direct modular reasoning settles the question immediately.
5.2 Orders, Primitive Roots, and Indices
Definition. For gcd(a,n)=1, the order of a modulo n, written ordn(a), is the smallest positive integer k with ak≡1(modn).
Proposition 5.4.ordn(a)∣φ(n), and more generally ordn(a)∣e for any e with ae≡1(modn).
Proof. Let d=ordn(a) and suppose ae≡1. By the Division Algorithm, e=dq+r, 0≤r<d. Then 1≡ae=(ad)qar≡ar(modn). If r>0, this contradicts minimality of d; so r=0, i.e. d∣e. Taking e=φ(n) (valid by Euler's Theorem) gives d∣φ(n). ■
Definition.g is a primitive root modulo n if ordn(g)=φ(n), i.e. the powers of g generate every residue coprime to n.
Theorem 5.5 (Existence of primitive roots mod a prime).For every prime p, a primitive root modulo p exists.
Proof. For each divisor d∣(p−1), let ψ(d) be the number of elements of {1,…,p−1} with order exactly d. We show ψ(d)∈{0,φ(d)} for each d, and ∑d∣p−1ψ(d)=p−1; since also ∑d∣p−1φ(d)=p−1 (Theorem 3.5), equality ψ(d)=φ(d) for everyd is forced — in particular ψ(p−1)=φ(p−1)≥1, so a primitive root exists.
Why ψ(d)∈{0,φ(d)}: Suppose ψ(d)>0, i.e. some a has order exactly d. Then a,a2,…,ad are d distinct solutions (mod p) of xd≡1(modp) — and a polynomial of degree d has at most d roots over the field Z/pZ (a standard fact: if xd−1 had more than d roots, polynomial division by (x−r) for each root would eventually force the zero polynomial to have positive degree, a contradiction — valid since Z/pZ is an integral domain, having no zero divisors, so (x−r) divides out cleanly at each root). So {a,a2,…,ad} is the complete solution set of xd≡1, and every element of order d must be among these powers ak; a direct check shows ak has order exactly d iff gcd(k,d)=1, of which there are φ(d) values. So ψ(d)=φ(d) whenever it is positive, confirming ψ(d)∈{0,φ(d)}.
Finally ∑d∣p−1ψ(d)=p−1 since every one of 1,…,p−1 has some order dividing p−1 (Proposition 5.4). ■
Theorem 5.6 (Existence, general case — statement).A primitive root modulo n exists if and only if n∈{1,2,4,pk,2pk}for an odd prime p and k≥1.
The forward extension (from prime p to prime power pk) is proved by an explicit lifting argument: if g is a primitive root mod p, then either g or g+p is a primitive root mod p2, and once one has a primitive root mod p2 it automatically remains one mod pk for all k (a computation using the Binomial Theorem, in the same spirit as the LTE proof above). The obstruction for 2k, k≥3: the group (Z/2kZ)× is not cyclic for k≥3 but splits as Z/2×Z/2k−2 (a classical structure theorem of Gauss). We state these without full proof.
Indices (discrete logarithm). If g is a primitive root mod n, every a coprime to n is a≡gind(a)(modn) for a unique ind(a)∈{0,…,φ(n)−1}, and ind(ab)≡ind(a)+ind(b)(modφ(n)) — indices behave exactly like logarithms, converting multiplicative congruence problems into additive (linear congruence) ones.
5.3 Quadratic Residues and Euler's Criterion
Definition. For odd prime p and p∤a, a is a quadratic residue (QR) mod p if x2≡a(modp) has a solution, and a non-residue (NQR) otherwise. The Legendre symbol is (pa)=⎩⎨⎧1−10a is a QR mod pa is a NQR mod pp∣a.
Proof. Let g be a primitive root mod p (Theorem 5.5), and write a≡gk(modp). The quadratic residues are exactly the even powers of g (since g2j=(gj)2 is manifestly a square, and these 2p−1 distinct even powers must be all the QRs, as x↦x2 is exactly 2-to-1 on the p−1 nonzero residues). So a is a QR iff k is even.
Now, a(p−1)/2=gk(p−1)/2. If k is even, k(p−1)/2 is a multiple of p−1, so gk(p−1)/2≡1(modp). If k is odd, note g(p−1)/2 has order exactly 2 (its square is gp−1≡1, and it is not itself ≡1 since g has order p−1, not a divisor ≤(p−1)/2), so g(p−1)/2≡−1(modp) (the only order-2 element in (Z/pZ)×). Then a(p−1)/2=(g(p−1)/2)k≡(−1)k=−1(modp) since k is odd. Both cases match (pa). ■
Corollary 5.8 (Multiplicativity).(pab)=(pa)(pb), immediate from Euler's Criterion since (ab)(p−1)/2=a(p−1)/2b(p−1)/2.
Corollary 5.9 (First Supplementary Law).(p−1)=(−1)(p−1)/2, i.e. −1 is a QR mod p iff p≡1(mod4) — recovering Challenge Problem 2.2 by a one-line argument.
5.4 The Law of Quadratic Reciprocity
Theorem 5.10 (Quadratic Reciprocity).For distinct odd primes p,q: (qp)(pq)=(−1)2p−1⋅2q−1. Equivalently, (qp)=(pq) unless both p≡q≡3(mod4), in which case (qp)=−(pq).
Supplementary Laws. (p−1)=(−1)(p−1)/2,(p2)=(−1)(p2−1)/8, i.e. 2 is a QR mod p iff p≡±1(mod8).
On the proof. Gauss gave (over his lifetime) eight distinct proofs. The classical elementary route uses Gauss's Lemma — counting how many of the least-magnitude residues of a,2a,…,2p−1a(modp) are negative — combined with a lattice-point-counting argument (Eisenstein's version) comparing the two primes' contributions. This proof, while fully elementary, is long; we state the theorem, name the method (Gauss's Lemma + lattice-point counting), and turn to its use, which is where the real computational power lies.
Worked Example 5.2. Is 219 a QR mod 383 (383 is prime)? Factor 219=3×73, so (383219)=(3833)(38373).
For (3833): since 383≡3(mod4) and 3≡3(mod4), reciprocity flips sign: (3833)=−(3383)=−(32) (as 383≡2(mod3)). Since 3≡3(mod8), 2 is a NQR mod 3 (directly: squares mod 3 are {0,1}), so (32)=−1, giving (3833)=−(−1)=1.
For (38373):73≡1(mod4), so reciprocity does not flip sign: (38373)=(73383). Now 383=5⋅73+18, so (73383)=(7318)=(732)(739)=(732)⋅1 (as 9=32 is a perfect square). Since 73≡1(mod8), (732)=1. So (38373)=1.
Combining: (383219)=1×1=1 — 219 is a QR mod 383.■
Trick. This example illustrates the standard computational algorithm: factor out squares and small primes, apply reciprocity to flip each odd-prime symbol into a smaller modulus, and recurse — precisely mirroring the Euclidean Algorithm in structure and speed (O(logp) reciprocity/reduction steps).
5.5 Challenge Problems
Challenge Problem 5.1 (IMO 1990, Problem 3).Find all positive integers n such that n2∣2n+1.
Solution. We claim the answers are n=1 and n=3.
Step 0.2n+1 is always odd, so n2 must be odd, hence n is odd. n=1 works trivially (1∣3). Assume now n>1.
Step 1 (the smallest prime factor of n is 3). Let p be the smallest prime dividing n; p is odd. Since p∣n∣2n+1, we have 2n≡−1(modp), so 22n≡1(modp). Let d=ordp(2). Then d∣2n but d∤n (else 2n≡1, contradicting 2n≡−1≡1 as p>2); also d∣p−1 (Proposition 5.4 with φ(p)=p−1). Every prime factor of d is ≤d≤p−1<p, so — since p is the smallest prime factor of n — d shares no prime factor with n, i.e. gcd(d,n)=1. Combined with d∣2n, this forces d∣2. Since d=1 (2≡1(modp)), d=2, so p∣22−1=3: p=3.
Step 2 (exact power of 3 dividing n). Let a=v3(n)≥1. By LTE (Corollary 5.2, since 3∣(2+1) and n is odd), v3(2n+1)=v3(2+1)+v3(n)=1+a.
Since n2∣2n+1 requires v3(n2)=2a≤1+a, we get a≤1, so a=1: write n=3m with 3∤m.
Step 3 (m=1). Suppose m>1 and let q be the smallest prime factor of m; then q=3, and every prime factor of n is either 3 or ≥q (since q is the smallest prime factor of m). Let d′=ordq(2); as in Step 1, d′∣2n, d′∤n, d′∣q−1, and every prime factor of d′ is <q. So d′ can only share the prime 3 with n (all other prime factors of n being ≥q>d′'s factors). Write d′=3st with 3∤t; since d′∣2n=6m and v3(6m)=1, we get s≤1.
If s=0: gcd(d′,n)=1, so as in Step 1, d′∣2, giving d′=2, so q∣22−1=3 — contradicting q=3.
If s=1: d′=3t with t∣2m and gcd(t,m)=1 (its prime factors being <q≤ all prime factors of m), so t∣2, giving t∈{1,2}, i.e. d′∈{3,6}.
d′=3: then q∣23−1=7, so q=7. But ord7(2)=3 (since 23=8≡1(mod7)) is odd, so the cyclic subgroup generated by 2 mod 7 has odd order and cannot contain −1 (which has order 2) — so 2n≡−1(mod7) is never solvable. Contradiction.
d′=6: then q∣26−1=63=32⋅7, so q=7 again; but we just computed ord7(2)=3=6. Contradiction.
Both sub-cases fail, so m=1 is forced: n=3.
Conclusion: the solutions are n=1 and n=3 (check: 23+1=9=32✓). ■
Challenge Problem 5.2.For an odd prime p, evaluate T=∑k=0p−1(pk2+k).
Solution. The terms k=0 and k=p−1 both contribute 0 (since k2+k=k(k+1)≡0(modp) there), so T=∑k=1p−2(pk(k+1)).
For k∈{1,…,p−2}, k is invertible mod p, so we may write k(k+1)=k2(1+k−1), giving (using multiplicativity, Corollary 5.8, and (pk2)=1): (pk(k+1))=(p1+k−1).
As k ranges over {1,…,p−2} (i.e. all nonzero residues exceptp−1), its inverse k−1 also ranges over all nonzero residues except the inverse of p−1 — and (p−1)−1≡p−1(modp) (since (p−1)2≡1), so k−1 ranges over exactly the same set {1,…,p−2}, just relabeled. Substituting j=k−1: T=∑j=1p−2(p1+j)=∑i=2p−1(pi)=[∑i=1p−1(pi)]−(p1)=0−1=−1,
using the standard fact that ∑i=1p−1(pi)=0 (there are exactly 2p−1 QRs and 2p−1 NQRs, contributing equal numbers of +1's and −1's).
Conclusion:T=−1 for every odd prime p. ■
Verification (p=5): QRs mod 5 are {1,4}. Terms k=0,…,4: k(k+1)=0,2,6≡1,12≡2,20≡0, giving Legendre symbols 0,−1,1,−1,0, summing to −1. ✓
Appendix A — Master Formula Sheet
Divisibility and the Euclidean Algorithm a=bq+r,0≤r<bgcd(a,b)=gcd(b,amodb)gcd(a,b)⋅lcm(a,b)=ab ax+by=gcd(a,b) has an integer solution (Beˊzout)gcd(a,b)=1⟺∃x,y:ax+by=1
Prime Factorization n=∏ipiai(unique)gcd(a,b)=∏pmin(vpa,vpb)lcm(a,b)=∏pmax(vpa,vpb)
Chinese Remainder Theorem x≡ai(modmi)(pairwise coprime mi)⟹x≡∑aiMiyi(modM),Mi=miM,Miyi≡1(modmi)
Classical Theorems ap−1≡1(modp)(Fermat)aφ(n)≡1(modn)(Euler)(p−1)!≡−1(modp)(Wilson) ae≡aemodφ(m)(modm) if gcd(a,m)=1;ae≡aφ(m)+(emodφ(m))(modm) if e≥log2m (any a)
Every construction in this treatise is a shadow of a single algebraic fact: Z is a Euclidean domain. The Division Algorithm (Theorem 1.1) is precisely the defining property of a Euclidean domain; Bézout's Identity and unique factorization (Theorems 1.5, 1.9) are its immediate structural consequences, and every Euclidean domain is automatically a principal ideal domain (PID) and hence a unique factorization domain (UFD). When we write gcd(a,b)=ax+by, we are really exhibiting the ideal (a,b)⊆Z as the principal ideal (gcd(a,b)) — Bézout's Identity is the statement that ideals in Z are principal.
Modular arithmetic is the passage to quotient rings: Z/nZ. A prime p is precisely a value of n for which the ideal (p) is maximal (equivalently prime), and correspondingly Z/pZ is not merely a ring but a field — this is exactly why polynomials over Z/pZ have at most as many roots as their degree (used decisively in Theorem 5.5), a property that can fail over Z/nZ for composite n. The group of units (Z/nZ)× has order φ(n), and Euler's Theorem is nothing but Lagrange's Theorem from group theory ("the order of an element divides the order of the group") applied to this group — the same abstract fact that, applied to the cyclic group generated by a primitive root, structures all of Part V.
Diophantine equations, in turn, push this machinery outward: when Z itself is too small to factor an expression like x2+y2 or y2+2, we pass to a larger ring of algebraic integers — Z[i], Z[−2] — and ask whether unique factorization still holds there. When it does (as in both our examples, each a Euclidean domain in its own right), the same Bézout/UFD machinery that solved everything in Part I solves equations invisible to elementary methods. When it does not — as happens already for rings like Z[−5] — mathematicians in the 19th century were forced to invent ideals in the modern sense (Kummer's "ideal numbers," formalized by Dedekind) precisely to restore unique factorization, this time at the level of ideals rather than elements. This is the birth of algebraic number theory.
Finally, Quadratic Reciprocity — stated but not fully proved here — is now understood as the first, and simplest, instance of a vast unification called class field theory and, more generally, the Artin Reciprocity Law, which describes how primes split in abelian extensions of number fields in terms of data purely internal to the base field. Seen this way, the question "is a a square mod p?" and the question "how does p factor in Z[a]?" are the same question — the theme first glimpsed in Section 4.5, where "−1 is a QR mod p" and "p splits in Z[i]" turned out to be identical statements in different languages.
In short: discrete number theory — divisibility, congruences, quadratic residues — is the visible surface of continuous algebraic structure: rings, ideals, and groups. Every theorem in this treatise can be re-read, without changing a single line of logic, as a theorem about the ring Z; and every generalization worth pursuing next asks the same question of a larger ring.