User:James in dc/sandbox/Jacobi symbol

sandbox

Jacobi symbol
In algebraic number theory and related branches of mathematics the Jacobi symbol $$\left(\tfrac{a}{m}\right)$$ is defined for all integers $$a$$ and all positive odd integers $$m.$$. For $$m= p_1p_2\dots$$ where the $$p_i$$ are odd primes the Jacobi symbol is the product of Legendre symbols
 * $$ \left(\frac{a}{m}\right)=\left(\frac{a}{p_1}\right)\left(\frac{a}{p_2}\right)\dots$$. (and for completeness $$\left(\frac{a}{1}\right)$$ is defined to be $$1.$$)

Properties of the Jacobi symbol
The Legendre symbol $$\left(\tfrac{a}{p}\right) $$ is defined for all integers $$a$$ and all odd primes $$p$$ by
 * $$\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.$$

The properties of the Jacobi symbol are derived from those of the Legendre symbol.

Since $$ \left(\tfrac{a_1a_2}{p}\right) = \left(\tfrac{a_1}{p}\right) \left(\tfrac{a_2}{p}\right) $$ is true for each factor, $$ \left(\tfrac{a_1a_2}{m}\right) = \left(\tfrac{a_1}{m}\right) \left(\tfrac{a_2}{m}\right). $$

Obviously from the definition $$ \left(\tfrac{a}{m_1m_2}\right) = \left(\tfrac{a}{m_1}\right) \left(\tfrac{a}{m_2}\right). $$

These imply that $$\left(\tfrac{ab^2}{m}\right) =\left(\tfrac{a}{m}\right)$$ and $$\left(\tfrac{a}{mn^2}\right) =\left(\tfrac{a}{m}\right). $$

If $$a_1\equiv a_2\pmod{m}$$ then $$a_1\equiv a_2\pmod{p}$$ for every $$p$$ in the product so $$ \left(\tfrac{a_1}{p}\right) = \left(\tfrac{a_2}{p}\right) $$ and therefore $$ \left(\tfrac{a_1}{m}\right) = \left(\tfrac{a_2}{m}\right). $$

If $$\gcd(a,m)>1$$ then one of the $$p$$s divides $$a, \;$$ $$ \left(\tfrac{a}{p}\right) = 0 $$ and $$ \left(\tfrac{a}{m}\right) = 0. $$

If $$\gcd(a,m)=1$$ then every $$ \left(\tfrac{a}{p}\right) = \pm 1 $$ and $$ \left(\tfrac{a}{m}\right) = \pm1 $$ as well.

Differences between Legendre and Jacobi symbols
$$\left(\tfrac{a}{m}\right)=1$$ for composite $$m$$ only means that there were an even number of minus signs in the product; it does not mean that $$a$$ is a quadratic residue $$\bmod m$$. For prime $$m$$ it does.

Euler's criterion is not true for composite $$m$$. For example $$\left(\tfrac{2}{15}\right)=1$$ but $$2^7\equiv8\pmod{15}.$$ See uses, below.

First Supplement
Let $$m=p_1p_2\cdots q_1q_2\dots$$ where $$p_i\equiv 1\pmod{4}$$ and $$q_i\equiv 3\pmod{4}$$. The values of the Legendre symbols are known: $$ \left(\tfrac{-1}{p}\right) = 1 $$ and $$ \left(\tfrac{-1}{q}\right) = -1. $$

$$m\equiv 3 \pmod{4} $$ if and only if the number of $$q_i$$ is odd.

Likewise $$ \left(\tfrac{-1}{m}\right) = \left(\tfrac{-1}{p_1}\right) \left(\tfrac{-1}{p_2}\right) \cdots \left(\tfrac{-1}{q_1}\right) \left(\tfrac{-1}{q_2}\right) \dots$$ is $$-1$$ if and only if the number of $$q_i$$ is odd. This shows that
 * $$\left(\frac{-1}{m}\right) =

