Quadratic Residue Calculator

Number Theory

Residue statusQuadratic residue
Residue set mod 7{1, 2, 4}
Number of distinct residues3
a mod m2
How this was calculated
  1. Square every integer from 1 to 6 and reduce mod 7: the distinct squares form the residue set {1, 2, 4}.
  2. Reduce a mod m: 2 mod 7 = 2.
  3. Check membership: 2 appears in the residue set, so 2 is a quadratic residue mod 7.
  4. Count the set: there are 3 distinct quadratic residues mod 7, not counting 0.

An integer a is called a quadratic residue modulo m when there is some number x whose square leaves the remainder a after division by m. In other words, the congruence x squared is congruent to a (mod m) has a solution. If no such x exists, a is a quadratic non-residue. This calculator answers the question by the most direct method possible: it squares every integer from 1 to m - 1, reduces each square modulo m, and collects the distinct remainders into the residue set. Your value a is then reduced mod m and checked against that set.

Working with the full set, rather than a yes-or-no symbol, is instructive. Modulo 7 the squares of 1 through 6 are 1, 4, 2, 2, 4 and 1, so the distinct residue set is {1, 2, 4} and there are exactly 3 residues. Modulo 11 the set is {1, 3, 4, 5, 9}, with 5 elements. Seeing the set makes the classic pattern visible: for an odd prime p, exactly half of the numbers 1 to p - 1, that is (p - 1) / 2 of them, are residues.

The concept dates to Euler and Gauss, whose law of quadratic reciprocity relates the residue status of two primes to each other and remains one of the jewels of number theory. You do not need reciprocity to use this page - enumeration settles every case here directly - but reciprocity explains why the patterns you see in the residue sets are so regular, and it is the tool professionals reach for when the modulus has dozens of digits.

Worked examples

Two modulo seven

(2 · 7)

Residue statusQuadratic residue
Residue set mod 7{1, 2, 4}
Number of distinct residues3
a mod m2

Three modulo seven

(3 · 7)

Residue statusQuadratic non-residue
Residue set mod 7{1, 2, 4}
Number of distinct residues3
a mod m3

Five modulo eleven

(5 · 11)

Residue statusQuadratic residue
Residue set mod 11{1, 3, 4, 5, 9}
Number of distinct residues5
a mod m5

How the quadratic residue test works

The method is exhaustive enumeration. For each x from 1 to m - 1 the calculator computes x squared mod m and keeps the distinct values. It then reduces a mod m - so a negative a or an a larger than m is handled automatically - and reports whether the reduced value appears in the set. The output shows the residue status, the complete residue set, the count of distinct residues, and a mod m itself, so you can see exactly why the answer came out as it did.

Enumeration is practical for the moduli used in coursework and most textbook problems, and this calculator accepts moduli up to 1000. For very large prime moduli, mathematicians switch to the Legendre symbol and Euler's criterion, which answer the same membership question for a single a without listing the whole set. The two approaches always agree on prime moduli; the enumeration here simply shows more of the structure.

Reading the residue set

The residue set has a few properties worth noticing. It never contains 0, because x ranges from 1 to m - 1 and none of those squares is a multiple of m when m is prime. The set is symmetric: x and m - x give the same square, which is why the squares repeat in pairs (1 and 6 both give 1 mod 7; 2 and 5 both give 4). That pairing is exactly why a prime modulus p has (p - 1) / 2 distinct residues rather than p - 1.

For composite moduli the count is less tidy, which is one reason the enumeration approach is valuable: it makes no assumption that m is prime. Mod 8, for example, the squares collapse to {1, 4} plus 0, a much sparser pattern than any prime of similar size. Comparing sets across moduli is a good way to build intuition before meeting the Legendre and Jacobi symbols.

Quadratic residues versus the Legendre symbol

The Legendre symbol (a/p) compresses the membership question into a single value: 1 if a is a residue mod p, -1 if it is a non-residue, and 0 if p divides a. It is the right tool for large primes and for proofs, because laws such as quadratic reciprocity let you evaluate it by flipping and reducing the symbol instead of squaring anything. This calculator deliberately does something different: it works for any modulus, prime or composite, and shows the entire set of residues, which the Legendre symbol cannot do.

If you only need to know about one a modulo a large prime, a Legendre symbol calculator is faster. If you are learning the concept, checking homework, or exploring how the residue pattern changes with the modulus, the full set displayed here is more informative. The two pages complement each other, and their answers agree wherever both apply.

