Skip to content

Quadratic Residues

Updated · Equations updated; Sections updated; Examples or practice updated; Explanations expanded What changed?
  1. — Equations updated; Sections updated; Examples or practice updated; Explanations expanded

An integer rr is a quadratic residue modulo mm if x2≡r(modm)x^2\equiv r\pmod m has a solution. Some conventions reserve the term for nonzero residues, so we will explicitly say “nonzero quadratic residue” when needed.

Listing squares modulo a small integer is often enough to rule out an equation:

ModulusPossible square residues
330,10,1
440,10,1
550,1,40,1,4
880,1,40,1,4
16160,1,4,90,1,4,9

Because x2≡(−x)2x^2\equiv(-x)^2, only about half the inputs need to be checked.

Example. Prove that x2=8y+3x^2=8y+3 has no integer solutions.

The right side is three modulo eight, but the possible square residues modulo eight are zero, one, and four. The equation is impossible.


For an odd prime pp, exactly (p−1)/2(p-1)/2 nonzero residue classes are squares. The nonzero inputs pair as xx and −x-x, and no other repetition occurs: if x2≡y2x^2\equiv y^2, then p∣(x−y)(x+y)p\mid(x-y)(x+y), so x≡yx\equiv y or x≡−yx\equiv-y.

Euler’s criterion says that for p∤ap\nmid a,