\begin{cases} 1 & \text{if }m \equiv 1 \pmod 4\\ -1 & \text{if }m \equiv 3 \pmod 4 \end{cases} $$ which is equivalent to
 * $$\left(\frac{-1}{m}\right) = (-1)^\tfrac{m-1}{2}.$$

Second Supplement
Let $$m=p_1p_2\cdots q_1q_2\dots$$ where $$p_i\equiv \pm1\pmod{8}$$ and $$q_i\equiv \pm3\pmod{8}$$. The values of the Legendre symbols are known: $$ \left(\tfrac{2}{p}\right) = 1 $$ and $$ \left(\tfrac{2}{q}\right) = -1. $$

$$m\equiv \pm3 \pmod{8} $$ if and only if the number of $$q_i$$ is odd.

Likewise $$ \left(\tfrac{2}{m}\right) = \left(\tfrac{2}{p_1}\right) \left(\tfrac{2}{p_2}\right) \cdots \left(\tfrac{2}{q_1}\right) \left(\tfrac{2}{q_2}\right) \dots $$ is $$-1$$ if and only if the number of $$q_i$$ is odd. This shows that
 * $$\left(\frac{2}{m}\right) =

\begin{cases} 1 & \text{if }m \equiv 1,7\;\;\equiv 0\pm1 \pmod 8\\ -1 & \text{if }m \equiv 3,5\;\;\equiv 4\pm1 \pmod 8 \end{cases} $$ which is equivalent to
 * $$\left(\frac{2}{m}\right) = (-1)^\tfrac{m^2-1}{8}.$$

Combining with the first supplement gives
 * $$\left(\frac{-2}{m}\right) =

\begin{cases} 1 & \text{if }m \equiv 1,3\;\;\equiv 2\pm1 \pmod 8\\ -1 & \text{if }m \equiv 5,7\;\;\equiv 6\pm1 \pmod 8 \end{cases} $$ which is equivalent to
 * $$\left(\frac{-2}{m}\right) = (-1)^\tfrac{(m+2)^2-1}{8}.$$

Legendre
$$a $$ must be positive and odd.

Let $$m=p_1p_2\cdots q_1q_2\cdots$$ where $$p_i\equiv 1\pmod{4}$$ and $$q_i\equiv 3\pmod{4}$$.

In the same way let $$a=b_1b_2\cdots c_1c_2\cdots$$ where the $$b_i$$ and $$c_i$$ are prime and $$b_i\equiv 1\pmod{4}$$ and $$c_i\equiv 3\pmod{4}$$.

The values of the inverted Legendre symbols are known: $$ \left(\tfrac{b}{p}\right) = \left(\tfrac{p}{b}\right),\; \left(\tfrac{c}{p}\right) = \left(\tfrac{p}{c}\right),\; \left(\tfrac{b}{q}\right) = \left(\tfrac{q}{b}\right),\; \left(\tfrac{c}{q}\right) =-\left(\tfrac{q}{c}\right) $$

$$m\equiv 3 \pmod{4} $$ if and only if the number of $$q_i$$ is odd

$$a\equiv 3 \pmod{4} $$ if and only if the number of $$c_i$$ is odd.

$$\begin{align} \left(\frac{a}{m}\right) &=

\left(\frac{a}{p_1}\right) \left(\frac{a}{p_2}\right) \cdots \left(\frac{a}{q_1}\right) \left(\frac{a}{q_2}\right) \dots \\ &=

\left(\frac{b_1\cdots c_1\cdots}{p_1}\right) \left(\frac{b_1\cdots c_1\cdots}{p_2}\right) \cdots \left(\frac{b_1\cdots c_1\cdots}{q_1}\right) \dots \\ &=

\left(\frac{b_1}{p_1}\right) \cdots \left(\frac{c_1}{p_1}\right) \cdots \left(\frac{b_1}{p_2}\right) \cdots \left(\frac{c_1}{p_2}\right) \cdots \left(\frac{b_1}{q_1}\right) \cdots \left(\frac{c_1}{q_1}\right) \dots \\ &=

