Olympiad Number Theory II: Residues, Diophantine Equations & Factorials

Study Sheet

Olympiad Number Theory II: Residues, Diophantine Equations & Factorials

Quadratic residues, difference-of-squares counts, Legendre gaps, Euler at work

Deeper Arithmetic

Tip
Quadratic residues and the two supplements

For an odd prime pp: −1-1 is a QR iff p≡1(mod4)p \equiv 1 \pmod 4; 22 is a QR iff p≡±1(mod8)p \equiv \pm1 \pmod 8. Euler's criterion: a(p−1)/2≡(ap)a^{(p-1)/2} \equiv \left(\tfrac ap\right).

Eleven odd primes up to 100100 have −1-1 as a residue — exactly the primes that are sums of two squares (Fermat). Quadratic reciprocity (pq)(qp)=(−1)p−12q−12\left(\tfrac pq\right)\left(\tfrac qp\right) = (-1)^{\frac{p-1}{2}\frac{q-1}{2}} then decides every (ap)\left(\tfrac ap\right) by a Euclid-like descent. Olympiad use: showing an equation like x2+1≡0(modp)x^2 + 1 \equiv 0 \pmod p has no solution for p≡3(mod4)p \equiv 3 \pmod 4 kills whole families of Diophantine equations mod a well-chosen prime.

Tip
Difference of squares and factorization counts

x2−y2=Nx^2 - y^2 = N has positive solutions in bijection with factorizations N=deN = de, d<ed < e, d≡e(mod2)d \equiv e \pmod 2. For odd NN the count is τ(N)−12\tfrac{\tau(N) - 1}{2} when NN is a square, τ(N)2\tfrac{\tau(N)}{2} otherwise.

x2−y2=2025=34⋅52x^2 - y^2 = 2025 = 3^4\cdot5^2: τ=15\tau = 15, so 77 solutions (the factorization 45⋅4545\cdot45 gives y=0y = 0 and is excluded). For even NN both factors must be even, so N≡2(mod4)N \equiv 2 \pmod 4 has NO solutions — the standard parity obstruction. Every "sum/difference of squares" count is a divisor count in disguise.

Tip
Factorials: gaps in trailing zeros and Euler's theorem
n2550751001020jump 28 → 31zeros of n!
The count of trailing zeros climbs by 1 at multiples of 5, by 2 at 25, 50, 75, 100 and by 3 at 125.

v5(n!)v_5(n!) jumps by 22 at multiples of 2525 and by 33 at multiples of 125125, so some zero-counts (5,11,17,…,29,305, 11, 17, \ldots, 29, 30) never occur. Euler: aφ(n)≡1a^{\varphi(n)} \equiv 1 when gcd⁡(a,n)=1\gcd(a,n) = 1 — 2100≡1(mod125)2^{100} \equiv 1 \pmod{125}.

The smallest nn with at least 3030 trailing zeros is 125125 (v5v_5 leaps from 2828 at 124124 to 3131). Euler plus CRT computes huge powers modulo composites: 2100≡1(mod125)2^{100} \equiv 1 \pmod{125} and ≡0(mod8)\equiv 0 \pmod 8 give 2100≡376(mod1000)2^{100} \equiv 376 \pmod{1000}. Wilson's theorem (p−1)!≡−1(p-1)! \equiv -1 handles factorials modulo primes; pairing each residue with its inverse is the proof.

Side note
The mod-mm obstruction menu

Squares are 0,1 mod 40, 1 \bmod 4 and 0,1,4 mod 80, 1, 4 \bmod 8; cubes are 0,±1 mod 90, \pm1 \bmod 9 and  mod 7\bmod 7; fourth powers are 0,1 mod 160, 1 \bmod 16. Powers of 22 cycle with short periods mod small odd numbers. Before any heavy machinery, reduce a Diophantine equation modulo 4,8,9,7,164, 8, 9, 7, 16 — one of them usually shows there are no solutions, or restricts them to a residue class you can then handle.

Side note
Where it goes wrong: sums of two squares

Tempting: "nn is a sum of two squares exactly when no prime ≡3(mod4)\equiv3\pmod4 divides nn." But 9=32+029=3^2+0^2 and 18=32+3218=3^2+3^2.

