\chapter{Binary Quadratic Forms}   
\label{ch:quadform}

\section{Sums of Two Squares}

In this section we consider the question of which integers are the sum
of two squares.

During the next four lectures, we will study binary quadratic forms.
A simple example of a binary quadratic form that will occupy us today is 
$$
  x^2 + y^2.
$$
A typical question that one asks about a quadratic form is which
integers does it represent.  ``Are there integers~$x$ and~$y$ 
so that $x^2 + y^2 = 389$?  So that $x^2 + y^2 = 2001$?''


\subsection{Which Numbers are the Sum of Two Squares?}
The main goal of today's lecture is to prove the following theorem.
\begin{theorem}\label{thm:sumsquare}
A number~$n$ is a sum of two squares if and only if all prime factors
of~$n$ of the form $4m+3$ have even exponent in the prime factorization
of~$n$.
\end{theorem}
Before tackling a proof, we consider a few examples.
\begin{example}\mbox{}\vspace{-3ex}\\
\begin{itemize}
\item $5 = 1^2 + 2^2$.
\item $7$ is not a sum of two squares.
\item $2001$ is divisible by $3$ because $2+1$ is, but not by $9$ since $2+1$ is not, so $2001$ is {\em not} a sum of two squares.
\item $2\cdot 3^4\cdot 5\cdot 7^2\cdot 13$ is a sum of two squares.
\item $389$ is a sum of two squares, since $389\con 1\pmod{4}$ and $389$ is prime.
\item $21=3\cdot 7$ is {\em not} a sum of two squares even though $21\con 1\pmod{4}$.
\end{itemize}
\end{example}

In preparation for the proof of Theorem~\ref{thm:sumsquare}, we recall a
result that emerged when we analyzed how  partial convergents
of a continued fraction converge.
\begin{lemma}\label{lem:approx}
If $x\in\R$ and $n\in\N$, then there is a fraction $\ds\frac{a}{b}$
in lowest terms such that $0<b\leq n$ and
$$\left| x - \frac{a}{b} \right| \leq \frac{1}{b(n+1)}.$$
\end{lemma}
\begin{proof}
Let $[a_0,a_1,\ldots]$ be the continued fraction expansion of~$x$.
As we saw in the proof of Theorem~2.3 in Lecture~18, for each~$m$
$$
 \left| x - \frac{p_m}{q_m}\right|
  < \frac{1}{q_m \cdot q_{m+1}}.
$$
Since $q_{m+1}$ is always at least~$1$ bigger than $q_m$ and $q_0=1$, 
either there exists an~$m$ such that $q_m\leq n < q_{m+1}$, or the
continued fraction expansion of~$x$ is finite and $n$ is larger
than the denominator of the rational number~$x$.  In the first
case,
$$
  \left| x - \frac{p_m}{q_m}\right|
   < \frac{1}{q_m \cdot q_{m+1}}
      \leq \frac{1}{q_m \cdot (n+1)},$$
so $\ds\frac{a}{b} = \frac{p_m}{q_m}$ satisfies the conclusion of
the lemma.   In the second case, just let $\ds\frac{a}{b} = x$.

\end{proof}

\begin{definition}
A representation $n=x^2 + y^2$ is {\em primitive} if
$\gcd(x,y)=1$.%; otherwise, it is {\em imprimitive}.
\end{definition}

\begin{lemma}\label{lem:primitive}
If~$n$ is divisible by a prime~$p$ of the form $4m+3$, then~$n$
has no primitive representations.
\end{lemma}
\begin{proof}
If~$n$ has a primitive representation, $n=x^2 + y^2$, then
$$
   p \mid x^2 + y^2\quad \text{ and }\quad \gcd(x,y)=1,
$$
so $p\nmid x$ and $p\nmid y$.  Thus
$x^2 + y^2 \con 0\pmod{p}$
so, since $\Z/p\Z$ is a field we can divide by $y^2$ and see that
$$
  (x/y)^2 \con -1\pmod{p}.
$$
Thus the quadratic residue symbol $\kr{-1}{p}$ equals $+1$.
However,
$$
   \kr{-1}{p} = (-1)^{\frac{p-1}{2}} = (-1)^\frac{4m+3-1}{2} = (-1)^{2m+1} = -1.
$$
\end{proof}

\begin{proof}[Proof of Theorem~\ref{thm:sumsquare}]
\noindent$\left(\Longrightarrow\right)$
Suppose that~$p$ is of the form $4m+3$, that $p^r\mid\mid n$ (exactly
divides) with~$r$ odd, and that $n=x^2+y^2$.  Letting $d=\gcd(x,y)$,
we have
$$
   x = dx', \quad y = dy', \quad n = d^2 n'
$$
with $\gcd(x',y')=1$ and
$$
   (x')^2 + (y')^2 = n'.
$$

Because~$r$ is odd, $p\mid n'$, so Lemma~\ref{lem:primitive}
implies that $\gcd(x',y')>1$, a contradiction.
\vspace{0.3ex}

\noindent$\left(\Longleftarrow\right)$
Write $n=n_1^2 n_2$ where $n_2$ has no prime factors of the 
form $4m+3$.  It suffices to show that~$n_2$ is a sum of two
squares.  Also note that
$$
  (x_1^2 + y_1^2)(x_2^2+y_2^2) = (x_1x_2+y_1y_2)^2 + (x_1y_2-x_2y_1)^2,
$$
so a product of two numbers that are sums of two squares is also
a sum of two squares.\footnote{This algebraic identity is secretely
the assertion that the norm map $N:\Q(i)^* \ra \Q^*$ sending
$x+iy$ to $(x+iy)(x-iy)=x^2+y^2$ is a homomorphism.}
Also, the prime~$2$ is a sum of two squares.
It thus suffices to show that if~$p$ is a prime of the form
$4m+1$, then~$p$ is a sum of two squares.

Since 
$$
  (-1)^{\frac{p-1}{2}} = (-1)^{\frac{4m+1-1}{2}} = +1,