\pm \left(\frac{p_1}{b_1}\right) \cdots \left(\frac{p_1}{c_1}\right) \cdots \left(\frac{p_2}{b_1}\right) \cdots \left(\frac{p_2}{c_1}\right) \cdots \left(\frac{q_1}{b_1}\right) \cdots \left(\frac{q_1}{c_1}\right) \dots \\ &=

\pm \left(\frac{p_1}{b_1}\right) \left(\frac{p_2}{b_1}\right) \cdots \left(\frac{q_1}{b_1}\right) \cdots \left(\frac{p_1}{b_2}\right) \left(\frac{p_2}{b_2}\right) \cdots \left(\frac{q_1}{b_2}\right) \dots \\ &=

\pm \left(\frac{p_1p_2\cdots q_1\cdots}{b_1}\right) \left(\frac{p_1p_2\cdots q_1 \cdots}{b_2}\right) \cdots \left(\frac{p_1p_2\cdots q_1 \cdots}{c_1}\right) \dots \\ &=

\pm \left(\frac{m}{b_1}\right) \left(\frac{m}{b_2}\right) \cdots \left(\frac{m}{c_1}\right) \dots \\ &=

\pm \left(\frac{m}{a}\right). \end{align} $$

The sign is negative if and only if the number of $$c_iq_j$$ factors is odd, i.e. when both the number of $$q_i$$ is odd and the number of $$c_j$$ is odd. This shows that
 * $$\left(\frac{a}{m}\right) =

\begin{cases} \;\;\;\left(\frac{m}{a}\right)& \text{if }m \equiv 1 \pmod 4 \text{ or } a\equiv 1\pmod 4\\ -\left(\frac{m}{a}\right) & \text{if }m \equiv 3 \pmod 4 \text{ and } a\equiv 3 \pmod 4 \end{cases} $$ which is equivalent to
 * $$\left(\frac{a}{m}\right) = \left(\frac{m}{a}\right)(-1)^{\tfrac{m-1}{2}\tfrac{a-1}{2}}.$$

Gauss
$$a$$ must be positive and odd.

Define the $$^*$$ operator on positive odd $$m$$ as:

m^* = \begin{cases} \;\;\;m & \text{if }m \equiv 1 \pmod 4\\ -m & \text{if }m \equiv 3 \pmod 4. \end{cases} $$

This operator is multiplicative $$ (ab)^*=a^*b^*.$$

Let $$m=p_1p_2\cdots$$ and $$a=b_1b_2\cdots$$.

For every Legendre symbol in the product it is known that $$\left(\tfrac{b}{p}\right)=\left(\tfrac{p^*}{b}\right).$$

$$\begin{align} \left(\frac{a}{m}\right) &=

\left(\frac{a}{p_1}\right) \left(\frac{a}{p_2}\right) \cdots \\ &=

\left(\frac{b_1 b_2\cdots}{p_1}\right) \left(\frac{b_1 b_2 \cdots}{p_2}\right) \cdots \\ &=

\left(\frac{b_1}{p_1}\right) \left(\frac{b_2}{p_1}\right) \cdots \left(\frac{b_1}{p_2}\right) \left(\frac{b_2}{p_2}\right) \cdots \\ &=

\left(\frac{p_1^*}{b_1}\right) \left(\frac{p_1^*}{b_2}\right) \cdots \left(\frac{p_2^*}{b_1}\right) \left(\frac{p_2^*}{b_2}\right) \cdots \\ &=

\left(\frac{p_1^*}{b_1}\right) \left(\frac{p_2^*}{b_1}\right) \cdots \left(\frac{p_1^*}{b_2}\right) \left(\frac{p_2^*}{b_2}\right) \cdots \\ &=

\left(\frac{p_1^*p_2^*\cdots}{b_1}\right) \left(\frac{p_1^*p_2^*\cdots}{b_2}\right) \cdots \\ &=

\left(\frac{m^*}{b_1}\right) \left(\frac{m^*}{b_2}\right) \cdots \\ &=

\left(\frac{m^*}{a}\right). \end{align} $$

