\chapter{Quadratic Reciprocity}   
\label{ch:reciprocity}


\section{Primitive Roots}



\hd{Key Idea:} {\em There is an element of $(\Z/p\Z)$ of order $p-1$.}

\subsection{Polynomials over $\Z/p\Z$}
\begin{proposition}\label{prop:atmost}
Let $f\in(\Z/p\Z)[x]$ be a nonzero polynomial
over the ring $\Z/p\Z$.  Then there are at most
$\deg(f)$ elements $\alpha\in\Z/p\Z$ such that
$f(\alpha)=0$.
\end{proposition}
\begin{proof}
We proceed by induction on $\deg(f)$.  The cases
$\deg(f)=0,1$ are clear.  Write
$f = a_n x^n + \cdots a_1 x + a_0$.  If
$f(\alpha)=0$ then 
\begin{align*}
 f(x) &= f(x) - f(\alpha)\\
      &= a_n(x^n-\alpha^n) + \cdots a_1(x-\alpha) + a_0(1-1)\\
      &= (x-\alpha)(a_n(x^{n-1}+\cdots + \alpha^{n-1}) + \cdots a_1)\\
      &= (x-\alpha)g(x),
\end{align*}
for some polynomial $g(x)\in(\Z/p\Z)[x]$.
Next suppose that $f(\beta)=0$ with $\beta\neq \alpha$.  Then
$(\beta-\alpha) g(\beta) = 0$, so, since $\beta-\alpha\neq 0$
(hence $\gcd(\beta-\alpha, p)=1$, we have $g(\beta)=0$.
By our inductive hypothesis,~$g$ has at most $n-1$ roots, so
there are at most $n-1$ possibilities for~$\beta$.
It follows that~$f$ has at most~$n$ roots.
\end{proof}

\comment{
\begin{example}   
Find the roots\footnote{This 
example was part of the Harvard graduate school qualifying exam
this year.} 
of 
$$
  f = x^4 - x^3 + x^2 +x +1 \in(\Z/3\Z)[x].
$$
Solution: $f(0) = 1$, $f(1)=0$, $f(2)=0$, so the roots are
$\{1,2\}$. Also, 
$$
 f = (x+1)(x+2)(x^2+2x+2).
$$
Incidentally, 
PARI can factor polynomials over finite fields very quickly:
\begin{verbatim}
? factormod(x^4 - x^3 + x^2 +x +1,3)
%1 =
[Mod(1, 3)*x + Mod(1, 3) 1]
[Mod(1, 3)*x + Mod(2, 3) 1]
[Mod(1, 3)*x^2 + Mod(2, 3)*x + Mod(2, 3) 1]
\end{verbatim}
\end{example}
}

\begin{proposition}\label{prop:dsols}
Let $p$ be a prime number and let $d$ be a divisor
of $p-1$.  Then $f(x) = x^d-1\in(\Z/p\Z)[x]$ has
exactly~$d$ solutions.
\end{proposition}
\begin{proof}
Let $e$ be such that $de=p-1$.
We have
\begin{align*}
x^{p-1} - 1 &= (x^d)^e - 1\\
   &= (x^d - 1)((x^d)^{e-1} + (x^d)^{e-2} + \cdots + 1)\\
   &= (x^d - 1)g(x),
\end{align*}
where $\deg(g(x)) = p-1-d$.
Recall that Fermat's little theorem implies that $x^{p-1}-1$ has 
exactly $p-1$ roots in $\Z/p\Z$.  By Proposition~\ref{prop:atmost},
$f(x)$ has {\em at most}
$p-1-d$ roots and $x^d-1$ has at most~$d$ roots, so  
$g(x)$ has exactly $p-1$ roots and $x^d - 1$ has 
exactly~$d$ roots, as claimed.
\end{proof}

\hd{WARNING:} The analogue of this theorem is false
for some $f\in(\Z/n\Z)[x]$ with~$n$ composite.
For example, if $n=n_1\cdot n_2$ with $n_1, n_2\neq 1$, then
$f=nx$ has at least {\em two} distinct zeros, namely~$0$ 
and $n_2\neq 0$.

\subsection{The Structure of $(\Z/p\Z)^* = \{1,2,\ldots, p-1\}$}
In this section, we prove that the group
$(\Z/p\Z)^*$ is cylic.

\begin{definition}
A {\em primitive root} modulo~$p$ is an element of 
$(\Z/p\Z)^*$ of order $p-1$.
\end{definition}

\hd{Question:} For which primes~$p$ is there a primitive
root? (Ans. Every prime.)

\begin{lemma}\label{lemma}
Suppose $a,b\in(\Z/n\Z)^*$ have orders~$r$ and~$s$, respectively,
and that $\gcd(r,s)=1$.  Then $ab$ has order $rs$. 
\end{lemma}
This is a general fact about commuting elements of a group.
\begin{proof}
Since 
$
  (ab)^{rs} = a^{rs}b^{rs}=1,
$ the order of $ab$ is a divisor $r_1 s_1$ of $rs$, where
$r_1\mid r$ and $s_1\mid s$.
Thus
$$
  a^{r_1 s_1}b^{r_1 s_1} = (ab)^{r_1 s_1} = 1.
$$
Raise both sides to the power $r_2$, where $r_1 r_2 = r$.
Then
$$
   a^{r_1 r_2 s_1} b^{r_1 r_2 s_1} = 1,
$$
so, since $a^{r_1 r_2 s_1} = (a^{r_1 r_2})^{s_1} = 1$,
$$
  b^{r_1 r_2 s_1} = 1.
$$
This implies that $s\mid r_1 r_2 s_1$,
and, since $\gcd(s,r_1 r_2)=1$, it follows that $s=s_1$.
A similar argument shows that $r=r_1$, so the order of $ab$ is $rs$.
\end{proof}




\begin{theorem}\label{theorem}
For every prime~$p$ there is a primitive
root mod~$p$.  In other words, the 
group $(\Z/p\Z)^*$ is a cyclic group
of order $p-1$.
\end{theorem}
\begin{proof}
Write 
$$
  p-1 = q_1^{n_1}q_2^{n_2}\cdots q_r^{n_r}
$$
as a product of distinct primes $q_i$.  

By Proposition~\ref{prop:dsols},
the polynomial $x^{q_i^{n_i}}-1$ has exactly
$q_i^{n_i}$ roots, and the polynomial 
$x^{q_i^{n_i-1}}-1$ has exactly 
$q_i^{n_i-1}$ roots.  Thus there is an $a_i\in\Z/p\Z$ such
that $a_i^{q_i^{n_i}}=1$ but $a_i^{q_i^{n_i-1}}\neq 1$.
This $a_i$ has order $q_i^{n_i}$.
For each $i=1,\ldots, r$, choose such an~$a_i$.
By repeated application of Lemma~\ref{lemma}, we see that
$$
   a = a_1 a_2 \cdots a_r
$$
has order 
   $q_1^{n_1}\cdots q_r^{n_r} = p-1$,
so~$a$ is a primitive root.
\end{proof}


\begin{remark}
There are $\vphi(p-1)$ primitive roots modulo~$p$, since there are
$q_i^{n_i} - q_i^{n_i-1}$ ways to choose $a_i$.  To see this, we check
that two distinct choices of sequence $a_1,\ldots, a_r$ define two 
different primitive roots.  Suppose that
$$
  a_1 a_2 \cdots a_r = a'_1 a'_2 \cdots a'_r,
$$
with $a_i$, $a'_i$ of order $q_i^{n_i}$, for $i=1,\ldots,r$.
Upon raising both sides of this equality to the power
$s = q_2^{n_2}\cdots q_r^{n_r}$, we see that
$a_1^s = a_1'^s$.  Since $\gcd(s,q_1^{n_1})=1$, 
there exists~$t$ such that $st\con1\pmod{q_1^{n_1}}$. 
It follows that 
$$
  a_1 = (a_1^s)^t = (a_1'^s)^t = a'_1.
$$
Upon canceling $a_1$ from both sides, we see that $a_2 \cdots a_r =
 a'_2 \cdots a'_r$; by repeating the above argument, we see that $a_i =
 a'_i$ for all~$i$.  Thus, different choices of the $a_i$ must lead to
 different primitive roots; in other words, if the primitive roots are the
 same, then the $a_i$ were the same.

For example, there are $\vphi(16)=2^4-2^4=8$ primitive roots
mod $17$:
\begin{verbatim}
? for(n=1,16,if(znorder(Mod(n,17))==16,print1(n," ")))
3 5 6 7 10 11 12 14
\end{verbatim}
\end{remark}

\begin{example}
In this example, we illustrate the proof of Theorem~\ref{theorem}
when $p=13$.   We have
$$p-1 = 12 = 2^2\cdot 3.$$
The polynomial $x^4 - 1$ has roots $\{1,5,8,12\}$ and 
$x^2-1$ has roots $\{1,12\}$, so we take $a_1=5$.
The polynomial $x^3-1$ has roots $\{1,3,9\}$, so 
set $a_2=3$.  Finally, $a=5\cdot 3=15=2$.  Note
that the successive powers of $2$ are
$$2,\, 4,\, 8,\,  3,\,  6,\,  12,\,  11,\,  9,\,  5,\,  10,\,  7,\,  1,$$
so~$2$ really does have order~$12$.
\end{example}

\begin{example}
The result is false if, e.g.,~$p$ is replaced by a big power of~$2$. 
The elements of $(\Z/8\Z)^*$ all have order dividing~$2$, but
$\vphi(8)=4$.
\end{example}


\begin{theorem}\label{theorem:cyclic}
Let~$p^n$ be a power of an odd prime.  Then there is an element of
$(\Z/p^n\Z)^*$ of order $\vphi(p^n)$.  Thus $(\Z/p^n\Z)^*$ is cyclic.
\end{theorem}
I will not prove Theorem~\ref{theorem:cyclic} in class.  I will
probably put a problem on your next homework set that will guide you
to a proof.


\subsection{Artin's Conjecture}
\begin{conjecture}[Emil Artin]
If $a\in\Z$ is not $-1$ or a perfect square, then
the number $N(x,a)$ of primes $p\leq x$ such that~$a$ 
is a primitive root modulo~$p$ is asymptotic to $C(a)\pi(x)$,
where $C(a)$ is a constant that depends only on~$a$.
In particular, there are infinitely many primes~$p$ such
that $a$ is a primitive root modulo~$p$.
\end{conjecture}
Nobody has proved this conjecture for even a single choice of~$a$.
There are partial results, e.g., that there are infinitely
many $p$ such that the order of $a$ is divisible by the largest
prime factor of $p-1$. (See, e.g., Moree, Pieter, {\em 
A note on {A}rtin's conjecture}.)

%The web page 
%\begin{verbatim}
%http://www.ieeta.pt/~tos/p-roots.html
%\end{verbatim}
%contains some computational results about Artin's conjecture.




%\newcommand{\kr}[2]{\left(\frac{#1}{#2}\right)}
\section{Quadratic Reciprocity I}



\hd{Key Ideas:}
\begin{itemize}
\item {\em Euler's Criterion:} When is $a$ a square modulo~$p$?
\item Quadratic reciprocity
\item Lemma of Gauss
\end{itemize}

\subsection{Euler's Criterion}


\begin{proposition}[Euler's Criterion]\label{prop:euler}
Let~$p$ be an odd prime and~$a$ an integer not divisible by~$p$.
Then $x^2\con a\pmod{p}$ has a solution if and only if
$$
  a^{(p-1)/2}\con 1\pmod{p}.
$$  
\end{proposition}
\begin{proof}
By the theorem from Lecture~11, there is an integer~$g$ that
has order~$p-1$ modulo~$p$.  Every integer coprime to~$p$ is 
congruent to a power of~$g$.
First suppose that~$a$ is congruent to a perfect square modulo~$p$,
so 
$$
  a\con (g^r)^2 \con g^{2r}\pmod{p}
$$ 
for some~$r$.  Then
$$
  a^{(p-1)/2} \con g^{2r\cdot\frac{p-1}{2}} \con 
    g^{r(p-1)}\con 1\pmod{p}.
$$
Conversely, suppose that
$a^{(p-1)/2}\con 1\pmod{p}$.  We have 
$a\con g^r \pmod{p}$ for some integer~$r$.
Thus $g^{r(p-1)/2}\con 1\pmod{p}$, so
$$
  p-1 \mid r(p-1)/2
$$
which implies that~$r$ is even.  
Thus $a\con (g^{r/2})^2\pmod{p}$, so~$a$
is congruent to a square modulo~$p$.
\end{proof}

\begin{corollary}
If $x^2\con a\pmod{p}$ has no solutions if and only if $a^{(p-1)/2}\con
-1\pmod{p}$.
\end{corollary}
\begin{proof}
This follows from Proposition~\ref{prop:euler} and that
the polynomial $x^2-1$ has no roots besides $+1$ and $-1$.
\end{proof}



\begin{example}
Suppose $p=11$.  
By squaring each element of $(\Z/11\Z)^*$, we see
exactly which numbers are squares modulo~$11$:
$$
  1^2 = 1,\, 2^2 = 4,\, 3^2 = 9,\, 4^2 = 5,\, 5^2 = 3,\, 6^2 = 3,\, 
  7^2 = 5,\, 8^2 = 9,\, 9^2 = 4,\, 10^2 = 1.
$$
Thus the squares are $\{1,3,4,5,9\}$.
Next, we compute $a^{(p-1)/2}=a^{5}$ for each $a\in(\Z/11\Z)^*$.
$$
 1^{5} = 1,\, 2^{5} = -1,\, 3^{5} = 1,\, 4^{5} = 1,\, 5^{5} = 1,\, 
 6^{5} = -1,\, 7^{5} = -1,\, 8^{5} = -1,\, 9^{5} = 1,\, 10^{5} = -1.
$$
The~$a$ with $a^5=1$ are $\{1, 3, 4, 5, 9\}$, which is exactly the
same as the set of squares, just as Proposition~\ref{prop:euler}
predicts.

\end{example}

\begin{example}
Determine whether or not~$3$ is a square 
modulo $p=726377359$.  \newline{\bf Answer:} 
We compute $3^{(p-1)/2}$ modulo~$p$
using PARI:
\begin{verbatim}
? Mod(3,p)^((p-1)/2)
%5 = Mod(726377358, 726377359)   \\ class of -1 modulo 726377359.
\end{verbatim}
Thus~$3$ is not a square modulo~$p$.  This computation wasn't too
difficult, but it would have been very tedious to carry about by hand.
The law of quadratic reciprocity, which we will state in the next section,
is a vastly more powerful way to answer such questions.
For example, you could easily answer the above question
by hand using quadratic reciprocity.
\end{example}


\begin{remark}\label{rem:homo}
Proposition~\ref{prop:euler} can be reformulated in more group-theoretic
language as follows.  The map
$$
  (\Z/p\Z)^* \ra \{\pm 1\}
$$
that sends~$a$ to $a^{(p-1)/2}\pmod{p}$
is a homomorphism of groups, whose kernel is
the subgroup of squares of elements of $(\Z/p\Z)^*$.
\end{remark}


\begin{definition}
An element $a\in\Z$ with $p\nmid a$ is called a {\em quadratic
residue} modulo~$p$ if~$a$ is a square modulo~$p$.
\end{definition}


\subsection{The Quadratic Reciprocity Law}
Let~$p$ be an odd prime and let~$a$ be an integer with $p\nmid a$.
Set
$$
   \kr{a}{p} = 
      \begin{cases}
          +1 & \text{if $a$ is a quadratic residue, and}\\
          -1 & \text{otherwise}.
      \end{cases}
$$
Proposition~\ref{prop:euler} implies that 
$$
  \kr{a}{p} \con a^{(p-1)/2}\pmod{p}.
$$
Also, notice that 
$$
   \kr{a}{p}\cdot \kr{b}{p} = \kr{ab}{p},
$$
because
$\kr{\cdot }{p}$ is a homomorphism (see Remark~\ref{rem:homo}).

The symbol $\kr{a}{p}$ only depends on the residue class
of~$a$ modulo~$p$.  Thus tabulating the value of 
$\kr{a}{5}$ for hundreds of~$a$ would be silly.
{\em Would it be equally silly to make a table of $\kr{5}{p}$
for hundreds of primes~$p$?}  Let's begin making such a table
and see whether or not there is an obvious pattern.
(To compute $\kr{a}{p}$ in PARI, use the command {\tt kronecker(a,b)}.)
\begin{center}
\begin{tabular}{|crc|}\hline
$p$ & $\kr{5}{p}$ & $p$ mod 5\\\hline
7 & $-1 $& 2\\
11 &$ 1 $& 1\\
13 &$ -1$& 3\\
17 &$ -1$& 2\\
19 &$ 1 $& 4\\
23 &$ -1$& 3\\
29 &$ 1 $& 4\\
31 &$ 1 $& 1\\
37 &$ -1$& 2\\
41 &$ 1 $& 1\\
43 &$ -1$& 3\\
47 &$ -1$& 2\\\hline
\end{tabular}
\end{center}
The evidence suggests that $\kr{5}{p}$ depends only on the
congruence class of~$p$; more precisely, $\kr{5}{p}=1$ if and only 
if $p\con 1,4\pmod{5}$, i.e., $p$ is a square modulo~$5$.
However, when I think directly about the equation
$$
   5^{(p-1)/2} \pmod{p},
$$
I see no way that knowing that $p\con 1,4 \pmod{5}$ helps us
to evaluate that strange expression!  And yet, the numerical
evidence is so {\em compelling}!  Argh!


Based on such computations, various mathematicians found a conjectural
explanation for this mystery in the 18th century.  Finally, on April
8, 1796, at your age (age 19), Gauss proved their conjecture.

\begin{theorem}[The Law of Quadratic Reciprocity]\label{thm:recip}
Suppose that $p$ and $q$ are odd primes.  Then
$$\kr{p}{q} = (-1)^{\frac{p-1}{2}\cdot \frac{q-1}{2}}\kr{q}{p}.$$
\end{theorem}
We will prove this theorem in the next lecture.

In the case considered above, this theorem implies that
$$
  \kr{5}{p} = (-1)^{2\cdot \frac{p-1}{2}} \kr{p}{5}
            =\kr{p}{5}
            = \begin{cases} +1 & \text{ if }p\con 1, 4\pmod{5}\\
                            -1 & \text{ if }p\con 2, 3\pmod{5}.
	      \end{cases}
$$
Thus the quadratic reciprocity law ``explains'' why knowing~$p$ modulo~$5$
helps in computing $5^{\frac{p-1}{2}}\pmod{p}$. 

Here is a list of almost 200 proofs of Theorem~\ref{thm:recip}:
\begin{verbatim}
    http://www.rzuser.uni-heidelberg.de/~hb3/rchrono.html
\end{verbatim}

\subsection{A Lemma of Gauss}
The proof we will give of Theorem~\ref{thm:recip} was first discovered
by Gauss, though not when he was~$19$.  This proof is given in many
elementary number theory texts (including Davenport).  It depends on
the following lemma of Gauss:

\begin{lemma}
Let~$p$ be an odd prime and let~$a$ be an integer $\not\con 0\pmod{p}$.
Form the numbers 
$$
  a,\, 2a,\, 3a,\, \ldots,\, \frac{p-1}{2} a
$$
and reduce them modulo~$p$ to lie in the interval 
$(-\frac{p}{2},\,\, \frac{p}{2})$.
Let $\nu$ be the number of negative numbers in the resulting set.
Then 
$$
  \kr{a}{p} = (-1)^{\nu}.
$$
\end{lemma}
\begin{proof}
In defining $\nu$, we expressed each number in 
$$
  S = \left\{a, 2a, \ldots, \frac{p-1}{2} a\right\}
$$
as congruent to a number in the set
$$
  \left\{ 1, -1, 2, -2, \ldots, \frac{p-1}{2}, -\frac{p-1}{2}\right\}.
$$
No number 
$1, 2, \ldots \frac{p-1}{2}$
appears more than once, with either choice of sign, because if it
did then either two elements of~$S$ are congruent modulo~$p$ or
$0$ is the sum of two elements of~$S$, and both events are impossible.
Thus the resulting set must be of the form 
$$T = \left\{\eps_1\cdot 1, \eps_2 \cdot 2, \ldots, 
                   \eps_{(p-1)/2}\cdot \frac{p-1}{2} \right\},$$
where each $\eps_i$ is either $+1$ or $-1$.  Multiplying together
the elements of~$S$ and of~$T$, we see that
$$
   (1a) \cdot (2a)\cdot(3a)\cdot \cdots \cdot  \left(\frac{p-1}{2} a\right)
        \con 
   (\eps_1\cdot 1)\cdot (\eps_2 \cdot 2) \cdots 
      \left(\eps_{(p-1)/2}  \cdot \frac{p-1}{2}\right)\pmod{p},
$$
so 
$$
  a^{(p-1)/2} \con \eps_1\cdot \eps_2\cdot \cdots \cdot \eps_{(p-1)/2}\pmod{p}.$$
The lemma then follows from Proposition~\ref{prop:euler}.
\end{proof}






\section{Quadratic Reciprocity II}

\subsection{Recall Gauss's Lemma}
We proved the following lemma in the previous lecture. 
\begin{lemma}
Let~$p$ be an odd prime and~$a$ an integer with $p\nmid a$.
Form the numbers 
$
  a,\, 2a,\, 3a,\, \ldots,\, \frac{p-1}{2} a
$
and reduce them modulo~$p$ to lie in the interval 
$(-\frac{p}{2},\,\, \frac{p}{2})$.
Let $\nu$ be the number of negative numbers in the resulting set.
Then 
$
  \kr{a}{p} = (-1)^{\nu}.
$
\end{lemma}


\subsection{Euler's Conjecture}
\begin{lemma}\label{lem:even}
Let $a, b\in\Q$.  Then for any $n\in\Z$, 
$$\#\left((a,b)\intersect \Z\right)
\con \#\left((a,b+2n)\intersect \Z\right)
\con \#\left((a+2n,b)\intersect \Z\right)
\pmod{2}.$$
\end{lemma}
\begin{proof}
If $n>0$, then 
$$(a,b+2n) = (a,b) \union [b,b+2n),$$
where the union is disjoint.  Let $[x]$ denote the least integer
$\geq x$.  There are $2n$ integers,
$$[b], [b]+1, \ldots, [b]+2n-1,$$
in the interval $[b,b+2n)$, so
the assertion of the lemma is true in this case.
We also have
$$(a,b-2n) = (a,b)\backslash [b-2n,b)$$
and $[b-2n,b)$ also contains exactly $2n$ integers,
so the lemma is also true when~$n$ is negative. 
The statement about $\#\left((a+2n,b)\intersect \Z\right)$
is proved in a similar manner.
\end{proof}


The following proposition was first conjectured by Euler, based
on extensive numerical evidence.   Once we've proved this proposition,
it will be easy to deduce the quadratic reciprocity law.
\begin{proposition}[Euler's Conjecture]\label{prop}
Let~$p$ be an odd prime and~$a\in\N$ a natural number with $p\nmid a$.
\begin{enumerate}
\item The symbol $\kr{a}{p}$ depends only on~$p$ modulo~$4a$.
\item If~$q$ is a prime with $q\con - p \pmod{4a}$, 
then $\kr{a}{p} = \kr{a}{q}$.
\end{enumerate}
\end{proposition}
\begin{proof}
To apply Gauss's lemma, we have to compute the parity of the intersection
of 
$$S = \left\{a, 2a, 3a, \ldots \frac{p-1}{2}a\right\}$$
and 
$$I = \left(\frac{1}{2}p,p\right) \union \left(\frac{3}{2}p, 2p\right)
\union \cdots \union 
\left(\left(b-\frac{1}{2}\right)p,bp\right),$$
where 
$b=\frac{1}{2}a$ or $\frac{1}{2}(a-1)$, whichever is an integer.
{\small (Why? We have to check that every element of $S$ that reduces
to something in the interval $(-\frac{p}{2},0)$ lies in~$I$.
This is clear if $b=\frac{1}{2}a < \frac{p-1}{2}a$.  If 
$b=\frac{1}{2}(a-1)$, then $bp+\frac{p}{2} > \frac{p-1}{2}a$,
so $((b-\frac{1}{2})p,bp)$ is the last interval that could contain
an element of of $S$ that reduces to $(-\frac{p}{2},0)$.)
Also note that the integer endpoints of~$I$ are not in~$S$, since
those endpoints are divisible by~$p$, but no element of~$S$ is
divisible by~$p$.}

Dividing~$I$ through by~$a$, we see that 
$$\#(S\intersect I) = \#\left(\Z \intersect \frac{1}{a}I\right),$$
where 
$$\frac{1}{a} I = 
\left(\left(\frac{p}{2a},\frac{p}{a}\right)
\union
\left(\frac{3p}{2a},\frac{2p}{a}\right)
\union
\cdots
\union
\left(\frac{(2b-1)p}{2a},\frac{bp}{a}\right)
\right).$$

Write $p=4ac+r$, and let
$$
J = \left(\left(\frac{r}{2a},\frac{r}{a}\right)
\union
\left(\frac{3r}{2a},\frac{2r}{a}\right)
\union
\cdots
\union
\left(\frac{(2b-1)r}{2a},\frac{br}{a}\right)
\right).
$$
The only difference between~$I$ and~$J$ is that the endpoints of
intervals are changed by addition of an even integer. 
By Lemma~\ref{lem:even}, 
$$\nu=\#\left(\Z \intersect \frac{1}{a}I\right) \con \#(\Z\intersect J)\pmod{2}.$$
Thus $\kr{a}{p}=(-1)^\nu$ depends only on~$r$, 
i.e., only on~$p$ modulo~$4a$.  {\large\sc WOW!}

If $q\con -p\pmod{4a}$, then the only change in the above computation
is that~$r$ is replaced by~$4a-r$.  This changes $\frac{1}{a}I$ into
$$K = 
\left(\left(2-\frac{r}{2a},4-\frac{r}{a}\right)
\union
\left(6-\frac{3r}{2a},8-\frac{2r}{a}\right)
\union
\cdots
\union
\left(4b-2-\frac{(2b-1)r}{2a},4b-\frac{br}{a}\right)
\right).$$
Thus $K$ is the same as $-\frac{1}{a}I$, except even integers
have been added to the endpoints.  By Lemma~\ref{lem:even}, 
$$\#(K\intersect \Z)\con \#\left(\left(\frac{1}{a}I\right)\intersect\Z\right)\pmod{2},$$
so $\kr{a}{p} = \kr{a}{q}$, which completes the proof.
\end{proof}

The following more careful analysis in the special case when $a=2$
helps illustrate the proof of the above lemma, and is frequently
useful in computations.
\begin{proposition}\label{prop:p2}
Let~$p$ be an odd prime.  Then
$$
\kr{2}{p} = \begin{cases} \hfill1 & \text{ if } p\con \pm 1\pmod{8}\\
                         -1 & \text{ if } p\con \pm 3\pmod{8}
            \end{cases}.
$$
\end{proposition}
\begin{proof}
When $a=2$, the set $S = \{a,2a,\ldots,2\cdot\frac{p-1}{2}\}$ is
$$
 \{ 2, 4, 6, \ldots, p-1 \}.
$$
We must count the parity of the number of elements of~$S$
that lie in the interval $I=(\frac{p}{2}, p)$.
Writing $p=8c+r$, we have
\begin{align*}
\#\left(I\intersect S\right) &=\#\left(\frac{1}{2}I \intersect \Z\right)
=\#\left(\left(\frac{p}{4},\frac{p}{2}\right)\intersect \Z\right)\\
&=\#\left(\left(2c+\frac{r}{4}, 4c+\frac{r}{2}\right)\intersect \Z\right)
\con \#\left(\left(\frac{r}{4}, \frac{r}{2}\right)\intersect \Z\right)
\pmod{2},
\end{align*}
where the last equality comes from Lemma~\ref{lem:even}.
The possibilities for~$r$ are $1,3,5,7$.  When $r=1$, the cardinality 
is~$0$, when $r=3, 5$ it is $1$, and when $r=7$ it is $2$.
\end{proof}


\subsection{The Quadratic Reciprocity Law}
With the lemma in hand, it is straightforward to deduce the quadratic
reciprocity law.
\begin{theorem}[Gauss]\label{thm:recip}
Suppose that~$p$ and~$q$ are distinct odd primes.  Then
$$\kr{p}{q}\cdot \kr{q}{p} = (-1)^{\frac{p-1}{2}\cdot \frac{q-1}{2}}.$$
\end{theorem}
\begin{proof}
First suppose that $p\con q\pmod{4}$.  By swapping~$p$ and~$q$ if
necessary, we may assume that $p>q$, and write $p-q=4a$.  
Since $p=4a+q$, 
$$\kr{p}{q} = \kr{4a+q}{q} = \kr{4a}{q} = \kr{a}{q},$$
and
$$\kr{q}{p} = \kr{p-4a}{p} = \kr{-4a}{p} = \kr{-1}{p}\cdot \kr{a}{p}.$$
Proposition~\ref{prop} implies that 
$\kr{a}{q} = \kr{a}{p}$, since $p\con q\pmod{4a}$.  Thus
$$\kr{p}{q}\cdot\kr{q}{p} = \kr{-1}{p} = (-1)^{\frac{p-1}{2}}
     = (-1)^{\frac{p-1}{2}\cdot \frac{q-1}{2}},$$
where the last equality is because $\frac{p-1}{2}$ is even
if and only if $\frac{q-1}{2}$ is even.

Next suppose that $p\not\con q\pmod{4}$, so $p\con -q\pmod{4}$.
Write $p+q=4a$.  We have 
$$\kr{p}{q} = \kr{4a-q}{q} = \kr{a}{q},
\quad\text{ and }\quad
\kr{q}{p} = \kr{4a-p}{p} = \kr{a}{p}.$$
Since $p\con -q\pmod{4a}$, Proposition~\ref{prop}
implies that $\kr{p}{q}=\kr{q}{p}$.
Since $(-1)^{\frac{p-1}{2}\cdot \frac{q-1}{2}}=1$, the
proof is complete.
\end{proof}



\subsubsection{Examples}
\begin{example}
Is~$6$ a square modulo~$389$?
We have
$$\kr{6}{389} = \kr{2\cdot 3}{389}
=\kr{2}{389}\cdot \kr{3}{389} 
= (-1)\cdot(-1)= 1.$$
Here, we found that $\kr{2}{389} = -1$ using Proposition~\ref{prop:p2}
and that $389\con 3\pmod{8}$.  We found $\kr{3}{389}$ as follows:
$$\kr{3}{389} = \kr{389}{3} = \kr{2}{3}=-1.$$ Thus $6$ is a square
modulo~$389$.  

Annoyingly, though we know that~$6$ is a square modulo~$389$, we still
don't know an~$x$ such that $x^2\con 6\pmod{389}$!  
\begin{verbatim}
? for(a=1,388,if(Mod(a,389)^2==6,print1(a, " ")))
28 361
\end{verbatim}
\end{example}



\begin{example}
Is~$3$ a square modulo $p=726377359$?  We proved that the answer is ``no''
in the previous lecture by computing $3^{p-1}\pmod{p}$.  It's easier to
prove that the answer is no using Theorem~\ref{thm:recip}:
$$
\kr{3}{726377359}
= (-1)^{1\cdot \frac{726377358}{2}}\cdot \kr{726377359}{3}
 = -\kr{1}{3} = -1.
$$
\end{example}


\begin{exercises}
\item\label{ex:rec1} 
Calculate the following symbols by hand: $\kr{3}{97}$, $\kr{5}{389}$,
$\kr{2003}{11}$, and $\kr{5!}{7}$.

\item\label{ex:rec2}
Prove that for $p\geq 5$ prime,
$\ds
  \kr{3}{p} = 
     \begin{cases} \hfill 1 & \text{ if }p\con 1, 11\pmod{12},\\
                  -1 & \text{ if }p\con 5, 7\pmod{12}.
     \end{cases}
$  
\item\label{ex:rec3}
 Prove that there is no primitive root
modulo~$2^n$ for any $n\geq 3$.
[Relate the statement for $n=3$ to the statement for $n>3$.]
%\ans{If there were a primitive root modulo $2^n$, then there would
%be one modulo $2^3$, but there isn't as a direct check shows.}

\item\label{ex:rec4}
Prove that if~$p$ is a prime, then
there is a primitive root modulo~$p^2$.
[Hint: Write down an element of $(\Z/p^2\Z)^*$ that looks like it might
      have order~$p$, and prove that it does.  Recall that if $a, b$
      have orders $n, m$, with $\gcd(n,m)=1$, then $ab$ has order $nm$.]

\item\label{ex:rec5} 
Use the fact that $(\Z/p\Z)^*$ is cyclic to give a direct proof
that $\kr{-3}{p}=1$ when $p\con 1\pmod{3}$. [Hint: There is an $c\in (\Z/p\Z)^*$ of
order~$3$.  Show that $(2c+1)^2=-3$.]

\item\label{ex:rec6} 
If $p\con 1\pmod{5}$, show directly that $\kr{5}{p}=1$ by the method of 
Exercise~\ref{ex:rec5}. [Hint: Let $c\in(\Z/p\Z)^*$ be an element of
order~$5$.  Show that $(c+c^4)^2+(c+c^4)-1=0$, etc.]

\item\label{ex:rec7}  
For which primes~$p$ is 
$\ds \sum_{a=1}^{p-1} \kr{a}{p}=0$?

\item\label{ex:rec8}  
Artin conjectured that the number of primes $p\leq x$
such that~$2$ is a primitive root
modulo~$p$ is asymptotic to $C\pi(x)$ where $\pi(x)$ is the number of
primes $\leq x$ and~$C$ is a fixed constant called Artin's constant.
Using a computer, make an educated guess as to what~$C$ should be, to
a few decimal places of accuracy.  Explain your reasoning.  (Note:
Don't try to prove that your guess is correct.)  
%\ans{\tt ispr(n, p) =
%if(n\%p == 0, 0, znorder(Mod(n,p))==p-1);\\ A(n, d, gen=0,
%prim=0)=forprime(p=2,d,prim++;if(ispr(n,p),gen++));gen/prim\\ ?
%A(2,500000)*1.0\\ \%37 = 0.3738263758486205402282247580\\ ?
%x=1;forprime(p=2,100000,x=x*(1.0-1/(p*(p-1))));x*1.0
%$\backslash\backslash$ for fun, notice\\ \%38 =
%0.3739561136265594983326614081 }

\item\label{ex:rec9} 
How many natural numbers $x < 2^{13}$ satisfy the equation
$$x^2\con 5\pmod{2^{13}-1}?$$  
(You may assume that $2^{13}-1$ is prime.)

\item\label{ex:rec10} 
Find the natural number $x<97$ such that $x\con 4^{48}\pmod{97}$.
(Note that $97$ is prime.)

\end{exercises}