$$
$-1$ is a square modulo~$p$; i.e., there exists~$r$ such
that $r^2\con -1\pmod{p}$.  Taking $n=\lfloor \sqrt{p}\rfloor$
in Lemma~\ref{lem:approx} we see that there are integers $a, b$
such that
$0<b<\sqrt{p}$ and 
$$
 \left| -\frac{r}{p} - \frac{a}{b}\right| \leq\frac{1}{b(n+1)} < \frac{1}{b\sqrt{p}}.
$$
If we write
$$
  c = rb + pa
$$
then
$$
  |c| < \frac{pb}{b\sqrt{p}} = \frac{p}{\sqrt{p}} = \sqrt{p}
$$
and
$$
   0 < b^2 + c^2 < 2p.
$$
But $c \con rb\pmod{p}$, so 
$$
  b^2 + c^2 \con b^2 + r^2 b^2 \con b^2(1+r^2) \con 0\pmod{p}.
$$
Thus $b^2 + c^2 = p$.
\end{proof}

\subsection{Computing $x$ and $y$}
Suppose~$p$ is a prime of the form $4m+1$.  There is a construction of
Legendre of~$x$ and~$y$ that is explained on pages 120--121 of
Davenport.  I'm unconvinced that it is any more efficient than the
following naive algorithm: compute $\sqrt{p-x^2}$ for $x=1,2,\ldots$
until it's an integer.  This takes at most $\sqrt{p}$ steps.  Here's
a simple PARI program which implements this algorithm.

\begin{verbatim}
{sumoftwosquares(n) =
   local(y);
   for(x=1,floor(sqrt(n)),
      y=sqrt(n-x^2); 
      if(y-floor(y)==0, return([x,floor(y)]))
   );
   error(n," is not a sum of two squares.")
}
\end{verbatim}

\subsection{Sums of More Squares}
Every natural number is a sum of {\bf four} squares.  See pages
124--126 of Davenport for a proof.  

A natural number is a sum of {\bf three} squares if and only if it is not a
power of~$4$ times a number that is congruent to~$7$ modulo~$8$.  For
example, $7$ is not a sum of three squares.  This is more difficult to
prove.






\section{Binary Quadratic Forms}



\subsection{Introduction}
A {\em binary quadratic form} is a homogeneous polynomial
$$
  ax^2 + bxy + cy^2 \in\Z[x,y].
$$
(There is a theory of quadratic forms in $n$-variables, but we will
not study it in this course.)  Chapter VI of Davenport's book is clear
and well written.  Read it.

\hd{The Classic Problem:}  {\sf Given a binary quadratic form
$f(x,y) = ax^2 + bxy + cy^2$,
what is the set of integers $\{f(x,y) : x, y\in\Z\}$?}\vspace{1ex}

That is, for which integers~$n$ are there integers~$x$ and~$y$ such
that 
$$
  ax^2 + bxy + cy^2 = n?
$$
We gave a clean answer to this question in the last lecture
in the case when $f(x,y) = x^2 + y^2$.  The set
of sums of two squares is the set of integers~$n$ 
such that any prime divisor~$p$ of~$n$ of the form $4m+3$ exactly
divides~$n$ to an even power (along with~$0$).
In your homework (Problem~5), you will give a simple answer to 
the question of which numbers are of the form $x^2+2y^2$. 
Is there a simple answer in general?

\subsection{Equivalence}
\begin{definition}
The modular group $\SL_2(\Z)$ is the group of all $2\times 2$
integer matrices with determinant $+1$.
\end{definition}
If $g=\abcd{p}{q}{r}{s}\in\SL_2(\Z)$ and 
$f(x,y)=ax^2 + bxy+cy^2$ is a quadratic form, let 
$$
  f|_g (x,y)= f(px+qy, rx+sy) =
    f\left(\mtwo{p}{q}{r}{s}\vtwo{x}{y}\right),
$$
where for simplicity we will sometimes write
$f\left(\vtwo{x}{y}\right)$ for $f(x,y)$.
\begin{proposition}
The above formula defines a right action of the group $\SL_2(\Z)$
on the set of binary quadratic forms, in the sense that
$$
   f|_{gh} = (f|_g)|_h.
$$
\end{proposition}
\begin{proof}
$$
  f|_{gh}(x,y) = f\left(gh\vtwo{x}{y}\right)
                 = f|_g\left(h\left(\vtwo{x}{y}\right)\right)
  = (f|_g)|_h(x,y).
$$
%       &= f\left(g\left(h\vtwo{x}{y}\right)\right) 
%        = f\left(gh\vtwo{x}{y}\right)
%        = f|_{gh}\left(\vtwo{x}{y}\right).
%\end{align*}
\end{proof}

\begin{proposition}\label{prop:samerep}
Let $g\in\SL_2(\Z)$ and let $f(x,y)$ be a binary quadratic form.
The set of integers represented by $f(x,y)$ is exactly the same
as the set of integers represented by $f|_g(x,y)$.
\end{proposition}
\begin{proof}
If $f(x_0, y_0)=n$  then 
since $g^{-1}\in\SL_2(\Z)$, we have $g^{-1}\vtwo{x_0}{y_0}\in\Z^2$, so
$$
  f|_g\left(g^{-1}\vtwo{x_0}{y_0}\right) = f (x_0, y_0) = n.
$$
Thus every integer represented by~$f$ is also represented by $f|_g$.
Conversely, if $f|_g(x_0,y_0)=n$, then 
$f\left(g\vtwo{x_0}{y_0}\right) = n$, 
so~$f$ represents~$n$.
\end{proof}

Define an equivalence relation $\sim$ on the set of all binary
quadratic forms by declaring that~$f$ is equivalent to~$f'$ if
there exists $g\in\SL_2(\Z)$ such that $f|_g = f'$.

For simplicity, we will sometimes denote the quadratic form
$ax^2 + bxy+cy^2$ by $(a,b,c)$.  
Then, for example, since $g=\abcd{0}{-1}{1}{\hfill 0}\in\SL_2(\Z)$,
we see that
$(a,b,c)\sim (c,-b,a)$,
since if $f(x,y) = ax^2 + bxy + cy^2$, then
$f(-y,x) = ay^2 - bxy + cx^2$.