a(p−1)/2≡{1(modp),a is a quadratic residue,−1(modp),a is a nonresidue.a^{(p-1)/2}\equiv \begin{cases} 1\pmod p,&a\text{ is a quadratic residue},\\ -1\pmod p,&a\text{ is a nonresidue}. \end{cases}

It gives a test without searching for a square root. One justification is that every nonzero square satisfies the first equation by Fermat’s theorem. There are already (p−1)/2(p-1)/2 such roots, the maximum possible for that polynomial; every remaining input has value −1-1 because its value squared is one.

Example. Determine whether 55 is a square modulo 1313.

Compute by squaring:

52≡−1(mod13),56≡(−1)3≡−1(mod13).5^2\equiv-1\pmod{13},\qquad5^6\equiv(-1)^3\equiv-1\pmod{13}.

Euler’s criterion says that five is a nonresidue. Thus x2≡5(mod13)x^2\equiv5\pmod{13} has no solution.

The test determines existence, not the square root itself. If a root is needed, a short residue table can still be the fastest method for a small prime.


For an odd prime pp, the number −1-1 is a quadratic residue exactly when p≡1(mod4)p\equiv1\pmod4. Euler’s criterion gives

(−1)(p−1)/2=1(-1)^{(p-1)/2}=1

exactly in that case. Equivalently, a square root of negative one has multiplicative order four, forcing 4∣p−14\mid p-1.

Example. An odd prime pp divides n2+1n^2+1. Prove that p≡1(mod4)p\equiv1\pmod4.

Since n2≡−1(modp)n^2\equiv-1\pmod p, we have p∤np\nmid n and negative one is a square modulo pp. The criterion above therefore gives p≡1(mod4)p\equiv1\pmod4.


If q≡3(mod4)q\equiv3\pmod4 is prime and q∣a2+b2q\mid a^2+b^2, then qq divides both aa and bb. Otherwise, if q∤bq\nmid b, division by b2b^2 would give (ab−1)2≡−1(modq)(ab^{-1})^2\equiv-1\pmod q, which is impossible. Thus q∣bq\mid b, and the original congruence then gives q∣aq\mid a.

Removing the resulting factor of q2q^2 and repeating shows that every prime congruent to three modulo four appears to an even exponent in a nonzero sum of two integer squares.

Example. Decide whether 20232023 can be written as a2+b2a^2+b^2 for integers a,ba,b.

Factor 2023=7⋅1722023=7\cdot17^2. The prime seven is three modulo four and has odd exponent one. The necessary condition fails, so no representation exists.


The product identity

(a2+b2)(c2+d2)=(ac−bd)2+(ad+bc)2(a^2+b^2)(c^2+d^2)=(ac-bd)^2+(ad+bc)^2

lets us multiply representations. It is also the modulus identity for (a+bi)(c+di)(a+bi)(c+di).

Fermat’s two-square theorem, also called Fermat’s Christmas theorem, says that a prime p≡1(mod4)p\equiv1\pmod4 is a sum of two integer squares. Together with 2=12+122=1^2+1^2, the product identity, and the obstruction above, it gives the full criterion:

A positive integer is a sum of two integer squares, allowing zero, exactly when every prime divisor congruent to three modulo four has even exponent. The existence direction for primes is a deeper theorem; the small-modulus obstruction alone does not prove it.

Example. Find two different representations of 6565 as a sum of two squares.

Use 5=12+225=1^2+2^2 and 13=22+3213=2^2+3^2. The product identity gives

65=(1⋅2−2⋅3)2+(1⋅3+2⋅2)2=42+72.65=(1\cdot2-2\cdot3)^2+(1\cdot3+2\cdot2)^2=4^2+7^2.

Changing the sign of the second imaginary component gives another representation:

65=(1⋅2+2⋅3)2+(2⋅2−1⋅3)2=82+12.65=(1\cdot2+2\cdot3)^2+(2\cdot2-1\cdot3)^2=8^2+1^2.

For an odd prime pp, the Legendre symbol packages the quadratic-residue test:

(ap)={0,p∣a,1,a is a nonzero square modulo p,−1,a is a nonsquare modulo p.\left(\frac ap\right)= \begin{cases} 0,&p\mid a,\\ 1,&a\text{ is a nonzero square modulo }p,\\ -1,&a\text{ is a nonsquare modulo }p. \end{cases}

The notation is a symbol, not an ordinary fraction. Its numerator can be reduced modulo the denominator, and it is multiplicative:

(abp)=(ap)(bp).\left(\frac{ab}{p}\right)=\left(\frac ap\right)\left(\frac bp\right).

For nonzero residues, this follows from Euler’s criterion by multiplying the corresponding powers. Cases involving zero are immediate. Thus a product of two nonresidues is a residue, while multiplying a residue by a nonresidue produces a nonresidue.

Example. Modulo seven, both three and five are nonresidues. Determine whether fifteen is a residue.

Multiplicativity gives (15/7)=(3/7)(5/7)=1(15/7)=(3/7)(5/7)=1. Since fifteen is one modulo seven, this agrees with the direct square 121^2. The example illustrates that “nonsquare times nonsquare” behaves differently from ordinary positive integers.


Quadratic Reciprocity and the Supplementary Laws

Section titled “Quadratic Reciprocity and the Supplementary Laws”

For distinct odd primes p,qp,q, quadratic reciprocity states

(pq)(qp)=(−1)(p−1)(q−1)/4.\left(\frac pq\right)\left(\frac qp\right) =(-1)^{(p-1)(q-1)/4}.

The sign changes exactly when both primes are three modulo four. Otherwise the two symbols are equal. The supplementary laws are

(−1p)=(−1)(p−1)/2,(2p)=(−1)(p2−1)/8.\left(\frac{-1}{p}\right)=(-1)^{(p-1)/2},\qquad \left(\frac2p\right)=(-1)^{(p^2-1)/8}.

Thus two is a residue exactly when p≡1p\equiv1 or 7(mod8)7\pmod8. These are deeper theorems; their role here is to reduce a residue calculation to smaller primes. They do not find an actual square root automatically.

Example. Determine whether six is a square modulo one hundred one.

Factor the numerator: (6/101)=(2/101)(3/101)(6/101)=(2/101)(3/101). Since 101≡5(mod8)101\equiv5\pmod8, the first symbol is negative one.

Because 101≡1(mod4)101\equiv1\pmod4, reciprocity gives (3/101)=(101/3)=(2/3)=−1(3/101)=(101/3)=(2/3)=-1. Their product is one, so six is a square modulo 101.

Indeed, 392=1521≡6(mod101)39^2=1521\equiv6\pmod{101}, providing an explicit check.

Example. Determine whether seven is a square modulo twenty-nine and find its roots if it is.

Reciprocity has no sign change because twenty-nine is one modulo four:

(729)=(297)=(17)=1.\left(\frac7{29}\right)=\left(\frac{29}7\right)=\left(\frac17\right)=1.

A short search gives 62=36≡7(mod29)6^2=36\equiv7\pmod{29}. The two roots are six and negative six, or residues six and twenty-three. A prime modulus gives at most two roots of a nonzero quadratic equation.

Do not apply these prime-denominator tests directly to an arbitrary composite modulus. Solve the congruence modulo its prime powers and combine compatible roots with CRT. A composite-symbol analogue can equal one without guaranteeing that a square root exists.


A Gaussian integer is a+bia+bi with integers a,ba,b. Its norm is

N(a+bi)=a2+b2.N(a+bi)=a^2+b^2.

The norm is multiplicative because complex moduli multiply. Thus finding a sum-of-two-squares representation is equivalent to finding a Gaussian integer with a prescribed norm.

Gaussian integers admit a Euclidean algorithm. Given z,wz,w with w≠0w\ne0, approximate the real and imaginary parts of z/wz/w by nearest integers to obtain a Gaussian integer qq. Each coordinate error is at most one-half, so the remainder r=z−qwr=z-qw satisfies N(r)≤N(w)/2<N(w)N(r)\le N(w)/2<N(w) unless it is zero.

This norm decrease proves termination just as the ordinary Euclidean algorithm does. It also supplies a Gaussian Bézout identity for the GCD. These facts explain why a square root of negative one modulo a prime can be converted into a sum of two squares.

Example. Use the root 52≡−1(mod13)5^2\equiv-1\pmod{13} to construct a sum-of-two-squares representation of thirteen.

Compute a Gaussian GCD of thirteen and 5+i5+i. A convenient division is

13=2(5+i)+(3−2i).13=2(5+i)+(3-2i).

The next division is exact:

5+i=(1+i)(3−2i).5+i=(1+i)(3-2i).

Thus a Gaussian GCD is 3−2i3-2i, whose norm is thirteen. Hence 13=32+2213=3^2+2^2.

The calculation turns the modular root into a Euclidean remainder with the required norm.


Why a Prime That Is One Modulo Four Is a Sum of Squares

Section titled “Why a Prime That Is One Modulo Four Is a Sum of Squares”

Let p≡1(mod4)p\equiv1\pmod4 be prime, and choose tt with t2≡−1(modp)t^2\equiv-1\pmod p. Such a root exists by the negative-one criterion; Wilson’s theorem also constructs one.

Take a Gaussian GCD δ=gcd⁡(p,t+i)\delta=\gcd(p,t+i). Its norm divides p2p^2, because δ\delta divides pp, so the possible norms are 1,p,p21,p,p^2.

The norm cannot be one. Otherwise Gaussian Bézout gives a combination of pp and t+it+i equal to one. Multiply it by t−it-i. Since (t+i)(t−i)=t2+1(t+i)(t-i)=t^2+1 is divisible by pp, this would force pp to divide t−it-i as a Gaussian integer, which is impossible because its imaginary coefficient is negative one.

The norm cannot be p2p^2 either. In that case p/δp/\delta has norm one, making δ\delta an associate of pp. Then pp would divide t+it+i, again impossible from the imaginary coefficient.

Therefore N(δ)=pN(\delta)=p. Writing δ=a+bi\delta=a+bi yields p=a2+b2p=a^2+b^2. This proves the prime existence theorem using the Euclidean structure of Gaussian integers, rather than inferring existence from a modular test alone.


Constructing Representations of Composite Numbers

Section titled “Constructing Representations of Composite Numbers”

Once the prime-factor criterion passes, multiply representations using the two-square identity. Prime factors congruent to three modulo four have even exponents and can be absorbed into a common integer square. Powers of two use 2=12+122=1^2+1^2, while primes congruent to one modulo four use their individual representations.

Example. Construct a representation of 325325 as a sum of two squares.

Since 325=52⋅13325=5^2\cdot13, use 5=12+225=1^2+2^2 and 13=22+3213=2^2+3^2. One simple representation comes from multiplying the known representation of thirteen by the square twenty-five:

325=102+152.325=10^2+15^2.

Using a nontrivial representation 25=32+4225=3^2+4^2 instead, the product identity gives

325=(3⋅2−4⋅3)2+(3⋅3+4⋅2)2=62+172.325=(3\cdot2-4\cdot3)^2+(3\cdot3+4\cdot2)^2=6^2+17^2.

Changing the conjugate choice gives 325=12+182325=1^2+18^2. Different Gaussian factorizations can produce distinct integer representations.

The distinction between an existence criterion and a construction matters. Factoring the number proves that a representation exists, while multiplying explicit prime representations produces one. A numerical search may be quicker for small numbers, but the structural method scales and explains the result.


Using Residues as a Filter Rather Than a Final Answer

Section titled “Using Residues as a Filter Rather Than a Final Answer”

A congruence solution is necessary for an integer equation, but generally not sufficient. Passing tests modulo three, five, and eight does not prove that a square or sum-of-squares representation exists. It merely eliminates some obstructions.

For instance, an integer can be a square modulo several small moduli and still lie strictly between consecutive integer squares. Combine residue information with bounds, factorization, or valuation arguments before declaring a classification complete.

Likewise, if a prime three modulo four divides a sum of squares, the forced common divisor may invite descent. Divide by its square and repeat until the valuation is exhausted. This proves the even-exponent condition and can turn an equation into an impossibility argument when primitivity is assumed.


Primitive Representations and Forced Common Factors

Section titled “Primitive Representations and Forced Common Factors”

A sum-of-two-squares representation is primitive when its two entries are coprime. The ordinary existence criterion allows square factors of primes three modulo four, but such a prime forces a common divisor in every representation.

Example. Find all integer representations of forty-five as a sum of two squares and determine whether any is primitive.

Since three divides x2+y2=45x^2+y^2=45 and is three modulo four, it divides both variables. Write x=3u,y=3vx=3u,y=3v. The equation becomes u2+v2=5u^2+v^2=5.

The only absolute-value pair is one and two. Thus all representations of forty-five have absolute values three and six, with independent signs and either order. There are eight ordered representations, and none is primitive because every pair has common divisor three.

The factorization criterion proves existence because 45=32⋅545=3^2\cdot5 has an even exponent on three. The same prime exponent nevertheless prevents primitivity. These are different questions.

A factor of four also prevents a primitive representation: squares are zero or one modulo four, so a sum divisible by four forces both variables even. Hence a primitive sum of two squares contains no prime three modulo four and has at most one factor of two.


Constructing a Right Triangle with Prime Hypotenuse

Section titled “Constructing a Right Triangle with Prime Hypotenuse”

If a prime p=u2+v2p=u^2+v^2, the product identity gives

p2=(u2−v2)2+(2uv)2.p^2=(u^2-v^2)^2+(2uv)^2.

Thus a representation of the prime produces an integer right triangle with hypotenuse pp. For an odd prime, the two entries are coprime and of opposite parity, so the resulting triangle is primitive.

For example, 13=32+2213=3^2+2^2 produces legs five and twelve. This connects the prime residue condition p≡1(mod4)p\equiv1\pmod4 to the existence of primitive right triangles with that prime as hypotenuse.

A prime three modulo four cannot occur as the hypotenuse of a primitive integer right triangle: its square being a sum of two squares would force both legs divisible by that prime. This contradiction is another use of the same residue obstruction.