How to Find a Quadratic Nonresidue Modulo a Prime p
At a glance
- 1. Test a candidate
Choose an integer a from 2 to p − 1.
- 2. Apply Euler's criterion
Compute a to the power (p − 1)/2 modulo p.
- 3. Check the result
A result of p − 1 means a is a quadratic nonresidue.
- 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 is a number that is not congruent to the square of any integer modulo . When , 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 not divisible by , Euler’s criterion states
Since is represented by , you can test a candidate by computing the modular power . If the result is , then is a nonresidue. If it is , 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 and test consecutive integers up to , skipping any multiple of . The search is guaranteed to find a nonresidue: among the nonzero residue classes modulo , exactly are squares and are nonsquares. In particular, a nonresidue exists among the candidates from through .
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 . The loop will not reach a multiple of before it finds an answer, since all the tested values are less than .
Worked example: p = 17
Here . Test first:
So 2 is a quadratic residue, not the answer. Next test :
Because , Euler’s criterion shows that 3 is a quadratic nonresidue modulo 17.
Why the condition p ≡ 1 mod 4 is not enough
The congruence tells you that is a square modulo . It does not determine whether 2, 3, or any other fixed small number is a nonresidue. For example, 2 is a nonresidue when , but it is a residue when .
A small shortcut sometimes helps. If , then 2 is a nonresidue by the supplementary law for 2. If , test another candidate with Euler’s criterion. For an odd prime with , quadratic reciprocity also gives : thus 3 is a nonresidue when , and a residue when .
These shortcuts can save tests, but the Euler-criterion search works in every case. For instance, with , 2 is a residue because , and 3 is a residue because . Testing 5 gives , so ; therefore 5 is a nonresidue.
Common mistakes
- Assuming 2 always works. Whether 2 is a residue depends on 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 ; Euler’s criterion checks that precisely.
- Using a candidate divisible by . Euler’s criterion in this form applies only when does not divide ; a multiple of is congruent to zero and is not a nonzero quadratic residue or nonresidue.
- Treating as a formula for the answer. It guarantees that is a residue, but finding another nonresidue requires testing or using additional information about .
References
- Kenneth Ireland and Michael Rosen, A Classical Introduction to Modern Number Theory
- 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.