\begin{example}
Consider the binary quadratic form
$$
  f(x,y) = 458x^2 + 214xy + 25y^2.
$$
Solving the representation problem for~$f$ might, at first glance,
look hopeless.  We find $f(x,y)$ for a few values of~$x$ 
and~$y$:
\begin{align*}
f(-1,-1)&=17\cdot 41\\
f(-1,0)&=2\cdot 229\\
f(0,-1)&=5^2\\
f(1,1)&=269\\
f(-1,2)&=2\cdot 5\cdot 13\\
f(-1,3)&=41
\end{align*}
Each number is a sum of two squares!
Letting $g=\abcd{\hfill4}{-3}{-17}{13}$, we have
$$f|_g = 
   458(4x-3y)^2 + 214(4x-3y)(-17x+13y)
    + 25(-17x+13y)^2 = \cdots = x^2 + y^2!!
$$
By Proposition~\ref{prop:samerep}, $f$ represents an integer~$n$
if and only if~$n$ is a sum of two squares.
\end{example}


\subsection{Discriminants}
\begin{definition}
The {\em discriminant} of $f(x,y) = ax^2 + bxy+cy^2$ is 
$b^2 - 4ac$.
\end{definition}
\begin{example}
$\disc(x^2 + y^2) = -4$ and 
$$\disc(458,214,25) = 214^2 - 4\cdot 25\cdot 458 = -4.$$
That the discriminants are the same is a good hint that
$(1,0,1)$ and $(458,214,25)$ are closely related.  
Inspecting discriminants is more effective than simply computing $f(x,y)$
for many values of~$x$ and~$y$ and staring at the result.
\end{example}
\begin{proposition}
If $f\sim f'$, then $\disc(f) = \disc(f')$.
\end{proposition}
\begin{proof}
By tedious but elementary algebra (see page 133 of Davenport's book),
one sees that if $g\in\SL_2(\Z)$, then
$$
  \disc(f|_g) = \disc(f)\cdot (\det(g))^2 = \disc(f).
$$
Since $f'=f|_g$ for some $g\in\SL_2(\Z)$, the proposition
follows.
\end{proof}

\noindent{\bf WARNING:} The converse of the proposition is false!
Forms with the same discriminant need not be equivalent.  For example,
the forms $(1,0,6)$ and $(2,0,3)$ have discriminant $-24$, but are not
equivalent.  To see this, observe that $(1,0,6)$ represents~$1$, but
$2x^2 + 3y^2$ does not represent~$1$.

\begin{proposition}
The set of all discriminants of forms is exactly the set of integers~$d$
such that $d\con 0\text{ or }1\pmod{4}$.
\end{proposition}
\begin{proof}
First note that $b^2-4ac$ is a square modulo~$4$, so it must equal $0$
or $1$ modulo~$4$.  Next suppose~$d$ is an integer
such that $d\con 0\text{ or }1\pmod{4}$.   If we set
$$
  c = \begin{cases}
  -d/4, &\text{if $d\con 0\pmod{4}$}\\
  -(d-1)/4 & \text{if $d\con 1\pmod{4}$},
      \end{cases}
$$
then $\disc(1,0,c)=d$ in the first case and
$\disc(1,1,c)=d$ in the second.
\end{proof}

\begin{definition}
The form $(1,0,-d/4)$ or $(1,1,-(d-1)/4)$ of discriminant~$d$ that
appears in the proof of the previous proposition is called the
{\em principal form} of discriminant~$d$.
\end{definition}

\begin{center}
\begin{tabular}{rc|cll}
$d$ &&&principal form&  \\\hline
$-4$ &&& (1,0,1) & $x^2 + y^2$\\
$5$ &&& (1,1,-1)&$x^2 + xy-y^2$\\
$-7$ &&& (1,1,2)&$x^2 + xy+2y^2$\\
$8$ &&& (1,0,-2)&$x^2 - 2y^2$\\
$-23$&&& (1,1,6)&$x^2 + xy + 6y^2$\\
$389$&&&(1,1,-97)&$x^2 + xy - 97y^2$
\end{tabular}
\end{center}



\subsection{Definite and Indefinite Forms}
\begin{definition}
A quadratic form with negative discriminant
is called {\em definite}.  A form with positive
discriminant is called {\em indefinite}.
\end{definition}
Let $(a,b,c)$ be a quadratic form.  Multiply by $4a$ and complete
the square:
\begin{align*}
  4a(ax^2+bxy+cy^2) &= 4a^2x^2 + 4abxy + 4acy^2\\
   &= (2ax+by)^2 + (4ac-b^2)y^2
\end{align*}
If $\disc(a,b,c)<0$ then $4ac-b^2=-\disc(a,b,c)>0$, so
$ax^2 + bxy+cy^2$ takes only positive or only negative values,
depending on the sign of~$a$.  In this sense, $(a,b,c)$ is very
{\sf definite} about its choice of sign.  If $\disc(a,b,c)>0$,
then $(2ax+by)^2 + (4ac-b^2)y^2$ takes both positive and negative
values, so $(a,b,c)$ does also.  

We will consider only definite forms in the next two lectures.

\subsection{Real Life}
The following text is from the documentation for binary quadratic
forms in the {\sc Magma} computer algebra system.  A quick scan of the
buzzwords emphasized (by me) below conveys an idea of where
binary quadratic forms appear in mathematics.

{\small
\begin{quote}
A binary quadratic form is an integral form $ax^2 + bxy + cy^2$ which
 is represented in {\sc Magma} by a tuple $(a, b, c)$. Binary
 quadratic forms play an central role in the {\em ideal theory of
 quadratic fields}, the classical theory of {\em complex
 multiplication}, and the theory of {\em modular forms}. Algorithms
 for binary quadratic forms provide efficient means of computing in
 the {\em ideal class group of orders} in a {\em quadratic field}. By
 using the explicit relation of definite quadratic forms with lattices
 with nontrivial endomorphism ring in the complex plane, one can apply
 {\em modular and elliptic functions} to forms, and exploit the
 analytic theory of complex multiplication.

The structures of quadratic forms of a given discriminant~$D$ correspond
to ordered bases of ideals in an order in a {\em quadratic number field},
defined up to scaling by the rationals. A form is primitive if the
coefficients $a$, $b$, and $c$ are coprime. For negative discriminants the
primitive reduced forms in this structure are in bijection with the
{\em class group of projective or invertible ideals}. For positive
discriminants, the reduced orbits of forms are used for this
purpose. Magma holds efficient algorithms for composition, enumeration
of reduced forms, {\em class group computations}, and 
{\em discrete logarithms}. A
significant novel feature is the treatment of nonfundamental
discriminants, corresponding to nonmaximal orders, and the collections
of homomorphisms between different class groups coming from the
inclusions of these orders.

The functionality for binary quadratic forms is rounded out with
various functions for applying modular and elliptic functions to
forms, and for {\em class polynomials} associated to {\em class groups} of
definite forms.
\end{quote}
}





\renewcommand{\vtwo}[2]{\left[
        \begin{matrix}#1\\#2
        \end{matrix}\right]}
\section{Reduction Theory}


Recall that a binary quadratic form is a function 
$f(x,y) = ax^2 + bxy+cy^2$.  Our motivating problem
is to decide which numbers are ``represented'' by~$f$;
i.e., for which integers~$n$ do there exist integers
$x, y$ such that $ax^2 + bxy + cy^2 = n$?  If $g\in\SL_2(\Z)$
then $f(x,y)$ and $f|_g(x,y) = f\left(g\vtwo{x}{y}\right)$ represent
exactly the same set of integers.  Also, $\disc(f)=\disc(f|_g)$,
where 
$\disc(f) = b^2 - 4ac$, 
and~$f$ is called {\em positive definite}
if $\disc(f)<0$ and $a>0$.

In today's lecture, we will learn about reduction theory, which
allows us to decide whether or not two positive definite binary
quadratic forms are equivalent under the action of $\SL_2(\Z)$.

If, in the future, you would like to pursue the theory of binary
quadratic forms in either a more algebraic or algorithmic direction, I
highly recommend that you look at Chapter~5 of Henri Cohen's book {\em
A Course in Computational Algebraic Number Theory} (GTM 138).


