Let $p$ be an odd prime. How many quadratic residues are there in $\mathbb{Z}_p$?
For an integer $a$ and an odd positive integer $n = \prod_{i=1}^k p_i^{e_i}$, define the Jacobi symbol:
\[\left({a \over n}\right) = \prod_{i=1}^k \left({a \over p_i}\right)^{e_i} ,\]where
\[\left({a \over p}\right) = \begin{cases} 0 & \text{if $a \equiv 0 \pmod{p}$,} \\ 1 & \text{if $a$ is a quadratic residue modulo $p$, and} \\ -1 & \text{if $a$ is a quadratic non-residue modulo $p$} \end{cases}\]is the Legendre symbol. Prove the following properties for the Jacobi symbol.
If $m_1 \equiv m_2 \pmod{n}$, then \(\left({m_1 \over n}\right) = \left({m_2 \over n}\right) .\)
Assuming $a \in \mathbb{Z}^*_p$:
Using the properties above, and additionally
and
if $m$ and $n$ are odd positive integers, then
\[\left({m \over n}\right) = \begin{cases} -\left({n \over m}\right) & \text{if $m \equiv n \equiv 3 \pmod{4}$}, \\ \left({n \over m}\right) & \text{otherwise} , \end{cases}\]write an algorithm to evaluate Jacobi symbols. The algorithm should not do any factoring other than dividing out powers of two.
def jacobi(a, n):
v = 1
while True:
a %= n # replace a by its remainder modulo n
if a == 0:
return 0
s = 1 if n % 8 in [1, 7] else -1
while a % 2 == 0:
a //= 2
v *= s
if a == 1:
return v
if a % 4 == 3 and n %% 4 == 3:
v *= -1
a, n = n, a
Calculate $\left({7411 \over 9283}\right)$, $\left({20964 \over 1987}\right)$ and $\left({1234567 \over 11111111}\right)$.