Jacobi symbol

From Wikipedia
Jump to navigation Jump to search

The Jacobi symbol is a generalization of the Legendre symbol. Introduced by Jacobi in 1837,[1] 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.

File:Carl Jacobi.jpg
Carl Gustav Jacob Jacobi who introduced the symbol.
k
n
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 1
3 0 1 −1
5 0 1 −1 −1 1
7 0 1 1 −1 1 −1 −1
9 0 1 1 0 1 1 0 1 1
11 0 1 −1 1 1 1 −1 −1 −1 1 −1
13 0 1 −1 1 1 −1 −1 −1 −1 1 1 −1 1
15 0 1 1 0 1 0 0 −1 1 0 0 −1 0 −1 −1
17 0 1 1 −1 1 −1 −1 −1 1 1 −1 −1 −1 1 −1 1 1

Jacobi symbol (k/n) for various k (along top) and n (along left side). Only 0 ≤ k < n are shown, since due to rule (2) 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 (k/n) = 1, but not all entries with a Jacobi symbol of 1 (see the n = 9 and n = 15 rows) are quadratic residues. Notice also that when either n or k is a square, all values are nonnegative.

Definition

[edit]

For any integer a and any positive odd integer n, the Jacobi symbol (a/n) is defined as the product of the Legendre symbols corresponding to the prime factors of n:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \left(\frac{a}{n}\right) := \left(\frac{a}{p_1}\right)^{\alpha_1}\left(\frac{a}{p_2}\right)^{\alpha_2}\cdots \left(\frac{a}{p_k}\right)^{\alpha_k},}

where

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle n=p_1^{\alpha_1}p_2^{\alpha_2}\cdots p_k^{\alpha_k}}

is the prime factorization of n.

The Legendre symbol (a/p) is defined for all integers a and all odd primes p by

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \left(\frac{a}{p}\right) := \left\{ \begin{array}{rl} 0 & \text{if } a \equiv 0 \pmod{p},\\ 1 & \text{if } a \not\equiv 0\pmod{p} \text{ and for some integer } x\colon\;a\equiv x^2\pmod{p},\\ -1 & \text{if } a \not\equiv 0\pmod{p} \text{ and there is no such } x. \end{array} \right.}

Following the normal convention for the empty product, (a/1) = 1.

When the lower argument is an odd prime, the Jacobi symbol is equal to the Legendre symbol.

Table of values

[edit]

The following is a table of values of Jacobi symbol (a/n) with n ≤ 59, a ≤ 30, n odd.