\subsection{Reduced Forms}
\begin{definition}[Reduced]
A positive definite quadratic form $(a,b,c)$ is {\em reduced}
if $|b|\leq a \leq c$ and if, in addition, when one of the
two inequalities is an equality (i.e., either $|b|=a$ or $a=c$),
then $b\geq 0$.
\end{definition}


There is a geometric interpretation of reduced, which we will not
use this later.  Let $D=\disc(a,b,c) = b^2 - 4ac$ and set $\tau =
\frac{-b+\sqrt{D}}{2a}$, so $\tau$ is the root of $ax^2 + bx + c$ 
with positive imaginary part.  The right action of $\SL_2(\Z)$ on
positive definite binary quadratic forms corresponds to the left
action of $\SL_2(\Z)$ by linear fractional transformations on the
complex upper half plane $\h = \{z \in \C : \Im(z)>0\}$.  The standard
fundamental domain for the action of $\SL_2(\Z)$ on~$\h$ is
$$\mathcal{F} = \left\{\tau \in \h : \Re(\tau)\in\left[-\frac{1}{2},\frac{1}{2}\right),
|\tau|>1 \text{ or } |\tau|=1\text{ and }\Re(\tau)\leq 0\right\}.$$
Then $(a,b,c)$ is reduced if and only if the corresponding complex
number~$\tau$ lies in $\mathcal{F}$.  For example, if $(a,b,c)$
is reduced then $\Re(\tau) = -b/2a\in[-1/2,1/2)$ since
$|b|\leq a$ and if $|b|=a$ then $b\geq 0$.  Also
$$
  |\tau| = \sqrt{\frac{b^2 + 4ac - b^2}{4a^2}} 
        = \sqrt{\frac{c}{a}} \geq 1$$
and if $|\tau|=1$ then $b\geq 0$ so $\Re(\tau) \leq 0$.

The following theorem (which is not proved in Davenport) highlights the
importance of reduced forms.
\begin{theorem}
There is exactly one reduced form in each equivalence class of
positive definite binary quadratic forms.
\end{theorem}
\begin{proof}
We have to prove two things.  First, that every class contains at least
one reduced form, and second that this reduced form is the only
one in the class.