Gauss' lemma
Gauss's lemma extends to Jacobi symbols. Let $$m>0$$ be an odd number and define the sets $$I =\{i:1\le i \le \tfrac{m-1}{2}\}  $$ and $$-I=\{-j:j\in I\}$$ Then $$(\mathbb{Z} / m \mathbb{Z})=I\cup-I\cup\{0\}$$. For $$a\in I,\gcd(a,m)=1$$ let $$t=\#\{j \in I: a j \in-I\}$$.

Then $$\left(\frac{a}{m}\right)=(-1)^t$$.

Zolotarev's lemma
Zolotarev's lemma extends to Jacobi symbols. For $$a\in (\mathbb{Z}/m\mathbb{Z})^\times$$  let $$\pi_a$$ be the permutation of the set $$(\mathbb{Z}/m\mathbb{Z})$$ (all residue classes, not just the relatively prime ones) induced by multiplication by $$a$$. The signature of this permutation is the Jacobi symbol:
 * $$\left(\frac{a}{m}\right) = \varepsilon(\pi_a)$$

See here for details and an example.

Calculating the Jacobi symbol
The expression $$a \bmod b $$ means any number $$x $$ satisfying $$x\equiv a \pmod b$$ and $$|x|<|b|.$$

Repeat these steps until $$ a$$ is 0 or 1. If it is 0, the two original numbers were not relatively prime; if it is 1 the accumulated sign is the value of the Jacobi symbol.

1) if $$ a$$ is negative $$ \left(\tfrac{a}{m}\right)  =  \left(\tfrac{-1}{m}\right)\left(\tfrac{|a|}{m}\right)$$            I.e. flip the sign if $$m\equiv 3 \pmod4$$.

2) If $$ a $$ is even repeatedly divide by 4 until $$ a$$ is odd or is twice an odd number.
 * In the latter case $$ \left(\tfrac{2a}{m}\right)  =  \left(\tfrac{2}{m}\right)\left(\tfrac{a}{m}\right)$$                I.e. flip the sign if $$m\equiv \pm3 \pmod8$$.

3) If $$a$$ is odd and positive $$ \left(\tfrac{a}{m}\right)  =(-1)^{\tfrac{m-1}{2}\tfrac{a-1}{2}}\left(\tfrac{m\bmod a}{a}\right) $$               I.e. flip the sign if $$m\equiv 3 \pmod4$$ and $$a\equiv 3 \pmod4$$.

A few examples:

$$ \left(\tfrac{-24}{5}\right)= \left(\tfrac{-1}{5}\right)\left(\tfrac{24}{5}\right)= \left(\tfrac{24}{5}\right)= \left(\tfrac{8}{5}\right)\left(\tfrac{3}{5}\right)= \left(\tfrac{2}{5}\right)\left(\tfrac{3}{5}\right) -\left(\tfrac{3}{5}\right)= -\left(\tfrac{2}{3}\right)=1 $$

$$ \left(\tfrac{-24}{7}\right)= \left(\tfrac{-1}{7}\right)\left(\tfrac{24}{7}\right)= -\left(\tfrac{24}{7}\right)= -\left(\tfrac{8}{7}\right)\left(\tfrac{3}{7}\right)= -\left(\tfrac{2}{7}\right)\left(\tfrac{3}{7}\right)= -\left(\tfrac{3}{7}\right)= \left(\tfrac{4}{3}\right)=1 $$

Analysis
Calculating the Jacobi symbol is similar to computing the gcd using the Euclidean algorithm.

$$\operatorname{Euclid}(a,b)$$
 * $$\operatorname{if}\;( a\le 0 \;\operatorname{or}\; b\le 0) \operatorname{fail}$$


 * $$\operatorname{if} \;(a= b )\operatorname{return} a$$
 * $$\operatorname{return} \operatorname{Euclid}(b \bmod a, a)$$

$$\operatorname{end} \operatorname{Euclid}$$