a
n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0
5 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0
7 1 1 −1 1 −1 −1 0 1 1 −1 1 −1 −1 0 1 1 −1 1 −1 −1 0 1 1 −1 1 −1 −1 0 1 1
9 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
11 1 −1 1 1 1 −1 −1 −1 1 −1 0 1 −1 1 1 1 −1 −1 −1 1 −1 0 1 −1 1 1 1 −1 −1 −1
13 1 −1 1 1 −1 −1 −1 −1 1 1 −1 1 0 1 −1 1 1 −1 −1 −1 −1 1 1 −1 1 0 1 −1 1 1
15 1 1 0 1 0 0 −1 1 0 0 −1 0 −1 −1 0 1 1 0 1 0 0 −1 1 0 0 −1 0 −1 −1 0
17 1 1 −1 1 −1 −1 −1 1 1 −1 −1 −1 1 −1 1 1 0 1 1 −1 1 −1 −1 −1 1 1 −1 −1 −1 1
19 1 −1 −1 1 1 1 1 −1 1 −1 1 −1 −1 −1 −1 1 1 −1 0 1 −1 −1 1 1 1 1 −1 1 −1 1
21 1 −1 0 1 1 0 0 −1 0 −1 −1 0 −1 0 0 1 1 0 −1 1 0 1 −1 0 1 1 0 0 −1 0
23 1 1 1 1 −1 1 −1 1 1 −1 −1 1 1 −1 −1 1 −1 1 −1 −1 −1 −1 0 1 1 1 1 −1 1 −1
25 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
27 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0
29 1 −1 −1 1 1 1 1 −1 1 −1 −1 −1 1 −1 −1 1 −1 −1 −1 1 −1 1 1 1 1 −1 −1 1 0 1
31 1 1 −1 1 1 −1 1 1 1 1 −1 −1 −1 1 −1 1 −1 1 1 1 −1 −1 −1 −1 1 −1 −1 1 −1 −1
33 1 1 0 1 −1 0 −1 1 0 −1 0 0 −1 −1 0 1 1 0 −1 −1 0 0 −1 0 1 −1 0 −1 1 0
35 1 −1 1 1 0 −1 0 −1 1 0 1 1 1 0 0 1 1 −1 −1 0 0 −1 −1 −1 0 −1 1 0 1 0
37 1 −1 1 1 −1 −1 1 −1 1 1 1 1 −1 −1 −1 1 −1 −1 −1 −1 1 −1 −1 −1 1 1 1 1 −1 1
39 1 1 0 1 1 0 −1 1 0 1 1 0 0 −1 0 1 −1 0 −1 1 0 1 −1 0 1 0 0 −1 −1 0
41 1 1 −1 1 1 −1 −1 1 1 1 −1 −1 −1 −1 −1 1 −1 1 −1 1 1 −1 1 −1 1 −1 −1 −1 −1 −1
43 1 −1 −1 1 −1 1 −1 −1 1 1 1 −1 1 1 1 1 1 −1 −1 −1 1 −1 1 1 1 −1 −1 −1 −1 −1
45 1 −1 0 1 0 0 −1 −1 0 0 1 0 −1 1 0 1 −1 0 1 0 0 −1 −1 0 0 1 0 −1 1 0
47 1 1 1 1 −1 1 1 1 1 −1 −1 1 −1 1 −1 1 1 1 −1 −1 1 −1 −1 1 1 −1 1 1 −1 −1
49 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1
51 1 −1 0 1 1 0 −1 −1 0 −1 1 0 1 1 0 1 0 0 1 1 0 −1 1 0 1 −1 0 −1 1 0
53 1 −1 −1 1 −1 1 1 −1 1 1 1 −1 1 −1 1 1 1 −1 −1 −1 −1 −1 −1 1 1 −1 −1 1 1 −1
55 1 1 −1 1 0 −1 1 1 1 0 0 −1 1 1 0 1 1 1 −1 0 −1 0 −1 −1 0 1 −1 1 −1 0
57 1 1 0 1 −1 0 1 1 0 −1 −1 0 −1 1 0 1 −1 0 0 −1 0 −1 −1 0 1 −1 0 1 1 0
59 1 −1 1 1 1 −1 1 −1 1 −1 −1 1 −1 −1 1 1 1 −1 1 1 1 1 −1 −1 1 1 1 1 1 −1

Properties

[edit]

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.[2]

The Jacobi symbol is defined only when the upper argument ("numerator") is an integer and the lower argument ("denominator") is a positive odd integer.

1. If n is (an odd) prime, then the Jacobi symbol (a/n) is equal to (and written the same as) the corresponding Legendre symbol.
2. If ab  (mod n), then Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \biggl(\frac{a}{n}\biggr) = \left(\frac{b}{n}\right) = \left(\frac{a \pm k \cdot n}{n}\right).}
3. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \biggl(\frac{a}{n}\biggr) = \begin{cases} \, 0 & \text{if } \gcd(a,n) \ne 1,\\ \pm1 & \text{if } \gcd(a,n) = 1. \end{cases} }

If either the top or bottom argument is fixed, the Jacobi symbol is a completely multiplicative function in the remaining argument:

4. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \;\left(\frac{ab}{n}\right)\; = \biggl(\frac{a}{n}\biggr)\left(\frac{b}{n}\right),\quad\text{so } \left(\frac{a^2}{n}\right) = \biggl(\frac{a}{n}\biggr)^2 = +1 \text{ or } 0. }
5. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \biggl(\frac{a}{mn}\biggr)=\biggl(\frac{a}{m}\biggr)\biggl(\frac{a}{n}\biggr),\quad\text{so } \left(\frac{a}{n^2}\right) = \biggl(\frac{a}{n}\biggr)^2 = +1 \text{ or } 0. }

The law of quadratic reciprocity: if m and n are odd positive coprime integers, then

6. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \biggl(\frac{m}{n}\biggr)\biggl(\frac{n}{m}\biggr) = (-1)^{\tfrac{m-1}{2}\cdot\tfrac{n-1}{2}} = \begin{cases} +1 & \text{if } n \equiv 1 \text{ or } m \equiv 1 \pmod 4,\\ -1 & \text{if } n \equiv m \equiv 3 \pmod 4. \end{cases} }

and its supplements

7. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \;\left(\frac{1}{n}\right)\; = \biggl(\frac{n}{1}\biggr) = +1. }
8. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \left(\frac{-1}{n}\right) = (-1)^\tfrac{n-1}{2} \,= \begin{cases} +1 & \text{if }n \equiv 1 \pmod 4,\\ -1 & \text{if }n \equiv 3 \pmod 4. \end{cases} }
9.  

Combining properties 4 and 9 gives:

10. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \left(\frac{2a}{n}\right) = \left(\frac{2}{n}\right) \biggl(\frac{a}{n}\biggr) = \begin{cases} \phantom{-}\left(\frac{a}{n}\right) & \text{if }n \equiv 1,7 \pmod 8,\\ - \left(\frac{a}{n}\right) & \text{if }n \equiv 3, 5\pmod 8. \end{cases} }

Combining properties 2, 6 and 10 gives:

11. If nm  (mod 4a), then Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \biggl(\frac{a}{n}\biggr) = \biggl(\frac{a}{m}\biggr) = \biggl(\frac{a}{n + 4k\left|a\right|}\biggr). } [3]
12. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \biggl(\frac{a}{n+2\left|a\right|}\biggr) = \biggl(\frac{a}{n}\biggr)(-1)^\tfrac{a(a-1)}{2} = \begin{cases} \phantom{-}\left(\frac{a}{n}\right) & \text{if }a \equiv 0,1 \pmod 4,\\ - \left(\frac{a}{n}\right) & \text{if }a \equiv 2,3 \pmod 4. \end{cases} }

Like the Legendre symbol:

  • If (a/n) = −1 then a is a quadratic nonresidue modulo n.
  • If a is a quadratic residue modulo n and gcd(a,n) = 1, then (a/n) = +1.

But, unlike the Legendre symbol:

  • If (a/n) = +1 then a may or may not be a quadratic residue modulo n.
  • If a is a quadratic nonresidue modulo n, then (a/n) may be −1 or +1.

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 +1 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 (a/n) is a Dirichlet character to the modulus n.

Calculating the Jacobi symbol

[edit]

The above formulas lead to an efficient O(log a log b)[4] algorithm for calculating the Jacobi symbol, analogous to the Euclidean algorithm for finding the gcd of two numbers. (This should not be surprising in light of rule 2.)

  1. Reduce the "numerator" modulo the "denominator" using rule 2.
  2. Extract any even "numerator" using rule 10.
  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.

In addition to the codes below, Riesel[5] has it in Pascal.

Implementation in Lua

[edit]
function jacobi(n, k)
  assert(k > 0 and k % 2 == 1)
  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

Implementation in C/C++

[edit]
// a/n is represented as (a,n)
int jacobi(int a, int n) {
    assert(n > 0 && n%2 == 1);
    // Step 1
    if ((a %= n) < 0) a += n; // Handle (a < 0)
    // Step 3
    int t = 0;   // XOR of bits 1 and 2 determines sign of return value
    while (a) {
        // Step 2
        while (a % 4 == 0) a /= 4;
        if (a % 2 == 0) {
            t ^= n;  // Could be "^= n & 6"; we only care about bits 1 and 2
            a /= 2;
        }
        // Step 4
        t ^= a & n & 2;  // Flip sign if a % 4 == n % 4 == 3
        int r = n % a;
        n = a;
        a = r;
    }
    if (n != 1)
        return 0;
    else if ((t + 2) & 4)
        return -1;
    else
        return 1;
}

Primality testing

[edit]

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,

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align} \left(\frac{19}{45}\right) &= 1 &&\text{ and } & 19^\frac{45-1}{2} &\equiv 1\pmod{45}. \\ \left(\frac{ 8}{21}\right) &= -1 &&\text{ but } & 8^\frac{21-1}{2} &\equiv 1\pmod{21}. \\ \left(\frac{ 5}{21}\right) &= 1 &&\text{ but } & 5^\frac{21-1}{2} &\equiv 16\pmod{21}. \end{align}}

So if it is unknown whether a number n is prime or composite, we can pick a random number a, calculate the Jacobi symbol (a/n) 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 Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align}2^{136,279,841} - 1\end{align}} (the largest known Mersenne prime as of October 2024). In nominal cases, the Jacobi symbol:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align}\left(\frac{s_i - 2}{M_p}\right) &= -1 & i \ne 0\end{align}}

This also holds for the final residue Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align}s_{p-2}\end{align}} 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 Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align}s\end{align}} (unless another error occurs and changes it back to −1).

See also

[edit]

Notes

[edit]
  1. Jacobi, C. G. J. (1837). "Über die Kreisteilung und ihre Anwendung auf die Zahlentheorie". Bericht Ak. Wiss. Berlin: 127–136.
  2. Ireland & Rosen pp. 56–57 or Lemmermeyer p. 10
  3. Cohen, Henri (1996) [1993]. A Course in Computational Algebraic Number Theory. Chapman & Hall/CRC. p. 28 Theorem 1.4.9.(4). ISBN 0-387-55640-0.
  4. Cohen, pp. 29–31
  5. p. 285

References

[edit]
[edit]