We first prove that there is a reduced form in every class.
Let $\mathcal{C}$ be an equivalence class of positive definite
quadratic forms of discriminant~$D$.  Let~$(a,b,c)$ be
an element of $\mathcal{C}$ such that~$a$ is minimal (amongst elements
of $\mathcal{C}$).  Note that for any such form we
have $c\geq a$, since $(a,b,c)$ is equivalent to 
$(c,-b,a)$ (use the matrix $\abcd{0}{-1}{1}{\hfill 0}$).
Applying the element $\abcd{1}{k}{0}{1}\in\SL_2(\Z)$ to $(a,b,c)$
for a suitably chosen integer~$k$ (precisely, 
$k=\lfloor(a-b)/2a\rfloor$) results in a form
$(a',b',c')$ with $a'=a$ and $b'\in (-a',a']$.
Since $a'=a$ is minimal, we have just as above that
$a'\leq c'$, hence
$(a',b',c')$ is ``just about'' reduced.
The only possible remaining problem would occur if $a'=c'$
and $b'<0$. In that case, changing $(a',b',c')$ to 
$(c'',b'',a'')=(c',-b',a')$
results in an equivalent form with $b''>0$, so that 
$(c'',b'',a'')$ is reduced.

Next suppose $(a,b,c)$ is a reduced form.  We
will now establish that $(a,b,c)$ is the only reduced form 
in its equivalence class.  First, we check that~$a$ is minimal amongst all
forms equivalent to~$(a,b,c)$.  Indeed, every other $a'$ has the
form $a'=ap^2 + bpr + cr^2$ with $p,r$ coprime integers (see this
by hitting $(a,b,c)$ by $\abcd{p}{q}{r}{s}$).  The
identities 
$$
 a p^2 + bpr + c r^2 = a p^2 \left(1+\frac{b}{a}\frac{r}{p}\right) 
   + cr^2 = a p^2 + c r^2\left( 1 + \frac{b}{c}\frac{p}{r}\right)
$$
then imply our claim since $|b|\leq a \leq c$
(use the first identity if $r/p<1$ and the second otherwise).
Thus any other reduced form $(a',b',c')$ equivalent to $(a,b,c)$
has $a'=a$.  But the same identity implies that
the only forms equivalent to $(a,b,c)$ with $a'=a$ are obtained by applying
a transformation of the form $\mtwo{1}{k}{0}{1}$ (corresponding
to $p=1$, $r=0$).  Thus $b' = b+2ak$ for some~$k$.  
Since $a=a'$ we have $b,b'\in(-a,a]$, so $k=0$.  
Finally 
$$
 c'=\frac{(b')^2-D}{4a'}=\frac{b^2-D}{4a} = c,
$$
so $(a',b',c')=(a,b,c)$.
\end{proof}

\subsection{Finding an Equivalent Reduced Form}
Here is how to find the reduced form equivalent
to a given positive definite form $(a,b,c)$.
This algorithm is useful for solving problems 8 and 9 on
the homework assignment.
Consider the following two operations, which can be used to diminish
one of~$a$ and $|b|$, without altering the other:
\begin{enumerate}
\item 
If $c<a$, replace $(a,b,c)$ by the equivalent form
$(c,-b,a)$.

\item
If $|b|>a$, replace $(a,b,c)$ by the equivalent form
$(a,b',c')$ where $b'=b+2ka$ and~$k$ is chosen so that
$b'\in(-a,a]$ (more precisely, $k=\lfloor \frac{a-b}{2a}\rfloor$),
and $c'$ is found from the fact that
$(b')^2 - 4ac'=D=\disc(a,b,c)$, so 
$c' = \frac{(b')^2 - D}{4a}$.
\end{enumerate}
Starting with $(a,b,c)$, if you iterate the appropriate operation,
eventually you will find the reduced form that is equivalent to
$(a,b,c)$.


\begin{example}
Let $f=458x^2 + 214xy + 25y^2$.  
\begin{center}
\begin{tabular}{|l|l|l|}\hline
Equivalent form & What I did & Matrix\\\hline
$(458,214,25)$ &              & \\\hline
$(25,-214,458)$& (1)            & $\abcd{0}{-1}{1}{0}$\\\hline
$(25,-14,2)$   & (2) with $k=4$ & $\abcd{1}{4}{0}{1}$\\\hline
$(2,14,25)$    & (1)            & $\abcd{0}{-1}{1}{0}$\\\hline
$(2,2,1)$      & (2) with $k=-3$& $\abcd{1}{-3}{0}{1}$\\\hline
$(1,-2,2)$     & (1)            & $\abcd{0}{-1}{1}{0}$\\\hline
$(1,0,1)$     & (2) with $k=1$ & $\abcd{1}{1}{0}{1}$\\\hline
\end{tabular}
\end{center}

Let 
\begin{align*}
 g &= \mtwo{0}{-1}{1}{0}\cdot \mtwo{1}{4}{0}{1}\cdot
 \mtwo{0}{-1}{1}{0}\cdot \mtwo{1}{-3}{0}{1}\cdot
 \mtwo{0}{-1}{1}{0}\cdot \mtwo{1}{1}{0}{1}\\
  &= \mtwo{\hfill3}{\hfill4}{-13}{-17}.
\end{align*}
Then 
$$f|_g = x^2 + y^2!$$
\end{example}

\subsection{Some PARI Code}
The following PARI code checks whether or not a form is reduced,
and computes the reduced form equivalent to a given form.
You can download it from my web page if you don't want to type
it in.
\begin{verbatim}
\\ true if and only if (a,b,c) is reduced.
{isreduced(a,b,c) = 
   if(b^2-4*a*c>=0 || a<0, 
      error("reduce: (a,b,c) must be positive definite."));
   if(!(abs(b)<=a && a<=c), return(0));
   if(abs(b)==a || a==c, return(b>=0));
   return(1);
}

\\ reduces, printing out each step.  returns the reduced form
\\ and a matrix that transforms the input form to the reduced form.
{reduce(a,b,c,s) = 
   local(D, k, t, g); 
   D=b^2-4*a*c;
   if(D>=0 || a<0, error("reduce: (a,b,c) must be positive definite."));
   g=[1,0;0,1];
   while(!isreduced(a,b,c),       \\ ! means ``not''
      if(c<a, 
         b = -b; t = a; a = c; c = t; 
         g = g*[0,-1;1,0];
         print([a,b,c], "  \t(1)"),  \\ backslash t means ``tab''
      \\ else
         if (abs(b)>a || -b==a, 
            k = floor((a-b)/(2*a)); 
            b = b+2*k*a;
            c = (b^2-D)/(4*a);
            g = g*[1,k;0,1];
            print([a,b,c], "  \t(2) with k=",k)
         )
      )
   );
   return([a,b,c,g])
}

/* Here is an example:
? \r quadform
? reduce(458,214,25)
[25, -214, 458] (1)
[25, -14, 2]    (2) with k=4
[2, 14, 25]     (1)
[2, 2, 1]       (2) with k=-3
[1, -2, 2]      (1)
[1, 0, 1]       (2) with k=1
%22 = [1, 0, 1, [3, 4; -13, -17]]
*/
\end{verbatim}










\section{The Class Group}


\subsection{Can You Hear the Shape of a Lattice?}
After Lecture 23, Emanuele Viola asked me whether or
not the following is true: {\em ``If $f_1$ and $f_2$ are binary quadratic
forms that represent exactly the same integers, is $f_1\sim f_2$?''}
The answer is no.  For example, 
$f_1=(2,1,3)=2x^2 + xy+3y^2$ and 
$f_2=(2,-1,3)=2x^2 -xy+3y^2$
are inequivalent reduced positive definite binary quadratic
forms that represent exactly the same integers.   Note that
$\disc(f_1) = \disc(f_2)=-23$.
There appears to be a sense in which 
all counterexamples resemble the one just given.

Questions like these are central to John H. Conway's book {\em The
sensual (quadratic) form}, which I've never seen because the Cabot
library copy is checked out and the Birkhoff copy has gone missing.  The
following is taken from the {\sc MathSciNet} review (I changed the
text slightly so that it makes sense):
\begin{quote}
Chapter~$2$ begins by posing Mark Kac's question of ``hearing the
shape of a drum'', and the author relates the higher-dimensional
analogue of this idea on tori---quotients of $\mathbf{R}\sp n$ by a
lattice---to the question of what properties of a positive definite
integral quadratic form are determined by the numbers the form
represents. A property of such a form is called ``audible'' if the
property is determined by these numbers, or equivalently, by the theta
function of the quadratic form. As examples, he shows that the
determinant of the form and the theta function of the dual form are
audible. He also provides counterexamples to the higher-dimensional
Kac question, the first of which were found by J. Milnor...
\end{quote}