What is true: nn is a sum of two squares iff every prime ≡3(mod4)\equiv3\pmod4 appears in nn to an even power. The argument behind it shows where the parity comes from: if q≡3(mod4)q\equiv3\pmod4 divides x2+y2x^2+y^2, then q∣xq\mid x and q∣yq\mid y (otherwise −1≡(x/y)2-1\equiv(x/y)^2 would be a residue mod qq), so q2q^2 divides x2+y2x^2+y^2 and one can divide out. Primes ≡3(mod4)\equiv3\pmod4 are not forbidden; they must come in pairs.

Try it
Try it: kill an equation mod 4

Show x2+y2=4z+3x^2 + y^2 = 4z + 3 has no integer solutions.

Show the answer

Squares are 00 or 1 mod 41 \bmod 4, so x2+y2∈{0,1,2} mod 4x^2 + y^2 \in \{0, 1, 2\} \bmod 4 — never 33. Done in one line; the same idea shows no integer of the form 4k+34k + 3 is a sum of two squares.

Proofs & Why It Matters

Tip
Proof: Euler's criterion

For odd prime pp and p∤ap \nmid a: a(p−1)/2≡1a^{(p-1)/2} \equiv 1 if aa is a QR, ≡−1\equiv -1 otherwise.

The multiplicative group mod pp is cyclic of order p−1p - 1 with generator gg; write a=gka = g^k. Then a(p−1)/2=gk(p−1)/2a^{(p-1)/2} = g^{k(p-1)/2}, which is 11 iff (p−1)∣kp−12(p - 1) \mid k\tfrac{p-1}{2} iff kk is even iff aa is a square (a=(gk/2)2a = (g^{k/2})^2). Since ap−1=1a^{p-1} = 1, the value a(p−1)/2a^{(p-1)/2} is a square root of 11, hence ±1\pm1. ■\blacksquare The first supplement follows immediately: (−1)(p−1)/2=1(-1)^{(p-1)/2} = 1 iff p−12\tfrac{p-1}{2} is even iff p≡1(mod4)p \equiv 1 \pmod 4.

Tip
Proof: Wilson's theorem

(p−1)!≡−1(modp)(p - 1)! \equiv -1 \pmod p for every prime pp.

In the product 1⋅2⋯(p−1)1\cdot2\cdots(p-1), pair each residue with its multiplicative inverse mod pp. A residue is its own inverse iff x2≡1x^2 \equiv 1 iff x≡±1x \equiv \pm1 (a polynomial of degree 22 over a field has at most 22 roots). All other residues pair off into products ≡1\equiv 1, leaving 1⋅(p−1)≡−11\cdot(p-1) \equiv -1. ■\blacksquare Significance: the converse holds too (nn composite ⇒(n−1)!≡0\Rightarrow (n-1)! \equiv 0 for n>4n > 4), so Wilson characterizes primes — and the pairing trick reappears in every "product of all elements of a group" problem.

Going Deeper: Worked Problems

Example
Worked: a two-squares count

How many ordered pairs of positive integers (x,y)(x, y) satisfy x2−y2=720x^2 - y^2 = 720?

Step 1 — (x−y)(x+y)=720(x - y)(x + y) = 720 with both factors of the SAME parity; since 720720 is even, both must be even.

Step 2 — write x−y=2dx - y = 2d, x+y=2ex + y = 2e: de=180de = 180 with d<ed < e.

Step 3 — 180=22⋅32⋅5180 = 2^2\cdot3^2\cdot5 has τ=18\tau = 18 divisors, so 99 pairs with d<ed < e (no square root since 180180 is not a square).

Step 4 — each gives x=d+ex = d + e, y=e−d>0y = e - d > 0: nine solutions. The parity step is where most attempts go wrong — an odd-times-even factorization of 720720 gives non-integer x,yx, y.

Example
Worked: an order argument for a divisibility claim

Show that 2n+12^n + 1 is never divisible by 77 for any positive integer nn.

Step 1 — the powers of 22 mod 77 cycle: 2,4,1,2,4,1,…2, 4, 1, 2, 4, 1, \ldots (the order of 22 mod 77 is 33).

Step 2 — so 2n mod 7∈{1,2,4}2^n \bmod 7 \in \{1, 2, 4\} and 2n+1 mod 7∈{2,3,5}2^n + 1 \bmod 7 \in \{2, 3, 5\} — never 00. ■\blacksquare

Step 3 — the general principle: p∣2n+1p \mid 2^n + 1 requires 22n≡12^{2n} \equiv 1 but 2n≢12^n \not\equiv 1, i.e. the order of 22 mod pp is exactly 2⋅(something dividing n)2\cdot(\text{something dividing } n) — an EVEN order. Mod 77 the order is 33, odd: impossible. This "order must be even" lemma settles every an+1a^n + 1 divisibility question.