Where quadratic residues are used

Quadratic residues sit underneath a surprising amount of modern cryptography. The Rabin cryptosystem bases its security on the difficulty of taking square roots modulo a composite number, which is exactly the residue-membership problem run in reverse. Quadratic-residuosity assumptions also appear in zero-knowledge proofs and in the Goldwasser-Micali cryptosystem, one of the earliest semantic-security constructions.

In pure number theory, residues drive primality tests, the theory of binary quadratic forms, and algorithms for solving polynomial congruences. They are also a standard early topic in undergraduate number theory courses, where computing small sets like {1, 2, 4} mod 7 by hand is a rite of passage - this calculator lets you check that hand work instantly.

Checking your hand calculations

Students meeting residues for the first time usually compute a table of squares by hand. The fastest sanity check is the count: for a prime modulus p the set must contain exactly (p - 1) / 2 values, so if your hand list mod 11 has 4 or 6 entries rather than 5, an arithmetic slip has crept in. The set must also be closed under the pairing x and p - x, so each value you find should arise from exactly two square roots (unless it comes from x = 0, which is excluded here).

A second useful check is multiplicativity: the product of two residues is always a residue, and the product of two non-residues is also a residue, while a residue times a non-residue is a non-residue. Mod 7, for instance, 2 and 4 are residues and their product 8 is congruent to 1, which is indeed in {1, 2, 4}; the non-residues 3 and 5 multiply to 15, congruent to 1, again a residue. If your table violates these rules, it is wrong somewhere.

Common questions and edge cases

Two inputs often cause confusion. First, a = 0 or any multiple of m: after reduction, a mod m is 0, which is never in the enumerated set, so the calculator reports a non-residue with a mod m equal to 0 - the mathematically precise statement is that 0 is a square (of 0 itself) but is conventionally listed separately from the residues proper, and this page follows that convention. Second, m = 2: the only square is 1 mod 2, so the set is {1} and every odd a is a residue.

Large a values are reduced before testing, so entering a = 100 mod 7 tests 2, since 100 mod 7 is 2, and the answer is 'residue'. This mirrors how congruences are used in practice: only the remainder class matters, never the representative you happened to write down.

FAQs

What is a quadratic residue?

An integer a is a quadratic residue mod m if some x squared is congruent to a mod m. Modulo 7 the residues are {1, 2, 4}: for example 3 squared is 9, and 9 mod 7 is 2, so 2 is a residue.

Is 2 a quadratic residue mod 7?

Yes. The squares of 1 to 6 mod 7 are 1, 4, 2, 2, 4, 1, so the residue set is {1, 2, 4} and 2 is in it.

Is 3 a quadratic residue mod 7?

No. The complete residue set mod 7 is {1, 2, 4}, and 3 never appears as a square mod 7, so 3 is a quadratic non-residue mod 7.

How many quadratic residues are there mod 11?

There are 5: the set {1, 3, 4, 5, 9}. This matches the general rule that an odd prime p has exactly (p - 1) / 2 residues among 1 to p - 1.

Does this calculator work for composite moduli?

Yes. It enumerates the squares directly, so it works for any modulus from 2 to 1000, prime or composite. The Legendre symbol, by contrast, is only defined for prime moduli.

Why is 0 not in the residue set?

The calculator squares the numbers 1 to m - 1. For a prime modulus none of those squares is divisible by m, so 0 never appears, and the reported count excludes 0.

What happens if a is negative or larger than m?

The value a is first reduced mod m, so -1 mod 7 is treated as 6 and 16 mod 7 as 2. The output shows a mod m so you can see the reduction that was tested.

What is quadratic reciprocity?

It is Gauss's law relating whether p is a residue mod q to whether q is a residue mod p, with correction factors for sign and the prime 2. It lets the Legendre symbol be evaluated for huge primes without squaring, but for the moduli on this page direct enumeration gives the same answers and shows the whole set.

Sources

Related calculators

Shoelace Formula Calculator
Area of any pentagon from its five corner coordinates, with perimeter and centroid - the surveyor's formula for irregular plots.
Superabundant Number Calculator
Test whether the abundancy index sigma(n) / n sets a new record - the superabundant numbers of Alaoglu and Erdos - or list them.
Unusual Number Calculator
Test whether a number is unusual - its largest prime factor exceeds its square root - or list the unusual numbers up to a limit.