\subsection{Class Numbers}
\begin{proposition}\label{prop:finite}
Let $D<0$ be a discriminant.  There are only finitely
many equivalence classes of positive definite binary
quadratic forms of discriminant~$D$.
\end{proposition}
\begin{proof}
Since there is exactly one reduced binary quadratic form
in each equivalence class, it suffices to show that there are
only finitely many reduced forms of discriminant~$D$.  Recall
that if a form $(a,b,c)$ is reduced, then 
$|b| \leq a \leq c$.
If $(a,b,c)$ has discriminant~$D$ then $b^2-4ac=D$.
Since $b^2\leq a^2 \leq ac$, we have
$D = b^2-4ac\leq -3ac$, so 
$$
   3ac \leq -D.
$$
There are only finitely many positive integers $a,c$ that
satisfy this inequality.
\end{proof}

\begin{definition}
A binary quadratic form $(a,b,c)$ is {\em primitive}
if $\gcd(a,b,c)=1$.
\end{definition}

\begin{definition}
The {\em class number} $h_D$ of discriminant $D<0$ is the number
of equivalence classes of primitive positive definite binary
quadratic forms of discriminant~$D$.
\end{definition}

I computed the following table of class number $h_D$ for $-D\leq 839$
using the built-in PARI function {\tt qfbclassno(D,1)}.
Notice that there are just a few $1$s at the beginning and then no more.
\begin{center}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
3&{\bf 1}\\
7&{\bf 1}\\
11&{\bf 1}\\
15&2\\
19&{\bf 1}\\
23&3\\
27&{\bf 1}\\
31&3\\
35&2\\
39&4\\
43&{\bf 1}\\
47&5\\
51&2\\
55&4\\
59&3\\
63&4\\
67&{\bf 1}\\
71&7\\
75&2\\
79&5\\
83&3\\
87&6\\
91&2\\
95&8\\
99&2\\
103&5\\
107&3\\
111&8\\
115&2\\
119&10\\\hline
\end{tabular}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
123&2\\
127&5\\
131&5\\
135&6\\
139&3\\
143&10\\
147&2\\
151&7\\
155&4\\
159&10\\
163&{\bf 1}\\
167&11\\
171&4\\
175&6\\
179&5\\
183&8\\
187&2\\
191&13\\
195&4\\
199&9\\
203&4\\
207&6\\
211&3\\
215&14\\
219&4\\
223&7\\
227&5\\
231&12\\
235&2\\
239&15\\\hline
\end{tabular}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
243&3\\
247&6\\
251&7\\
255&12\\
259&4\\
263&13\\
267&2\\
271&11\\
275&4\\
279&12\\
283&3\\
287&14\\
291&4\\
295&8\\
299&8\\
303&10\\
307&3\\
311&19\\
315&4\\
319&10\\
323&4\\
327&12\\
331&3\\
335&18\\
339&6\\
343&7\\
347&5\\
351&12\\
355&4\\
359&19\\\hline
\end{tabular}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
363&4\\
367&9\\
371&8\\
375&10\\
379&3\\
383&17\\
387&4\\
391&14\\
395&8\\
399&16\\
403&2\\
407&16\\
411&6\\
415&10\\
419&9\\
423&10\\
427&2\\
431&21\\
435&4\\
439&15\\
443&5\\
447&14\\
451&6\\
455&20\\
459&6\\
463&7\\
467&7\\
471&16\\
475&4\\
479&25\\\hline
\end{tabular}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
483&4\\
487&7\\
491&9\\
495&16\\
499&3\\
503&21\\
507&4\\
511&14\\
515&6\\
519&18\\
523&5\\
527&18\\
531&6\\
535&14\\
539&8\\
543&12\\
547&3\\
551&26\\
555&4\\
559&16\\
563&9\\
567&12\\
571&5\\
575&18\\
579&8\\
583&8\\
587&7\\
591&22\\
595&4\\
599&25\\\hline
\end{tabular}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
603&4\\
607&13\\
611&10\\
615&20\\
619&5\\
623&22\\
627&4\\
631&13\\
635&10\\
639&14\\
643&3\\
647&23\\
651&8\\
655&12\\
659&11\\
663&16\\
667&4\\
671&30\\
675&6\\
679&18\\
683&5\\
687&12\\
691&5\\
695&24\\
699&10\\
703&14\\
707&6\\
711&20\\
715&4\\
719&31\\\hline
\end{tabular}
\begin{tabular}{|ll|}\hline
$-D$ & $h_{D}$\\\hline
723&4\\
727&13\\
731&12\\
735&16\\
739&5\\
743&21\\
747&6\\
751&15\\
755&12\\
759&24\\
763&4\\
767&22\\
771&6\\
775&12\\
779&10\\
783&18\\
787&5\\
791&32\\
795&4\\
799&16\\
803&10\\
807&14\\
811&7\\
815&30\\
819&8\\
823&9\\
827&7\\
831&28\\
835&6\\
839&33\\\hline
\end{tabular}
\end{center}