Example
Worked: an explicit square root of −1-1

Let p≡1(mod4)p\equiv1\pmod4 be prime. Prove that x=(p−12)!x=\left(\tfrac{p-1}2\right)! satisfies x2≡−1(modp)x^2\equiv-1\pmod p. (folklore; a corollary of Wilson)

Step 1 — Wilson: (p−1)!≡−1(modp)(p-1)!\equiv-1\pmod p.

Step 2 — split the product: the second half is p+12,…,p−1\tfrac{p+1}2,\dots,p-1, which modulo pp are −p−12,…,−1-\tfrac{p-1}2,\dots,-1. So (p−1)!≡(p−12)!⋅(−1)(p−1)/2(p−12)!(p-1)!\equiv\left(\tfrac{p-1}2\right)!\cdot(-1)^{(p-1)/2}\left(\tfrac{p-1}2\right)!.

Step 3 — p−12\tfrac{p-1}2 is even because p≡1(mod4)p\equiv1\pmod4, so the sign is +1+1 and ((p−12)!)2≡−1\left(\left(\tfrac{p-1}2\right)!\right)^2\equiv-1. Example: p=13p=13, 6!=720=55⋅13+56!=720=55\cdot13+5, and 52=25≡−1(mod13)5^2=25\equiv-1\pmod{13}.

How to find it. Wilson's product of all residues is the square of the half-product up to the sign (−1)(p−1)/2(-1)^{(p-1)/2}. This gives a constructive proof of the first supplement (−1-1 is a residue for p≡1(mod4)p\equiv1\pmod4), which the next problem turns into sums of two squares.

Example
Worked: Fermat's two-squares theorem

Prove that every prime p≡1(mod4)p\equiv1\pmod4 is a sum of two squares. (Fermat; proof via Thue's lemma)

Step 1 — by the previous example there is an integer cc with c2≡−1(modp)c^2\equiv-1\pmod p.

Step 2 — Thue's lemma: let m=⌊p⌋m=\lfloor\sqrt p\rfloor, so m<p<m+1m<\sqrt p<m+1 (pp is not a square). Consider the (m+1)2>p(m+1)^2>p numbers cx−ycx-y with 0≤x,y≤m0\le x,y\le m. By pigeonhole two of them agree mod pp: cx1−y1≡cx2−y2cx_1-y_1\equiv cx_2-y_2. Put x=x1−x2x=x_1-x_2, y=y1−y2y=y_1-y_2; then cx≡y(modp)cx\equiv y\pmod p, ∣x∣,∣y∣≤m<p|x|,|y|\le m<\sqrt p, and (x,y)≠(0,0)(x,y)\ne(0,0) (if x=0x=0 then y≡0y\equiv0 and ∣y∣<p|y|<p force y=0y=0, and the two pairs would coincide).

Step 3 — then y2≡c2x2≡−x2y^2\equiv c^2x^2\equiv-x^2, so p∣x2+y2p\mid x^2+y^2.

Step 4 — size: 0<x2+y2<2p0<x^2+y^2<2p, so x2+y2=px^2+y^2=p. Example: p=13p=13, c=5c=5, m=3m=3: x=1x=1 gives cx≡5cx\equiv5, too large, but x=2x=2 gives 10≡−310\equiv-3, so (x,y)=(2,−3)(x,y)=(2,-3) and 13=22+3213=2^2+3^2.

How to find it. "pp divides x2+y2x^2+y^2 with x,yx,y small" is the goal, and a square root of −1-1 gives the divisibility for free along the line y≡cxy\equiv cx. Pigeonhole finds a short vector on that line, and "divisible by pp and less than 2p2p" pins the value.

Try it
Try it: a square root of −1-1 mod 17

Use the half-factorial to find an xx with x2≡−1(mod17)x^2\equiv-1\pmod{17}, and then write 1717 as a sum of two squares.

Show the answer

(17−12)!=8!=40320\left(\tfrac{17-1}2\right)!=8!=40320. Modulo 1717: 40320=2371⋅17+1340320=2371\cdot17+13, so x≡13≡−4x\equiv13\equiv-4, and indeed 42=16≡−14^2=16\equiv-1. Then 17=42+1217=4^2+1^2 — Thue's lemma with c=4c=4 gives (x,y)=(1,4)(x,y)=(1,4) at once.