Maths WorkshopMaths problems with full solutions
Exercise 1996Number theory › CongruencesOlympiadAdded 5 Nov 2026

Every prime ≡ 1 (mod 4) is a sum of two squares

#sum of two squares#Thue's lemma#pigeonhole#Wilson

Statement

Let pp be a prime with p≡1(mod4)p\equiv1\pmod4. Prove that p=a2+b2p=a^2+b^2 for some positive integers a,ba,b.

Hint

First find xx with x2≡−1(modp)x^2\equiv-1\pmod p (use Wilson's theorem). Then apply the pigeonhole principle to the numbers u+xvu+xv with 0≤u,v≤⌊p⌋0\le u,v\le\lfloor\sqrt p\rfloor.

Show solutionHide solution

Solution

Step 1: −1-1 is a square modulo pp. Let m=p−12m=\frac{p-1}2, which is even. By Wilson, −1≡(p−1)!=∏k=1mk(p−k)≡∏k=1mk(−k)=(−1)m(m!)2=(m!)2(modp)-1\equiv(p-1)!=\prod_{k=1}^m k(p-k)\equiv\prod_{k=1}^mk(-k)=(-1)^m(m!)^2=(m!)^2\pmod p. So x=m!x=m! satisfies x2≡−1x^2\equiv-1.

Step 2: Thue's lemma. Let s=⌊p⌋s=\lfloor\sqrt p\rfloor. Since pp is prime, it is not a square, so s<p<s+1s<\sqrt p<s+1 and (s+1)2>p(s+1)^2>p. Consider the (s+1)2(s+1)^2 numbers
u+xv,0≤u,v≤s.u+xv,\qquad0\le u,v\le s.
There are more of them than residues modulo pp, so two different pairs (u,v)≠(u′,v′)(u,v)\neq(u',v') satisfy u+xv≡u′+xv′(modp)u+xv\equiv u'+xv'\pmod p. Put A=u−u′A=u-u', B=v′−vB=v'-v. Then A≡xB(modp)A\equiv xB\pmod p, ∣A∣,∣B∣≤s<p|A|,|B|\le s<\sqrt p, and (A,B)≠(0,0)(A,B)\ne(0,0).

Step 3: conclusion. A2≡x2B2≡−B2A^2\equiv x^2B^2\equiv-B^2, so p∣A2+B2p\mid A^2+B^2. Moreover 0<A2+B2<p+p=2p0<A^2+B^2<p+p=2p. Hence A2+B2=pA^2+B^2=p. Neither AA nor BB is 00 (otherwise pp would be a square), so p=∣A∣2+∣B∣2p=|A|^2+|B|^2 with positive integers.

Check. p=13p=13: x=6!=720≡5x=6!=720\equiv5, 25≡−125\equiv-1 ✓, and 13=4+913=4+9. p=29=4+25p=29=4+25.

Lesson. Pigeonhole on a lattice of values forces a short vector in a congruence class (Thue's lemma); combined with x2≡−1x^2\equiv-1 it produces a sum of two squares.