We can compute these numbers using Proposition~\ref{prop:finite}.
The following PARI program enumerates the primitive reduced
forms of discriminant~$D$.
\begin{verbatim}
{isreduced(a,b,c) = 
   if(b^2-4*a*c>=0 || a<0, 
      error("reduce: (a,b,c) must be positive definite."));
   if(!(abs(b)<=a && a<=c), return(0));
   if(abs(b)==a || a==c, return(b>=0));
   return(1);
}
{reduce(f) = 
   local(D, k, t, a,b,c); 
   a=f[1]; b=f[2]; c=f[3]; D=b^2-4*a*c;
   if(D>=0 || a<0, error("reduce: (a,b,c) must be positive definite."));
   while(!isreduced(a,b,c),       \\ ! means ``not''
      if(c<a, 
         b = -b; t = a; a = c; c = t,
      \\ else
         if (abs(b)>a || -b==a, 
            k = floor((a-b)/(2*a)); 
            b = b+2*k*a;
            c = (b^2-D)/(4*a);
         )
      )
   );
   return([a,b,c])
}
{reducedforms(D)=
   local(bound, forms, b, r);
   if (D > 0 || D%4 == 2 || D%4==3, error("Invalid discriminant"));
   bound = floor(-D/3);
   forms = [];
   for(a = 1, bound,
      for(c = 1, bound,
         if(3*a*c<=-D && issquare(4*a*c+D),
            b = floor(sqrt(4*a*c+D));
            r = reduce([a,b,c]);
            print1([a,b,c], " ----> ", r);
            if (gcd(r[1],gcd(r[2],r[3])) == 1,
               forms = setunion(forms,[r]); print(""),
               \\ else
               print ("  \t(not primitive)")
            )
         )
      )
   );
   return(eval(forms));    \\ eval gets rid of the annoying quotes.
}
\end{verbatim}

For example, when $D=-419$ the program finds exactly $9$ reduced forms:
\begin{verbatim}
? D = -419
%21 = -419
? qfbclassno(D,1)
%22 = 9
? reducedforms(D)
[1, 1, 105] ----> [1, 1, 105]
[1, 3, 107] ----> [1, 1, 105]
[1, 5, 111] ----> [1, 1, 105]
[1, 7, 117] ----> [1, 1, 105]
[1, 9, 125] ----> [1, 1, 105]
[1, 11, 135] ----> [1, 1, 105]
[3, 1, 35] ----> [3, 1, 35]
[3, 5, 37] ----> [3, -1, 35]
[3, 7, 39] ----> [3, 1, 35]
[3, 11, 45] ----> [3, -1, 35]
[5, 1, 21] ----> [5, 1, 21]
[5, 9, 25] ----> [5, -1, 21]
[5, 11, 27] ----> [5, 1, 21]
[7, 1, 15] ----> [7, 1, 15]
[9, 7, 13] ----> [9, 7, 13]
[9, 11, 15] ----> [9, -7, 13]
[13, 7, 9] ----> [9, -7, 13]
[15, 1, 7] ----> [7, -1, 15]
[15, 11, 9] ----> [9, 7, 13]
[21, 1, 5] ----> [5, -1, 21]
[25, 9, 5] ----> [5, 1, 21]
[27, 11, 5] ----> [5, -1, 21]
[35, 1, 3] ----> [3, -1, 35]
[37, 5, 3] ----> [3, 1, 35]
[39, 7, 3] ----> [3, -1, 35]
[45, 11, 3] ----> [3, 1, 35]
[105, 1, 1] ----> [1, 1, 105]
[107, 3, 1] ----> [1, 1, 105]
[111, 5, 1] ----> [1, 1, 105]
[117, 7, 1] ----> [1, 1, 105]
[125, 9, 1] ----> [1, 1, 105]
[135, 11, 1] ----> [1, 1, 105]
%23 = [[1, 1, 105], [3, -1, 35], [3, 1, 35], [5, -1, 21], [5, 1, 21], 
       [7, -1, 15], [7, 1, 15], [9, -7, 13], [9, 7, 13]]
? length(%23)
%24 = 9
\end{verbatim}

\begin{theorem}[Heegner, Stark-Baker, Goldfeld-Gross-Zagier]
Suppose~$D$ is a negative discriminant that is either square
free or~$4$ times a square-free number. Then
\begin{itemize}
\item $h_D=1$ only for $D=-3,-4,-7,-8,-11,-19,-43,-67,-163$.
\item $h_D=2$ only for $D=-15, -20, -24, -35, -40, -51, -52, -88, -91,$\\
 $ -115,-123, -148, -187, -232, -235, -267, -403, -427$.
\item $h_D=3$ only for $D=-23,-31,-59,-83,-107,-139,-211,-283,-307,$\\
    $-331,-379,-499,-547,-643,-883,-907.$
\item $h_D=4$ only for $D=-39,-55,-56,-68,\ldots,-1555.$
\end{itemize}
\end{theorem}
To quote Henri Cohen: ``The first two statements concerning class
numbers~$1$ and~$2$ are very difficult theorems proved in 1952 by
Heegner and in 1968--1970 by Stark and Baker.  The general problem of
determing all imaginary quadratic fields with a given class number has
been solved in principle by Goldfeld-Gross-Zagier, but to my 
knowledge the explicit computations have been carried to the end
only for class numbers~$3$ and~$4$ (in addition to the already known
class numbers~$1$ and~$2$).

\subsection{The Class Group}
There are {\em much} more sophisticated ways to compute $h_D$ than
simply listing the reduced binary quadratic forms of discriminant~$D$,
which is an $O(|D|)$ algorithm.  For example, there is an algorithm
that can compute $h_D$ for $D$ having $50$ digits in a reasonable
amount of time.  These more sophisticated algorithms use the fact
that the set of primitive positive definite binary quadratic forms
of given discriminant is a finite abelian group.

