The guide
A plus one that proves nothing: the Jacobi symbol and the squares it cannot see
The Legendre symbol says whether a number is a square modulo a prime. The Jacobi symbol stretches it to any odd modulus, and then a plus one stops being a proof.
- 1837 the year Jacobi introduced his symbol
- (p - 1) / 2 nonzero squares modulo an odd prime p
- n mod 8 decides the sign for each factor of 2
- 240 of 320 bases coprime to 561 that witness it is composite
- 480 of 560 bases that witness 561 is composite
Read the full guide Fold the guide
Ask whether 5 is a square and the answer is no, because no whole number multiplied by itself gives 5. Ask the same question after throwing away everything but the remainder on division by 21 and the question changes. Now you are asking whether some whole number x leaves the remainder 5 when x times x is divided by 21. That is the question of quadratic residues, and it is old, useful and full of traps.
Squares on a clock, and Euler’s test
A number a is a quadratic residue modulo n when some x makes x squared leave the remainder a. A number that is not one is a nonresidue. Zero counts as a square, since zero times zero is zero. Take n = 23 and square every x from 0 to 22. Only twelve different remainders appear: 0, 1, 2, 3, 4, 6, 8, 9, 12, 13, 16 and 18.
Eleven of those are nonzero, and eleven is (23 - 1) / 2. That is no accident. For an odd prime p, there are (p + 1) / 2 residues counting zero and (p - 1) / 2 nonresidues, so exactly half of the nonzero remainders are squares. The pairing is easy to see: x and p minus x have the same square.
Composite moduli behave differently. The count of squares modulo n is the sequence 1, 2, 2, 2, 3, 4, 4, 3, 4, 6 and so on for n from 1 upward, and it is smaller than n for every n above 2. Modulo 15 only 0, 1, 4, 6, 9 and 10 are squares. Six out of fifteen is not a half, and nothing about 15 is prime. Brute force settles any single case, but it costs one squaring per remainder, which is hopeless for a modulus of forty digits.
For an odd prime p the Legendre symbol of a over p is 0 when p divides a, +1 when a is a nonzero square modulo p and -1 when it is not. Adrien-Marie Legendre introduced it in 1797 or 1798 while trying to prove the reciprocity law discussed below. Gauss, working earlier in his own notation, wrote the two verdicts as aRp and aNp.
The symbol would be only bookkeeping if there were no way to compute it. Euler supplied one in a paper of 1748. Raise a to the power (p - 1) / 2 and reduce modulo p: the result is 1 when a is a nonzero square and p minus 1, which is the same as -1, when it is not. Legendre’s original definition was exactly that power, and the two definitions are equivalent.
Check it on 2 and 23. Two raised to the 11th power is 2048, and 2048 leaves the remainder 1 when divided by 23. So 2 is a square modulo 23, and indeed 5 squared is 25, which leaves 2. Euler’s route needs only repeated squaring, which stays fast for numbers of any realistic length. The symbol is also multiplicative: two nonsquares multiply to a square, and a square times a nonsquare is a nonsquare. That fact drives everything that follows.
Past the primes
Jacobi introduced a generalisation in 1837. For any integer a and any odd positive n, write n as a product of primes, take the Legendre symbol of a over each prime and multiply the results, counting repeated primes with repeats. When n is prime nothing changes. When n is 1 the product is empty and the symbol is 1. The symbol is 0 exactly when a and n share a factor larger than 1, and otherwise it is +1 or -1.
The Jacobi symbol keeps the algebra of the Legendre symbol. It is unchanged when a is replaced by its remainder modulo n, it is multiplicative in the top number and in the bottom number, and the standard formulas for -1 and for 2 survive with the prime replaced by any odd number. NIST’s mathematical function library states the same result: both of those special values hold for an odd modulus, and reciprocity holds for any two odd numbers with no common factor.
What it does not keep is the meaning. A symbol of -1 still proves that a is a nonresidue, whatever the odd n. A symbol of +1 no longer proves the opposite. For a to be a square modulo a composite n it has to be a square modulo every prime factor of n, and the symbol can reach +1 when a fails at exactly two of them, because two minus signs cancel.
The smallest cases are worth keeping in mind. The symbol of 2 over 15 is +1, since 2 is a nonresidue modulo both 3 and 5, yet 2 is not among the squares 0, 1, 4, 6, 9 and 10. The symbol of 5 over 21 is +1 for the same reason, and the squares modulo 21 are only 0, 1, 4, 7, 9, 15, 16 and 18.
A short loop instead of a factorisation
The definition asks for the prime factors of n, and factoring a large number is the very thing we cannot do quickly. The reciprocity law removes the need. For odd positive numbers m and n with no common factor, the symbols of m over n and of n over m are equal unless both numbers leave remainder 3 on division by 4, in which case they are opposite. Gauss proved this, calling it privately the golden theorem, and his diary dates his first proof to April 1796. Legendre had tried earlier, and the MacTutor biography says his proof was unsatisfactory.
Add the rule for 2. The symbol of 2 over n is +1 when n leaves remainder 1 or 7 on division by 8 and -1 when n leaves 3 or 5. The rule reads n mod 8, not n mod 4, and that is the commonest slip. Take n = 5. It leaves 1 modulo 4, so a careless rule would say +1, but 2 is not a square modulo 5, whose squares are 0, 1 and 4. The true symbol is -1.
Now the loop is complete. Reduce the top modulo the bottom. Pull out factors of 2, flipping the sign once for each factor when the bottom is 3 or 5 modulo 8. If the top has become 1, stop with the running sign. Otherwise swap the two numbers, flip the sign if both are 3 modulo 4, and go round again. If the top reaches 0 the symbol is 0. This is the Euclidean algorithm with a sign bookkeeper, and the cost grows with the square of the number of digits.
Here is a worked case. For 1001 over the prime 9907, the first swap changes nothing because 1001 is 1 modulo 4. Then 9907 reduces to 898 modulo 1001, which is 2 times 449, and 1001 is 1 modulo 8, so no flip. Three more reduce-and-swap rows bring the pair to 37 over 29. There 37 reduces to 8, which is 2 cubed, and 29 is 5 modulo 8, so the odd number of twos flips the sign. The result is -1 after six rows.
Euler’s criterion agrees: 1001 raised to the power 4953 modulo 9907 leaves 9906, which is -1. So 1001 is not a square modulo 9907, and the loop never touched a factorisation of 9907.
What the symbol buys: primality tests
If Euler’s criterion is applied modulo a composite number, it may or may not match the Jacobi symbol, and the result need not even be 1 or -1. Wikipedia’s article on the symbol lists three small examples. For 19 over 45 both sides are 1. For 8 over 21 the symbol is -1 but the power is 1. For 5 over 21 the symbol is 1 but the power is 16.
That disagreement is a test. Choose a base a, compute the symbol by the fast loop, compute a to the power (n - 1) / 2 by repeated squaring, and compare. If they differ, n is certainly composite, and a is called an Euler witness. If they agree for many bases, n is probably prime. Solovay and Strassen developed the test in 1977, and the source notes that the idea had been found by Artjuhov in 1967.
The guarantee is what made it important. For an odd composite n, at least half of the bases coprime to n are witnesses, because the liars form a proper subgroup of the invertible remainders. After k independent random bases the chance of a wrong answer is at most 2 to the power minus k. For n = 65 the liars are 1, 8, 14, 18, 47, 51, 57 and 64, eight of the 48 coprime bases.
Compare the Fermat test, which asks only whether a raised to n minus 1 leaves 1. Carmichael numbers, the first being 561, 1105, 1729 and 2465, fool it for every coprime base. Count the Euler witnesses of 561, which is 3 times 11 times 17, and you find 480 of 560 bases. That includes 240 of 320 coprime bases, three quarters and comfortably above the half the theorem promises. The other 240 bases share a factor with 561 and count at once. The counts for 1105, 1729 and 2465 are 912, 1080 and 1568.
The Solovay-Strassen test has since been largely replaced by Miller-Rabin and by the Baillie-PSW test, but it remains important in the history of the field because it showed that the RSA cryptosystem was practical: large primes could be found quickly with a test that rarely errs. Miller-Rabin has its own useful property: a fixed list of the first twelve primes as bases gives an exact answer for every n below 2 to the power 64, which is about 1.8 times 10 to the power 19.
What a symbol cannot tell you
A tool that computes the Jacobi symbol is honest about three limits. First, a plus one for a composite modulus says nothing certain about squares. The grid of squares modulo n makes that visible for small n, which is why the check against brute force matters more than any formula. Second, it cannot factor a large number. Deciding whether a is a square modulo a composite whose factors are unknown is called the quadratic residuosity problem, and it is believed to be computationally hard. The Jacobi symbol settles only the half of the question that needs no factorisation: a symbol of -1 proves that a is a nonresidue.
Third, a probable prime is not a proof: for large numbers a Miller-Rabin pass is a label, exact below 2 to the power 64 with the twelve-base set and merely probable above. Forty-digit numbers also need care in software. JavaScript’s BigInt type represents integers too large for the ordinary number type, but it cannot be mixed with ordinary numbers in an operation and cannot be passed to the Math functions, so the code converts deliberately. Counting every witness of a modulus near 100,000 is heavy enough to run in a web worker, a background thread that receives copies of its inputs rather than shared memory, so the page stays responsive.
The practical test of any implementation is the one the examples above provide. Skip the sign flip for two numbers that are both 3 modulo 4 and 3 over 7 comes out +1 instead of -1. Key the rule for 2 to modulo 4 and 2 over 5 comes out +1 instead of -1. Both slips are easy to catch by counting squares.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Jacobi symbol Wikipedia
- Legendre symbol Wikipedia
- Euler's criterion Wikipedia
- Quadratic residue Wikipedia
- Quadratic reciprocity Wikipedia
- Solovay-Strassen primality test Wikipedia
- Miller-Rabin primality test Wikipedia
- Carmichael number Wikipedia
- DLMF: 27.9 Quadratic Characters NIST Digital Library of Mathematical Functions
- A000224 - OEIS The On-Line Encyclopedia of Integer Sequences
- Adrien-Marie Legendre MacTutor History of Mathematics, University of St Andrews
- BigInt MDN Web Docs
- Web Workers API MDN Web Docs