Ma · Mathematics

How to Find a Quadratic Nonresidue Modulo a Prime p

Published By the 9to5Science editorial team

At a glance

  1. 1. Test a candidate

    Choose an integer a from 2 to p − 1.

  2. 2. Apply Euler's criterion

    Compute a to the power (p − 1)/2 modulo p.

  3. 3. Check the result

    A result of p − 1 means a is a quadratic nonresidue.

  4. 4. Try the next value

    If the result is 1, test another candidate; the search must succeed.

Title: How to Find a Quadratic Nonresidue Modulo a Prime p Category: mathematics

A quadratic nonresidue modulo an odd prime pp is a number that is not congruent to the square of any integer modulo pp. When p≡1(mod4)p \equiv 1 \pmod 4, there is no single candidate that works for every prime, but Euler’s criterion gives a direct test. Try candidates and stop as soon as the test identifies one.

Test candidates with Euler’s criterion

For any integer aa not divisible by pp, Euler’s criterion states

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

Since −1(modp)-1 \pmod p is represented by p−1p-1, you can test a candidate by computing the modular power a(p−1)/2 mod pa^{(p-1)/2} \bmod p. If the result is p−1p-1, then aa is a nonresidue. If it is 11, aa is a residue, so try another value.

Use modular exponentiation rather than calculating the full, potentially enormous integer power. Standard repeated-squaring methods compute the remainder efficiently.

A guaranteed search procedure

Start with a=2a=2 and test consecutive integers up to p−1p-1, skipping any multiple of pp. The search is guaranteed to find a nonresidue: among the p−1p-1 nonzero residue classes modulo pp, exactly (p−1)/2(p-1)/2 are squares and (p−1)/2(p-1)/2 are nonsquares. In particular, a nonresidue exists among the candidates from 22 through p−1p-1.

In pseudocode:

for a = 2, 3, ..., p - 1:
    r = a^((p - 1)/2) mod p
    if r = p - 1:
        return a

The exponentiation and remainder are taken modulo pp. The loop will not reach a multiple of pp before it finds an answer, since all the tested values are less than pp.

Worked example: p = 17

Here (p−1)/2=8(p-1)/2=8. Test a=2a=2 first:

28=256≡1(mod17).2^8 = 256 \equiv 1 \pmod{17}.

So 2 is a quadratic residue, not the answer. Next test a=3a=3:

32≡9,34≡13,38≡132=169≡16(mod17).3^2 \equiv 9,\qquad 3^4 \equiv 13,\qquad 3^8 \equiv 13^2 = 169 \equiv 16 \pmod{17}.

Because 16≡−1(mod17)16 \equiv -1 \pmod{17}, Euler’s criterion shows that 3 is a quadratic nonresidue modulo 17.

Why the condition p ≡ 1 mod 4 is not enough

The congruence p≡1(mod4)p \equiv 1 \pmod 4 tells you that −1-1 is a square modulo pp. It does not determine whether 2, 3, or any other fixed small number is a nonresidue. For example, 2 is a nonresidue when p≡5(mod8)p \equiv 5 \pmod 8, but it is a residue when p≡1(mod8)p \equiv 1 \pmod 8.

A small shortcut sometimes helps. If p≡5(mod8)p \equiv 5 \pmod 8, then 2 is a nonresidue by the supplementary law for 2. If p≡1(mod8)p \equiv 1 \pmod 8, test another candidate with Euler’s criterion. For an odd prime p≠3p \ne 3 with p≡1(mod4)p \equiv 1 \pmod 4, quadratic reciprocity also gives (3/p)=(p/3)(3/p)=(p/3): thus 3 is a nonresidue when p≡2(mod3)p \equiv 2 \pmod 3, and a residue when p≡1(mod3)p \equiv 1 \pmod 3.

These shortcuts can save tests, but the Euler-criterion search works in every case. For instance, with p=13p=13, 2 is a residue because 26≡1(mod13)2^6 \equiv 1 \pmod{13}, and 3 is a residue because 36≡1(mod13)3^6 \equiv 1 \pmod{13}. Testing 5 gives 52≡−1(mod13)5^2 \equiv -1 \pmod{13}, so 56≡(−1)3≡−1(mod13)5^6 \equiv (-1)^3 \equiv -1 \pmod{13}; therefore 5 is a nonresidue.

Common mistakes

  • Assuming 2 always works. Whether 2 is a residue depends on pp modulo 8.
  • Calling a number a nonresidue because it is not visibly a square as an integer. The question is whether it is congruent to a square modulo pp; Euler’s criterion checks that precisely.
  • Using a candidate divisible by pp. Euler’s criterion in this form applies only when pp does not divide aa; a multiple of pp is congruent to zero and is not a nonzero quadratic residue or nonresidue.
  • Treating p≡1(mod4)p \equiv 1 \pmod 4 as a formula for the answer. It guarantees that −1-1 is a residue, but finding another nonresidue requires testing or using additional information about pp.

References

  1. Kenneth Ireland and Michael Rosen, A Classical Introduction to Modern Number Theory
  2. Ivan Niven, Herbert S. Zuckerman, and Hugh L. Montgomery, An Introduction to the Theory of Numbers

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.