\begin{definition}
Let $f_1=(a_1,b_1,c_1)$ and $f_2=(a_2,b_2,c_2)$ be two 
quadratic forms of the same discriminant~$D$.  Set
$s=(b_1+b_2)/2$, $n=(b_1-b_2)/2$ and let $u,v,w$ and~$d$
be such that
$$
  u a_1 + v a_2 + w s = d = \gcd(a_1, a_2, s)
$$
(obtained by two applications of Euclid's algorithm), and let
$d_0 = \gcd(d,c_1,c_2,n)$.  Define the composite of the equivalence
classes of the two forms $f_1$ and $f_2$ to be
the equivalence class of the form
$$
 (a_3, b_3, c_3) = \left(
   d_0 \frac{a_1 a_2}{d^2},
  b_2 + \frac{2a_2}{d}(v(s-b_2)-w c_2),
  \frac{b_3^2-D}{4a_3}\right).
$$
\end{definition}
This mysterious-looking group law is induced by ``multiplication
of ideals'' in the ``ring of integers'' of the quadratic imaginary
number field $\Q(\sqrt{D})$.
The following PARI program computes this group operation:
\begin{verbatim}
{composition(f1, f2)=
   local(a1,b1,c1,a2,b2,c2,D,s,n,bz0,bz1,u,v,w);
   a1=f1[1]; b1=f1[2]; c1=f1[3];
   a2=f2[1]; b2=f2[2]; c2=f2[3];   
   D = b1^2 - 4*a1*c1;
   if(b2^2 - 4*a2*c2 != D, error("Forms must have the same discriminant."));
   s = (b1+b2)/2;
   n = (b1-b2)/2;
   bz0 = bezout(a1,a2);
   bz1 = bezout(bz0[3],s);
   u = bz1[1]*bz0[1];
   v = bz1[1]*bz0[2];
   w = bz1[2];
   d = bz1[3];
   d0 = gcd(gcd(gcd(d,c1),c2),n);
   a3 = d0*a1*a2/d^2;
   b3 = b2+2*a2*(v*(s-b2)-w*c2)/d;
   c3 = (b3^2-D)/(4*a3);
   f3 = reduce([a3,b3,c3]);
   return(f3);
}
\end{verbatim}
Let's try the group out in the case when $D=-23$. 

\begin{verbatim}
? reducedforms(-23)
[1, 1, 6] ----> [1, 1, 6]
[2, 1, 3] ----> [2, 1, 3]
[3, 1, 2] ----> [2, -1, 3]
[6, 1, 1] ----> [1, 1, 6]
%56 = [[1, 1, 6], [2, -1, 3], [2, 1, 3]]
\end{verbatim}
Thus the group has elements $(1,1,6)$, $(2,-1,3)$, and $(2,1,3)$.
 Since $h_{-23}=3$,
the group must be cyclic of order~$3$.  Let's find the identity element.
\begin{verbatim}
? composition([1,1,6],[2,-1,3])
%58 = [2, -1, 3]
\end{verbatim}
Thus the identity element must be $(1,1,6)$. 
The element $(2,-1,3)$ is a generator for the group:
\begin{verbatim}
? composition([2,-1,3],[2,-1,3])
%59 = [2, 1, 3]
? composition([2,-1,3],[2,1,3])
%60 = [1, 1, 6]
\end{verbatim}


\begin{exercises}
\item\label{ex:qf1}
Which of the following numbers is a sum of two
squares?  Express those that are as a sum of 
two squares.
$$
-389,\,\, 12345, \,\,91210,\,\,729,\,\, 1729,\,\, 68252
$$

\item\label{ex:qf2} 
\begin{enumerate}
\item Write a simple computer program that takes a positive integer~$n$
as input and outputs a sequence {\tt [x,y,z,w]} of four integers
such that $x^2 + y^2 + z^2 + w^2=n$. 
\item  Write $2001$ as a sum of three squares.
\end{enumerate}


\item\label{ex:qf3} 
Find a positive integer that has a least three different
representations as the sum of two squares, disregarding signs and
the order of the summands. 

\item\label{ex:qf4} 
Show that a natural number~$n$ is the sum of two
integer squares if and only if it is the sum of two rational squares.

\item\label{ex:qf5} 
Mimic the proof of the main theorem of Lecture 21 to
show that an odd prime~$p$ is of the form $8m+1$ or $8m+3$ if and only
if it can be written as $p=x^2 + 2y^2$ for some choice of integers~$x$
and~$y$.  [Hint: Use the formula for the quadratic residue symbol
$\kr{-2}{p}$ from ???.]

\item\label{ex:qf6} 
A {\em triangular number} is a number that is the sum
of the first $m$ integers for some positive integer~$m$.
If~$n$ is a triangular number, show that all three of the
integers $8n^2$, $8n^2+1$, and $8n^2+2$ 
can be written as a sum of two squares.

\item\label{ex:qf7}
Prove that of any four consecutive integers, at least one is not
representable as a sum of two squares.

\item\label{ex:qf8}
Show directly that $13x^2+36xy+25y^2$ and
$58x^2+82xy+29y^2$ are each equivalent to the form
$x^2+y^2$, then find integers~$x$ and~$y$ such that
$13x^2+36xy+25y^2 = 389$.

\item\label{ex:qf9}
What are the discriminants of the forms
$199x^2-162xy+33y^2$ and $35x^2-96xy+66y^2$?  Are these
forms equivalent?

\item\label{ex:qf10} 
For any negative discriminant~$D$, let $C_D$ denote
the finite abelian group of equivalence classes of primitive positive
definite quadratic forms of discriminant~$D$.  Use a computer to
compute representatives for $C_D$ and determine the structure of $C_D$
as a produce of cyclic groups for each of the following five
values of~$D$:
$$
 D=-155, -231, -660, -12104, -10015.
$$



\end{exercises}