$$\operatorname{Jacobi}(a,m)$$
 * $$\operatorname{if} \; (m < 0 \;\operatorname{or} \;m \equiv 0 \pmod{2}) $$ $$\operatorname{fail}$$


 * $$\operatorname{if} \;(a = 0)$$ $$\operatorname{return} 0$$
 * $$\operatorname{if} \;(a = 1) $$ $$\operatorname{return} 1$$


 * Examine the bits in $$a$$ to find $$ a= s2^ka'$$ where
 * $$s = \pm 1, \;k \ge 0, \;a'>0,\;a' \equiv 1 \pmod{2}.$$


 * $$\operatorname{if}\;(s < 0 \operatorname{and} m \equiv 3 \pmod{4})$$
 * $$\operatorname{return} -\operatorname{Jacobi}(2^ka',m)$$


 * $$ \operatorname{if}\; (k \equiv 1 \pmod{2} \operatorname{and}m \equiv \pm 3 \pmod{8}) $$
 * $$\operatorname{return} -\operatorname{Jacobi}(a',m)$$


 * $$\operatorname{if}\;(a' \equiv 3\pmod{4} \operatorname{and} m \equiv 3\pmod{4}) $$
 * $$\operatorname{return} -\operatorname{Jacobi}(m \bmod a', a')$$


 * $$ \operatorname{return} \operatorname{Jacobi}(m \bmod a', a')$$

$$ \operatorname{end} \operatorname{Jacobi}$$

To show the parallel use the notation $$[\tfrac{a}{b}] $$for the gcd

[\tfrac{13}{21}] = [\tfrac{8}{13}] = [\tfrac{5}{8}] = [\tfrac{3}{5}]= [\tfrac{2}{3}]= [\tfrac{1}{2}]=

1 $$ Lamé’s Theorem (1844) states that the number of $$\operatorname{mod}$$ evaluations to compute a gcd is at most five times the number of base-ten digits of the smaller number. The Jacobi calculation does the same sort of recursion but sometimes divides by powers of 4. This has the effect of skipping ahead (the example would stop after the first step), reducing the number of $$\operatorname{mod}$$ operations and also reducing the size of the numbers compared to Euclid. In other words, the Lamé bound applies to Jacobi as well.

So, for example 2000-digit numbers will require about twice as many $$\operatorname{mod}$$s as 1000-digit ones. The cost of $$\operatorname{mod}$$ may go up with the number of digits.

Machine calculation
The algorithm is well-suited to machine calculations since its running time is $$ O (\min(\log a,\log m))$$ and it requires negligeable storage. A number of authors have published code to evaluate Jacobi. The Lua and C++ routines below replace the recursion with a loop.

Uses
The Jacobi symbol is of theoretical interest in number theory, but its main use is computational. Cryptography in particular uses large prime numbers. A number of primality testing and integer factorization algorithms take advantage of the fact that Jacobi symbols can be computed quickly.

Because Euler's criterion is not valid for composite $$m$$, the Jacobi symbol can be used to detect composite numbers. Pick a random number $$a$$ and calculate the Jacobi symbol $$\left(\tfrac{a}{m}\right)$$. If $$\left(\tfrac{a}{m}\right)\not\equiv a^\tfrac{m-1}{2}\pmod m$$ then $$m$$ is composite; this is the basis of the Solovay–Strassen primality test. In some algorithms, such as the Lucas–Lehmer primality test, the value of certain Jacobi symbols is known theoretically and can be used to detect (hardware or software) errors.

An example of a factoring algorithm that uses Jacobi symbols is the continued fraction algorithm To factor $$N $$it tries to find numbers satisfying $$x^2 \equiv y^2 \pmod{N}$$. This involves constructing a database of primes and quadratic residues which requires multiple Jacobi symbol evaluations.

History
The Legendre symbol was introduced in 1798. It is not practical for calculations because it requires factorization into primes before it can be inverted. Jacobi introduced his symbol in 1837. to facilitate calculation. As explained here Gauss' first proof of quadratic reciprocity considers composite moduli and obtains some special cases of the Jacobi symbol.

Table
($k⁄n$)  for  1≤ k ≤ 30,   1≤ n ≤ 59,    n odd.