Jacobi symbol




012345678910111213141516
11
301−1
501−1−11
7011−11−1−1
9011011011
1101−1111−1−1−11−1
1301−111−1−1−1−111−11
150110100−1100−10−1−1
17011−11−1−1−111−1−1−11−111


Jacobi symbol for various k and n. Only are shown, since due to rule below any other k can be reduced modulo n. Quadratic residues are highlighted in yellow — note that no entry with a Jacobi symbol of −1 is a quadratic residue, and if k is a quadratic residue modulo a coprime n, then, but not all entries with a Jacobi symbol of 1 are quadratic residues. Notice also that when either n or k is a square, all values are nonnegative.



The Jacobi symbol is a generalization of the Legendre symbol. Introduced by Jacobi in 1837, it is of theoretical interest in modular arithmetic and other branches of number theory, but its main use is in computational number theory, especially primality testing and integer factorization; these in turn are important in cryptography.

Definition

For any integer a and any positive odd integer n, the Jacobi symbol is defined as the product of the Legendre symbols corresponding to the prime factors of n:
where
is the prime factorization of n.
The Legendre symbol is defined for all integers a and all odd primes p by
Following the normal convention for the empty product, = 1.
When the lower argument is an odd prime, the Jacobi symbol is equal to the Legendre symbol.

Table of values

The following is a table of values of Jacobi symbol with n ≤ 59, k ≤ 30, n odd.
123456789101112131415161718192021222324252627282930
1111111111111111111111111111111
31−101−101−101−101−101−101−101−101−101−10
51−1−1101−1−1101−1−1101−1−1101−1−1101−1−110
711−11−1−1011−11−1−1011−11−1−1011−11−1−1011
9110110110110110110110110110110
111−1111−1−1−11−101−1111−1−1−11−101−1111−1−1−1
131−111−1−1−1−111−1101−111−1−1−1−111−1101−111
15110100−1100−10−1−10110100−1100−10−1−10
1711−11−1−1−111−1−1−11−111011−11−1−1−111−1−1−11
191−1−11111−11−11−1−1−1−111−101−1−11111−11−11
211−101100−10−1−10−100110−1101−101100−10
231111−11−111−1−111−1−11−11−1−1−1−101111−11−1
25111101111011110111101111011110
271−101−101−101−101−101−101−101−101−101−10
291−1−11111−11−1−1−11−1−11−1−1−11−11111−1−1101
3111−111−11111−1−1−11−11−1111−1−1−1−11−1−11−1−1
331101−10−110−100−1−10110−1−100−101−10−110
351−1110−10−1101110011−1−100−1−1−10−11010
371−111−1−11−11111−1−1−11−1−1−1−11−1−1−11111−11
39110110−1101100−101−10−1101−10100−1−10
4111−111−1−1111−1−1−1−1−11−11−111−11−11−1−1−1−1−1
431−1−11−11−1−1111−111111−1−1−11−1111−1−1−1−1−1
451−10100−1−10010−1101−10100−1−10010−110
471111−11111−1−11−11−1111−1−11−1−111−111−1−1
49111111011111101111110111111011
511−10110−1−10−110110100110−1101−10−110
531−1−11−111−1111−11−1111−1−1−1−1−1−111−1−111−1
5511−110−111100−1110111−10−10−1−101−11−10
571101−10110−1−10−1101−100−10−1−101−10110
591−1111−11−11−1−11−1−1111−11111−1−111111−1

Properties

The following facts, even the reciprocity laws, are straightforward deductions from the definition of the Jacobi symbol and the corresponding properties of the Legendre symbol.
The Jacobi symbol is defined only when the upper argument is an integer and the lower argument is a positive odd integer.
If either the top or bottom argument is fixed, the Jacobi symbol is a completely multiplicative function in the remaining argument:
The law of quadratic reciprocity: if m and n are odd positive coprime integers, then
and its supplements
Combining property 4 and 8 gives:
Like the Legendre symbol:
But, unlike the Legendre symbol:
This is because for a to be a quadratic residue modulo n, it has to be a quadratic residue modulo every prime factor of n. However, the Jacobi symbol equals one if, for example, a is a non-residue modulo exactly two of the prime factors of n.
Although the Jacobi symbol cannot be uniformly interpreted in terms of squares and non-squares, it can be uniformly interpreted as the sign of a permutation by Zolotarev's lemma.
The Jacobi symbol is a Dirichlet character to the modulus n.

Calculating the Jacobi symbol

The above formulas lead to an efficient algorithm for calculating the Jacobi symbol, analogous to the Euclidean algorithm for finding the gcd of two numbers.
  1. Reduce the "numerator" modulo the "denominator" using rule 2.
  2. Extract any even "numerator" using rule 9.
  3. If the "numerator" is 1, rules 3 and 4 give a result of 1. If the "numerator" and "denominator" are not coprime, rule 3 gives a result of 0.
  4. Otherwise, the "numerator" and "denominator" are now odd positive coprime integers, so we can flip the symbol using rule 6, then return to step 1.

    Implementation in Lua">Lua (programming language)">Lua


function jacobi
assert
n = n % k
t = 1
while n ~= 0 do
while n % 2 0 do
n = n / 2
r = k % 8
if r 3 or r 5 then
t = -t
end
end
n, k = k, n
if n % 4 3 and k % 4 3 then
t = -t
end
n = n % k
end
if k 1 then
return t
else
return 0
end
end

Example of calculations

The Legendre symbol is only defined for odd primes p. It obeys the same rules as the Jacobi symbol
Problem: Given that 9907 is prime, calculate.

Using the Legendre symbol

Using the Jacobi symbol

The difference between the two calculations is that when the Legendre symbol is used the "numerator" has to be factored into prime powers before the symbol is flipped. This makes the calculation using the Legendre symbol significantly slower than the one using the Jacobi symbol, as there is no known polynomial-time algorithm for factoring integers. In fact, this is why Jacobi introduced the symbol.

Primality testing

There is another way the Jacobi and Legendre symbols differ. If the Euler's criterion formula is used modulo a composite number, the result may or may not be the value of the Jacobi symbol, and in fact may not even be −1 or 1. For example,
So if it is unknown whether a number n is prime or composite, we can pick a random number a, calculate the Jacobi symbol and compare it with Euler's formula; if they differ modulo n, then n is composite; if they have the same residue modulo n for many different values of a, then n is "probably prime".
This is the basis for the probabilistic Solovay–Strassen primality test and refinements such as the Baillie-PSW primality test and the Miller–Rabin primality test.
As an indirect use, it is possible to use it as an error detection routine during the execution of the Lucas-Lehmer primality test which, even on modern computer hardware, can take weeks to complete when processing Mersenne numbers over . In nominal cases, the Jacobi symbol:
This also holds for the final residue and hence can be used as a verification of probable validity. However, if an error occurs in the hardware, there is a 50% chance that the result will become 0 or 1 instead, and won't change with subsequent terms of .