Quadratic Residue

DOWNLOAD Mathematica Notebook

If there is an integer 0<x<p such that

 x^2=q (mod p),
(1)

i.e., the congruence (1) has a solution, then q is said to be a quadratic residue (mod p). Note that the trivial case q=0 is generally excluded from lists of quadratic residues (e.g., Hardy and Wright 1979, p. 67) so that the number of quadratic residues (mod n) is taken to be one less than the number of squares (mod n). However, other sources include 0 as a quadratic residue.

If the congruence does not have a solution, then q is said to be a quadratic nonresidue (mod p). Hardy and Wright (1979, pp. 67-68) use the shorthand notations q R p and q N p, to indicated that q is a quadratic residue or nonresidue, respectively.

In practice, it suffices to restrict the range to 0<x<=|_p/2_|, where |_x_| is the floor function, because of the symmetry (p-x)^2=x^2 (mod p).

For example, 4^2=6 (mod 10), so 6 is a quadratic residue (mod 10). The entire set of quadratic residues (mod 10) are given by 1, 4, 5, 6, and 9, since

1^2=1 (mod 10)  2^2=4 (mod 10)  3^2=9 (mod 10)
(2)
4^2=6 (mod 10)  5^2=5 (mod 10)  6^2=6 (mod 10)
(3)
7^2=9 (mod 10)  8^2=4 (mod 10)  9^2=1 (mod 10),
(4)

making the numbers 2, 3, 7, and 8 the quadratic nonresidues (mod 10).

Quadratic residues

A list of quadratic residues for p<=20 is given below (OEIS A046071), with those numbers <p not in the list being quadratic nonresidues of p.

pquadratic residues
1(none)
21
31
41
51, 4
61, 3, 4
71, 2, 4
81, 4
91, 4, 7
101, 4, 5, 6, 9
111, 3, 4, 5, 9
121, 4, 9
131, 3, 4, 9, 10, 12
141, 2, 4, 7, 8, 9, 11
151, 4, 6, 9, 10
161, 4, 9
171, 2, 4, 8, 9, 13, 15, 16
181, 4, 7, 9, 10, 13, 16
191, 4, 5, 6, 7, 9, 11, 16, 17
201, 4, 5, 9, 16
QuadraticResidueNumbers

The numbers of quadratic residues (mod n) for n=1, 2, ... are 0, 1, 1, 1, 2, 3, 3, 2, 3, 5, 5, 3, 6, 7, 5, 3, ... (OEIS A105612).

Largest quadratic residue binary plot

The largest quadratic residues for p=2, 3, ... are 1, 1, 1, 4, 4, 4, 4, 7, 9, 9, 9, 12, 11, ... (OEIS A047210).

Care must be taken when dealing with quadratic residues, as slightly different definitions are also apparently sometimes used. For example, Stangl (1996) adopts the apparently nonstandard definition of quadratic residue as an integer x satisfying 0<x<p such that x^2=q (mod p) and x is relatively prime to p. This definition therefore excludes non-units (mod p). By this definition, the quadratic residues (mod n) for n=1, 2, ... are illustrated below (OEIS A096103, the numbers of them are given by 0, 1, 1, 1, 2, 1, 3, 1, 3, 2, 5, 1, 6, ... (OEIS A046073) and the number of squares s(n) in Z_n is related to the number q(n) of quadratic residues in Z_n by

 q(p^n)=s(p^n)-s(p^(n-2))
(5)

for n>=3 and p an odd prime (Stangl 1996). (Note that both q and s are multiplicative functions.)

nnon-unit squares (mod n)
21
31
41
51, 4
61
71, 2, 4
81
91, 4, 7

Given an odd prime p and an integer a, then the Legendre symbol is given by

 (a/p)={1   if a is a quadratic residue mod p; -1   otherwise.
(6)

If

 r^((p-1)/2)=+/-1 (mod p),
(7)

then r is a quadratic residue (+) or nonresidue (-). This can be seen since if r is a quadratic residue of p, then there exists a square x^2 such that r=x^2 (mod p), so

 r^((p-1)/2)=(x^2)^((p-1)/2)=x^(p-1) (mod p),
(8)

and x^(p-1) is congruent to 1 (mod p) by Fermat's little theorem.

Given p and q in the congruence

 x^2=q (mod p),
(9)

x can be explicitly computed for p and q of certain special forms:

 x={q^(k+1) (mod p)   for p=4k+3; q^(k+1) (mod p)   for p=8k+5 and q^(2k+1)=1 (mod p); 1/2(4q)^(k+1)(p+1) (mod p)   for p=8k+5 and q^(2k+1)=-1 (mod p).
(10)

For example, the first form can be used to find x given the quadratic residues q=1, 3, 4, 5, and 9 (mod p=11, having k=2), whereas the second and third forms determine x given the quadratic residues q=1, 3, 4, 9, 10, and 12 (mod p=13, having k=1), and q=1, 3, 4, 7, 9, 10, 11, 12, 16, 21, 25, 26, 27, 28, 30, 33, 34, 36 (mod p=37, having k=4).

More generally, let q be a quadratic residue modulo an odd prime p. Choose h such that the Legendre symbol (h^2-4q/p)=-1. Then defining

V_1=h
(11)
V_2=h^2-2q
(12)
V_i=hV_(i-1)-qV_(i-2)    for i>=3,
(13)

gives

V_(2i)=V_i^2-2q^i
(14)
V_(2i+1)=V_iV_(i+1)-hq^i,
(15)

and a solution to the quadratic congruence is

 x=1/2(p+1)V_((p+1)/2) (mod p).
(16)

Schoof (1985) gives an algorithm for finding x with running time O(ln^(10)n) (Hardy et al. 1990). The congruence can solved by the Wolfram Language command PowerMod[q, 1/2, p].

The following table gives the primes which have a given number d as a quadratic residue.

dprimes
-624k+1,5,7,11
-520k+1,3,7,9
-36k+1
-28k+1,3
-14k+1
28k+/-1
312k+/-1
510k+/-1
624k+/-1,5

Finding the continued fraction of a square root sqrt(D) and using the relationship

 Q_n=(D-P_n^2)/(Q_(n-1))
(17)

for the nth convergent P_n/Q_n gives

 P_n^2=-Q_nQ_(n-1) (mod D).
(18)

Therefore, -Q_nQ_(n-1) is a quadratic residue of D. But since Q_1=1, -Q_2 is a quadratic residue, as must be -Q_2Q_3. But since -Q_2 is a quadratic residue, so is Q_3, and we see that (-1)^(n-1)Q_n are all quadratic residues of D. This method is not guaranteed to produce all quadratic residues, but can often produce several small ones in the case of large D, enabling D to be factored.

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.