\chapter{Continued Fractions}\label{ch:contfrac}

A {\em continued fraction} is an expression of the form
$$a_0 + \frac{1}{a_1+\frac{1}{a_2+\frac{1}{a_3+\cdots}}}$$
which may or may not go on indefinitely.  We denote\footnote{{\em Warning:} 
This notation clashes with the notation 
used in Davenport.  Our notation is standard.} 
the value of this continued fraction by
$$[a_0,a_1,a_2,\ldots].$$
The $a_n$ are called the {\em partial quotients}
of the continued fraction (we will see why at the end of this lecture).
Thus, e.g., 
$$[1,2] = 1+\frac{1}{2} = \frac{3}{2},$$
and
$$\frac{172}{51} = [3,2,1,2,6]=3+\frac{1}{2+\frac{1}{1+\frac{1}{2+\frac{1}{6}}}}.$$

Continued fractions have many applications, from the abstract to the concrete.
They give good rational approximations to irrational numbers, and 
the have been used to understand why you can't tune a 
piano perfectly.\footnote{See 
{\tt http://www.research.att.com/\~{\mbox{}}njas/sequences/DUNNE/TEMPERAMENT.HTML}}
Continued fractions also suggest a sense in which~$e$ appears to
be ``less transcendental'' than~$\pi$.  

There are many places to read about continued fractions, including
Chapter X of Hardy and Wright's {\em Intro. to the Theory of Numbers},
\S13.3 of Burton's {\em Elementary Number Theory}, Chapter IV of
Davenport, and Khintchine's {\em Continued Fractions}. The notes
you're reading right now draw primarily on Hardy and Wright, since
their exposition is very clear and to the point.  I found Davenport's
chapter IV uneccessarily tedious; I felt marched through a thick
jungle to see a beautiful river.

\section{Finite Continued Fractions}
\begin{definition} 
A {\em finite continued fraction} is an expression
$$ 
       a_0 + \frac{1}{a_1+\frac{1}{a_2 + \cdots + \frac{1}{a_m},}}$$
where each $a_n$ is a rational number and $a_n>0$ for all $n\geq 1$.
If the $a_n$ are integers, we say that the continued fraction is
{\em integral}.
\end{definition}
To get a feeling for continued fractions, observe that 
\begin{align*}
[a_0] &= a_0,\\
[a_0, a_1] &= a_0 + \frac{1}{a_1} = \frac{a_0 a_1 + 1}{a_1},\\
[a_0, a_1, a_2] &= a_0 + \frac{1}{a_1 + \frac{1}{a_2}}
            = \frac{a_0 a_1 a_2 + a_0 + a_2}{a_1 a_2 + 1}.
\end{align*}
Also, 
\begin{align*}
  [a_0, a_1, \ldots ,a_{m-1}, a_m] &= 
          [a_0, a_1, \ldots, a_{m-2}, a_{m-1} + \frac{1}{a_m}]\\
        &= a_0 + \frac{1}{[a_1,\ldots, a_m]} \\
        &= [a_0, [a_1,\ldots, a_m]].
\end{align*}
\subsubsection{Partial Convergents}
Fix a continued fraction $[a_0,\ldots,a_m]$.
\begin{definition}
For $0\leq n\leq m$, the $n$th {\em convergent} of the 
continued fraction $[a_0,\ldots,a_m]$
is $[a_0,\ldots, a_n]$.
\end{definition}
For each $n\geq -1$, define real numbers $p_n$ and $q_n$ as follows:
\begin{align*}
  p_{-1} &= 1, \quad &p_0 = a_0, \qquad &p_1 = a_1 p_0 + p_{-1} = a_1 a_0 + 1,
              \quad &p_n = a_n p_{n-1} + p_{n-2},\\
  q_{-1} &= 0, \quad &q_0 = 1, \qquad &q_1 = a_1 q_0 + q_{-1} = a_1,
              \quad &q_n = a_n q_{n-1} + q_{n-2}.
\end{align*}

\begin{exercise}
Compute $p_n$ and $q_n$ for the continued fractions
$[-3,1,1,1,1,3]$ and $[0,2,4,1,8,2]$.  Observe that the
propositions below hold.
\end{exercise}


\begin{proposition}
$\ds [a_0, \ldots, a_n] = \frac{p_n}{q_n}$
\end{proposition}
\begin{proof}
We use induction.  We already verified the assertion when $n=0,1$.
Suppose the proposition is true for all continued fractions of
length $n-1$.  Then
\begin{align*}
[a_0,\ldots, a_n]
 &= [a_0,\ldots,a_{n-2}, a_{n-1} + \frac{1}{a_n}]\\
 &= \frac{\left( a_{n-1} + \frac{1}{a_n}\right) p_{n-2} + p_{n-3}}
         {\left( a_{n-1} + \frac{1}{a_n}\right) q_{n-2} + q_{n-3}}\\
 &= \frac{(a_{n-1}a_n +1)p_{n-2} + a_n p_{n-3}}
         {(a_{n-1}a_n +1)q_{n-2} + a_n q_{n-3}}\\
 &= \frac{a_n(a_{n-1}p_{n-2} + p_{n-3}) + p_{n-2}}
         {a_n(a_{n-1}q_{n-2} + q_{n-3}) + q_{n-2}}\\
 &= \frac{a_n p_{n-1} + p_{n-2}}{a_n q_{n-1} + q_{n-2}} = \frac{p_n}{q_n}.
\end{align*}
\end{proof}

\begin{proposition}\label{prop:dets}
For $n\leq m$,
\begin{enumerate}
\item 
the determinant of $\mtwo{p_n}{p_{n-1}}{q_n}{q_{n-1}}$ is 
$(-1)^{n-1}$; equivalently, 
$$\frac{p_n}{q_n} - \frac{p_{n-1}}{q_{n-1}} = 
                  (-1)^{n-1}\cdot\frac{1}{q_n q_{n-1}};$$
\item 
the determinant of $\mtwo{p_n}{p_{n-2}}{q_n}{q_{n-2}}$ is 
$(-1)^{n}a_n$; equivalently, 
$$\frac{p_n}{q_n} - \frac{p_{n-2}}{q_{n-2}} = 
                  (-1)^{n}\cdot\frac{a_n}{q_n q_{n-2}}.$$
\end{enumerate}
\end{proposition}
\begin{proof}
For the first statement, we proceed by induction.  The case $n=0$
holds because the determinant of $\mtwo{a_0}{1}{1}{0}$ is $-1=(-1)^{-1}$.
Suppose the statement is true for $n-1$.  Then 
\begin{align*}
p_{n}q_{n-1} - q_n p_{n-1} &= 
     (a_n p_{n-1} + p_{n-2}) q_{n-1} - (a_n q_{n-1} + q_{n-2}) p_{n-1}\\
  &= p_{n-2}q_{n-1} - q_{n-2} p_{m-1} \\
   &= 
       -(p_{n-1}q_{n-2} - p_{n-2} q_{n-1})\\
  &= -(-1)^{n-2} = (-1)^{n-1}.
\end{align*}
This completes the proof of the first statement.  For the second statement,
\begin{align*}
p_n q_{n-2} - p_{n-2} q_n &=  
        (a_n p_{n-1} + p_{n-2})q_{n-2} - p_{n-2}(a_n q_{n-1} + q_{n-2}) \\
       &= a_n(p_{n-1}q_{n-2} - p_{n-2}q_{n-1}) \\
       &= (-1)^n a_n.
\end{align*}
\end{proof}
                         

\begin{corollary}
The fraction $\ds \frac{p_n}{q_n}$ is in lowest terms.
\end{corollary}
\begin{proof}
If $p\mid p_n$ and $p\mid q_n$ then $p\mid (-1)^{n-1}$.
\end{proof}

\subsubsection{How the Convergents Converge}
Let $[a_0,\ldots, a_m]$ be a continued fraction and for
$n\leq m$ let 
$$
  c_n = [a_0, \ldots, a_n] = \frac{p_n}{q_n}
$$
denote the $n$th convergent.

\begin{proposition}
The even convergents $c_{2n}$ increase strictly with~$n$, and the
odd convergents $c_{2n+1}$ decrease strictly with~$n$.  Moreover,
the odd convergents $c_{2n+1}$ are greater than all of the
even convergents. 
\end{proposition}
\begin{proof}
For $n\geq 1$ the $a_n$ are positive, so the $q_n$ are all positive.
By Proposition~\ref{prop:dets}, for $n\geq 2$, 
$$c_n - c_{n-2} = (-1)^n \cdot \frac{a_n}{q_n q_{n-2} },$$
which proves the first claim.

Next, Proposition~\ref{prop:dets} implies that for $n\geq 1$,
$$
  c_n - c_{n-1} = (-1)^{n-1}\cdot \frac{1}{q_n q_{n-1}}
$$
has the sign of $(-1)^{n-1}$, so that
$c_{2n+1} > c_{2n}$.
Thus if there exists $r, n$ such that
$c_{2n+1} < c_{2r}$, then $r\neq n$.  
If $r<n$, then $c_{2n+1} < c_{2r} < c_{2n}$, a contradiction.  
If $r>n$, then $c_{2r+1} < c_{2n+1} < c_{2r}$, also a contradiction.
\end{proof}

\subsection{Every Rational Number is Represented}
\begin{proposition}
Every rational number is represented by
a continued fraction.
\end{proposition}
\begin{proof}
Let $a/b$, where $b>0$, be any rational number.  Euclid's
algorithm gives:
\begin{align*}
a &= b\cdot a_0 + r_1, & 0<r_1<b\\
b &= r_1\cdot a_1 + r_2, & 0<r_2<r_1\\
 &\cdots &\\
r_{n-2} &= r_{n-1}\cdot a_{n-1} + r_n, & 0<r_n < r_{n-1}\\
r_{n-1} &= r_n\cdot a_n + 0.
\end{align*}
Note that $a_i>0$ for $i>0$.
Rewrite the equations as follows:
\begin{align*}
a/b &= a_0 + r_1/b = a_0 + 1/(b/r_1),\\
b/r_1 &= a_1 + r_2 / r_1 = a_1 + 1/(r_1/r_2),\\
r_1/r_2 &= a_2 + r_3 / r_2 = a_2 + 1/(r_2/r_3),\\
\cdots\\
r_{n-1}/r_n &= a_n.
\end{align*}
It follows that 
$$
   \frac{a}{b} = [a_0,a_1,\ldots a_n].
$$


\end{proof}






%list of good links:
%http://mathforum.org/electronic.newsletter/mf.intnews2.44.html


\section{Infinite Continued Fractions}

\subsection{The Continued Fraction Algorithm}
Let $x\in\R$ and write
  $$x = a_0 + t_0$$
with $a_0\in\Z$ and $0\leq t_0 < 1$.  If $t_0\neq 0$, write
$$\frac{1}{t_0} = a_1 + t_1$$ with $a_1\in\N$ and $0\leq t_1 < 1$.
Thus $t_0 = \frac{1}{a_1 + t_1}=[0,a_1+t_1]$, which is a (nonintegral)
continued fraction expansion of $t_0$.  Continue in this manner
so long as $t_n\neq 0$ writing
$$\frac{1}{t_n} = a_{n+1} + t_{n+1}$$
with $a_{n+1}\in\N$ and $0\leq t_{n+1}<1$.
This process, which associates to a real number~$x$ the sequence
of integers $a_0, a_1, a_2, \ldots $, is called the
{\em continued fraction algorithm}.  

\begin{example}
Let $x=\frac{8}{3}$.  Then $x=2+\frac{2}{3}$, so 
$a_0=2$ and $t_0=\frac{2}{3}$.  Then
$\frac{1}{t_0}=\frac{3}{2} = 1+\frac{1}{2}$, so $a_1=1$ and $t_1=\frac{1}{2}$.
Then $\frac{1}{t_1} = 2$, so $a_2=2$, $t_2=0$, and the sequence terminates.
Notice that 
$$ \frac{8}{3} = [2,1,2],$$
so the continued fraction algorithm produces the
continued fraction of $\frac{8}{3}$.
\end{example}


\begin{proposition}\label{prop1}
For every $n$ such that $a_n$ is defined, we have
$$x = [a_0, a_1, \ldots, a_{n}+t_n],$$
and if $t_{n}\neq 0$ then
$
  x = [a_0, a_1, \ldots, a_{n}, \frac{1}{t_n}].
$
\end{proposition}

\begin{proof}
Use induction.  The statements are both true when $n=0$.
If the second statement is true for $n-1$, then
$$
x = [a_0,a_1, \ldots, a_{n-1},\frac{1}{t_{n-1}}]
  =[a_0,a_1, \ldots, a_{n-1},a_n + t_n]
  =[a_0,a_1, \ldots, a_{n-1},a_n, \frac{1}{t_n}].$$
Similarly, the first statement is true for~$n$ if 
it is true for $n-1$.

\end{proof}



\begin{example}
Let $x = \frac{1+\sqrt{5}}{2}.$  Then 
$$
   x = 1 + \frac{-1 + \sqrt{5}}{2},
$$
so $a_0 = 1$ and $t_0 = \frac{-1+\sqrt{5}}{2}$.
We have
$$
  \frac{1}{t_0} = \frac{2}{-1+\sqrt{5}} = \frac{-2-2\sqrt{5}}{-4}
        = \frac{1+\sqrt{5}}{2}
$$
so again $a_1 = 1$ and $t_1 =  \frac{-1+\sqrt{5}}{2}$.
Likewise, $a_n = 1$ for all~$n$.
Does the following crazy-looking equality makes sense??
$$\frac{1+\sqrt{5}}{2} = 
1 + \frac{1}{1 + \frac{1}{1 + \frac{1}{1 + \frac{1}{1 + \frac{1}{1 + \cdots}}}}}
$$
\end{example}

\begin{example}
Next suppose $x = e$.  Then
$$a_0, a_1, a_2, \ldots = 2,1,2,1,1,4,1,1,6,1,1,8,1,1,\ldots$$
\begin{verbatim}
? contfrac(exp(1))
%1 = [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 
       12, 1, 1, 14, 1, 1, 16, 1, 1, 18, 1, 1, 20, 2]
? \\ to get more terms, increase the real precision:
? \p60
? contfrac(exp(1),[])
%12 = [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 
   12, 1, 1, 14, 1, 1, 16, 1, 1, 18, 1, 1, 20, 1, 1, 22, 1, 
   1, 24, 1, 1, 26, 1, 1, 28, 1, 1, 30, 1, 1, 32, 1, 1, 34, 
   1, 1, 36, 1, 1, 38, 1, 1, 40, 1, 1, 42, 2]

\end{verbatim}
The following program uses a proposition we proved yesterday to compute the partial convergents
of a continued fraction:
\begin{verbatim}
{convergents(v)=
    local(pp,qq,p,q,tp,tq,answer);
    pp=1; qq=0; p=v[1]; q=1;    \\ pp is p_{n-1} and p is p_n.
    answer = vector(length(v)); \\ put answer in this vector
    answer[1] = p/q;
    for(n=2,length(v),
       tp=p; tq=q; p=v[n]*p+pp; q=v[n]*q+qq; pp=tp; qq=tq;
       answer[n] = p/q;
    );
    return(answer);
}
\end{verbatim}

Let's try this with $\pi$:
\begin{verbatim}
? contfrac(Pi)
%26 = [3, 7, 15, 1, 292, 1, 1, ...]
? convergents([3,7,15])
%27 = [3, 22/7, 333/106]
? convergents([3,7,15,1,292])
%28 = [3, 22/7, 333/106, 355/113, 103993/33102]
? %[5]*1.0
%29 = 3.1415926530119026040...
? % - Pi
%30 = -0.000000000577890634...
\end{verbatim}
\end{example}


\subsection{Infinite Continued Fractions}
\begin{theorem}
Let $a_0, a_1, a_2, \ldots $ be a sequence of integers such that $a_n > 0$ for 
all $n\geq 1$,
and for each $n\geq 0$, set
$c_n = [a_0, a_1, \ldots a_n].$
Then $\ds\lim_{n\ra \infty} c_n$ exists.
\end{theorem}
\begin{proof}
For any $m\geq n$, the number $c_n$ is a partial convergent of $[a_0, \ldots, a_m]$.
Recall from the previous lecture that the even convergents $c_{2n}$ form a strictly
{\em increasing} sequence and the odd convergents $c_{2n+1}$ form a strictly
{\em decreasing} sequence. Moreover, the even convergents are all $\leq c_1$ 
and the odd convergents are all $\geq c_0$.
Hence $\alpha_0 = \lim_{n\ra \infty} c_{2n}$ and $\alpha_1 = \lim_{n\ra \infty} c_{2n+1}$
both exist and $\alpha_0\leq \alpha_1$.
Finally, by a proposition from last time
$$| c_{2n} - c_{2n-1}|
   = \frac{1}{q_{2n}\cdot q_{2n-1}} \leq
   \frac{1}{2n(2n-1)} \ra 0,
$$
so $\alpha_0 = \alpha_1$.
\end{proof}

We define
$$
  [a_0, a_1, \ldots ] = \lim_{n\ra \infty} c_n.
$$

\begin{example}
We use PARI to illustrate the convergence of the theorem for $x=\pi$.
\begin{verbatim}
? a = contfrac(Pi)
%38 = [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, ...]
? c = convergents(a)
%39 = [3, 22/7, 333/106, 355/113, 103993/33102, 104348/33215, ...]
? \p9                      \\ so we can see.
   realprecision = 9 significant digits
? [c[1]*1.0, c[3]*1.0, c[5]*1.0, c[7]*1.0]  \\ odd ones converge up to pi
%43 = [3.00000000, 3.14150943, 3.14159265, 3.14159265]
? [c[2]*1.0,c[4]*1.0,c[6]*1.0,c[8]*1.0]     \\ even ones swoop down on pi.
%44 = [3.14285714, 3.14159291, 3.14159265, 3.14159265]
\end{verbatim}
\end{example}

\begin{theorem}
Let $x\in\R$ be a real number.  Then
$$
  x = [a_0, a_1, a_2, \ldots],
$$
where $a_0, a_1, a_2,\ldots$ is the sequence produced by the continued fraction algorithm.
\end{theorem}
\begin{proof}
If the sequence is finite then some $t_n=0$ and the
result follows by Proposition~\ref{prop1}.
Suppose the sequence is infinite. 
By Proposition~\ref{prop1},
$$
  x = [a_0, a_1, \ldots, a_n, \frac{1}{t_n}].
$$
By a proposition from the last lecture\footnote{Which we apply in a case when
the partial quotients of the continued fraction are not integers!},
$$
  x = \frac{\frac{1}{t_n} p_n + p_{n-1}}{\frac{1}{t_n} q_n + q_{n-1}}.
$$
Thus if $c_n = [a_0, a_1, \ldots, a_n]$, then
\begin{align*}
x - c_n &= x - \frac{p_n}{q_n}\\
      &=\frac{\frac{1}{t_n} p_n q_n + p_{n-1} q_n - \frac{1}{t_n} p_n q_n - p_n q_{n-1}}
        {q_n \left(\frac{1}{t_n} q_n + q_{n-1}\right)}.\\
      &= \frac{p_{n-1} q_n - p_{n}q_{n-1}}{q_n\left(\frac{1}{t_n} q_n + q_{n-1}\right)} \\
      &= \frac{(-1)^n}{q_n\left(\frac{1}{t_n} q_n + q_{n-1}\right)}.
\end{align*}
Thus 
\begin{align*}
 |x - c_n| &= \frac{1}{q_n\left(\frac{1}{t_n} q_n + q_{n-1}\right)} \\
           &< \frac{1}{q_n(a_{n+1} q_n + q_{n-1})} \\
           &= \frac{1}{q_n \cdot q_{n+1}} \leq \frac{1}{n(n+1)} \ra 0.
\end{align*}
(In the inequality we use that $a_{n+1}$ is the integer part of $\frac{1}{t_n}$, and
is hence $\leq \frac{1}{t_n}$.)
 
\end{proof}





\begin{proposition}\label{prop}
If~$x$ is a rational number then the sequence 
$a_0, a_1, a_2, \ldots $
terminates (at~$n$ say) and 
$$x = [a_0, a_1, a_2, \ldots, a_n].$$
\end{proposition}
\begin{proof}
Let $[b_0,b_1,\ldots, b_m]$ be the continued fraction representation
of~$x$ that we obtain using the Euclidean algorithm.  Then 
$$
  x = b_0 + 1/[b_1,\ldots,b_m].
$$  
If $[b_1,\ldots,b_m]=1$ then $m=1$ and $b_1=1$, which would never
happen using the Euclidean algorithm since $x$ is expressed in lowest
terms.  Thus $[b_1,\ldots,b_m]>1$, so in the continued fraction
algorithm we choose $a_0 = b_0$ and $t_0 = 1/[b_1, \ldots, b_m]$.
Repeating this argument enough times proves the claim.
\end{proof}



\section{Quadratic Irrationals}



In this lecture we prove that the continued fraction expansion of
a number is periodic if and only if the number is a quadratic irrational.

\subsection{Quadratic Irrationals}
\begin{definition}
An element $\alpha\in\R$ is a {\em quadratic irrational} 
if it is irrational and satisfies a quadratic polynomial.
\end{definition}
Thus, e.g., $(1+\sqrt{5})/2$ is a quadratic irrational.
Recall that
$$
  \frac{1+\sqrt{5}}{2} = [1,1,1,\ldots].
$$
The continued fraction of $\sqrt{2}$ is $[1,2,2,2,2,2,\ldots]$, and
the continued fraction of $\sqrt{389}$ is
$$
   [19,1,2,1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38,\ldots].$$
Does the $[1,2,1, 1, 1, 1, 2, 1, 38]$ pattern repeat over and over again??

\subsection{Periodic Continued Fractions}
\begin{definition}
A {\em periodic continued fraction} is a continued
fraction $[a_0, a_1, \ldots, a_n, \ldots]$ such that
$$
  a_n = a_{n+h}
$$
for a fixed positive integer~$h$ and all sufficiently large~$n$.
We call~$h$ the {\em period} of the continued fraction.
\end{definition}

\begin{example}
Consider the periodic continued fraction $[1,2,1,2,\ldots] = [\overline{1,2}]$.
 What does it converge to?
$$[\overline{1,2}] = 1+\frac{1}{2+\frac{1}{1+\frac{1}{2+ \frac{1}{1+\cdots}}}},$$
so if $\alpha=[\overline{1,2}]$ then 
$$
  \alpha = 1 + \frac{1}{2+\alpha}.
$$
Thus $2\alpha + \alpha^2 = 2 + \alpha + 1$, so 
$$
   \alpha^2 + \alpha - 3 = 0
\quad\text{ and }\quad \alpha = \frac{-1+\sqrt{7}}{2}.
$$
\end{example}

\begin{theorem}
An infinite integral continued fraction is periodic if and only if
it represents a quadratic irrational.
\end{theorem}
\begin{proof}
\par\noindent($\Longrightarrow$) First suppose that 
$$[a_0, a_1, \ldots, a_n, \overline{a_{n+1},\ldots, a_{n+h}}]$$
is a periodic continued fraction.  Set
 $\alpha=[a_{n+1},a_{n+2}, \ldots]$.  Then
$$
  \alpha = [a_{n+1},\ldots, a_{n+h}, \alpha],
$$
so 
$$
  \alpha = \frac{\alpha p_{n+h} + p_{n+h-1}}{\alpha q_{n+h} + q_{n+h-1}}.
$$
(We use that~$\alpha$ is the last partial convergent.)
Thus $\alpha$ satisfies a quadratic equation.  Since the $a_i$ are
all integers, the number
\begin{align*}
 [a_0, a_1, \ldots ] &= [a_0, a_1, \ldots, a_n, \alpha]\\
     &= a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \cdots + \alpha}}
\end{align*}
can be expressed as a polynomial in~$\alpha$ with
rational coefficients, so
$[a_0, a_1, \ldots]$ also satisfies a quadratic polynomial.
Finally, $\alpha\not\in\Q$ because periodic continued fractions
have infinitely many terms.
\vspace{2ex}

\par\noindent($\Longleftarrow$) 
This direction was first proved by Lagrange.  The proof
is much more exciting!
Suppose $\alpha\in\R$ satisfies a quadratic equation
$$a \alpha^2 + b\alpha + c = 0$$
with $a, b, c\in\Z$.
Let $[a_0, a_1, \ldots]$
be the expansion of~$\alpha$.  For each~$n$, let
$$
  r_n = [a_n, a_{n+1}, \ldots],
$$
so that 
$$
   \alpha = [a_0, a_1, \ldots, a_{n-1}, r_n].
$$
We have
$$
  \alpha = \frac{r_n p_n + p_{n-1}}{r_n q_n + q_{n-1}}.
$$
Substituting this expression for~$\alpha$ into the quadratic equation
for $\alpha$, we see that
$$A_n r_n^2 + B_n r_n + C_n = 0,$$
where
\begin{align*}
 A_n &= a p_{n-1}^2 + b p_{n-1} q_{n-1} + c q_{n-1}^2,\\
 B_n &= 2a p_{n-1} p_{n-2} + b(p_{n-1} q_{n-2} + p_{n-2} q_{n-1}) + 2c q_{n-1} q_{n-2},\\
 C_n &= a p_{n-2}^2 + b p_{n-2} q_{n-2} + c p_{n-2}^2.
\end{align*}
Note that $A_n, B_n, C_n\in\Z$, that $C_n = A_{n-1}$, and that
$$
  B^2 - 4A_n C_n = (b^2- 4ac)(p_{n-1}q_{n-2} - q_{n-1}p_{n-2})^2 = b^2 - 4ac.
$$

Recall from the proof of Theorem~2.3 of the previous lecture that
$$
  \left| \alpha - \frac{p_{n-1}}{q_{n-1}}\right|
  < \frac{1}{q_n q_{n-1}}.
$$
Thus
$$
  \left| \alpha q_{n-1} - p_{n-1}\right| < \frac{1}{q_n} < \frac{1}{q_{n+1}},
$$
so 
$$
  p_{n-1} = \alpha q_{n-1} + \frac{\delta}{q_{n-1}}
\qquad\text{with }|\delta| < 1.
$$
Hence
\begin{align*}
  A_n &= a\left(\alpha q_{n-1} + \frac{\delta}{q_{n-1}}\right)^2
        +b\left(\alpha q_{n-1} + \frac{\delta}{q_{n-1}}\right)q_{n-1}
        +c q_{n-1}^2\\
      &= (a\alpha^2 + b\alpha + c)q_{n-1}^2 + 2a\alpha\delta + 
           a\frac{\delta^2}{q_{n-1}^2} + b\delta\\
      &= 2a\alpha\delta + a\frac{\delta^2}{q_{n-1}^2} + b\delta.
\end{align*}
Thus 
$$
  |A_n| = \left|2a\alpha\delta + a\frac{\delta^2}{q_{n-1}^2} + b\delta\right|
       < 2|a\alpha| + |a| + |b|.
$$
Thus there are only finitely many possibilities for the integer $A_n$.
Also, 
$$
  |C_n| = |A_{n-1}|
  \quad\text{ and }\quad
  |B_n| = \sqrt{b^2 - 4(ac-A_n C_n)},
$$
so there are only finitely many triples $(A_n, B_n, C_n)$,
and hence only finitely many possibilities for $r_n$ as~$n$ 
varies.  Thus for some~$h>0$, 
$$
   r_n = r_{n+h}.
$$
This shows that the continued fraction for~$\alpha$ is periodic.
\end{proof}

\subsection{What About Higher Degree?}
\begin{definition}
An {\em algebraic number} is a root of a polynomial $f\in\Q[x]$.
\end{definition}

\hd{Open Problem:}\footnote{As far as I know this is still 
an open problem.} 
What is the continued fraction expansion of the algebraic number $\sqrt[3]{2}$?
\begin{verbatim}
? contfrac(2^(1/3))
%5 = [1, 3, 1, 5, 1, 1, 4, 1, 1, 8, 1, 14, 1, 10, 2, 1, 4, 12, 2, 3,
 2, 1, 3, 4, 1, 1, 2, 14, 3, 12, 1, 15, 3, 1, 4, 534, 1, 1, 5, 1, 1, 
 121, 1, 2, 2, 4, 10, 3, 2, 2, 41, 1, 1, 1, 3, 7, 2, 2, 9, 4, 1, 3, 7, 
 6, 1, 1, 2, 2, 9, 3, 1, 1, 69, 4, 4, 5, 12, 1, 1, 5, 15, 1, 4, 1, 1, 
 1, 1, 1, 89, 1, 22, 186, 5, 2, 4, 3, 3, 1, \ldots]
\end{verbatim}
I sure don't see a pattern, and that $534$ strips me of any confidence
that I ever will.  One could at least try to analyze the first few terms
of the continued fraction statistically (see Lang and Trotter, 1972).  

\vspace{2ex}
\hd{Khintchine (1963), page 59:}
\begin{quote}
No properties of the representing continued fractions,
analogous to those which have just been proved, are known
for algebraic numbers of higher degree.  [...] It is of
interest to point out that up till the present time no
continued fraction development of an algebraic
number of higher degree than the second is known.
It is not even known if such a development  has bounded
elements.  Generally speaking the problems associated with
the continued fraction expansion of algebraic numbers of degree
higher than the second are extremely difficult and virtually
unstudied.
\end{quote}

\vspace{1ex}
\hd{Richard Guy {\em Unsolved Problems in Number Theory} (1994), page 260:}
\begin{quote}
Is there an algebraic number of degree greater than two whose simple
continued fraction has unbounded partial quotients?
Does {\em every} such number have unbounded partial quotients?
\end{quote}




\section{Applications}



In this lecture we will learn about two applications of continued
fractions.  The first is a solution to the computational
problem of recognizing a rational number using a computer.  The second
application is to the following ancient question: Given a positive nonsquare
integer $d$, find {\em integers}~$x$ and~$y$ such that $x^2 - dy^2 = 1$.

\subsection{Recognizing Rational Numbers}
Suppose that you can compute approximations to a rational number using a
computer, and desparately want to know what the rational number is.
As Henri Cohen explains in his book {\em A Course in Computational
Algebraic Number Theory}, continued fraction are very helpful.  

\begin{quote}
Consider the following apparently simple problem.  Let $x\in\R$ be
given by an approximation (for example a decimal or binary one).
Decide if $x$ is a rational number or not.  Of course, this question
as posed does not really make sense, since an approximation is usually
itself a rational number.  In practice however the question does make
a lot of sense in many different contexts, and we can make it
algorithmically more precise.  For example, assume that one has an
algorithm which allows us to compute~$x$ to as many decimal places as
one likes (this is usually the case).  Then, if one claims that~$x$ is
(approximately) equal to a rational number $p/q$, this means that
$p/q$ should still be extremely close to~$x$ whatever the number of
decimals asked for, $p$ and $q$ being fixed.  This is still not
completely rigorous, but it comes quite close to actual practice, so
we will be content with this notion.

Now how does one find $p$ and $q$ if $x$ is indeed a rational number?
The standard (and algorithmically excellent) answer is to compute the
continued fraction expansion $[a_0, a_1, \ldots ]$ of~$x$.
The number~$x$ is rational if and only if its continued fraction expansion
is finite, i.e., if and only if one of the $a_i$ is {\em infinite}.
Since~$x$ is only given with the finite precision, $x$ we be considered
rational if~$x$ has a {\em very} large partial quotient $a_i$ in its
continued fraction expansion.
\end{quote}

The following example illustrates Cohen's remarks:
\begin{example}
\begin{verbatim}
? x
%13 = 9495/3847
? x*1.0
%14 = 2.4681570054587990642058747075643358461138549519105
? contfrac(x)
%15 = [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 2]
? contfrac(2.468157005458799064)
%16 = [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 328210621945, 2, 1, 1, 1, 1, 7]
? contfracpnqn([2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1])
%17 =
[9495 5852]
[3847 2371]
? contfrac(2.4681570054587990642058747075643)
%18 = [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 1885009518355562936415046, 1, 4]
? \p300
? x*1.0  \\ notice that no repeat is immediately evident in the digits of x
%19 = 2.468157005458799064205874707564335846113854951910579672472056147647517..
? \\ in fact, the length of the period of the decimal expansion 
  \\ of 1/3847 is 3846 (the order of 10 modulo 3847)!!
\end{verbatim}

\end{example}

\subsection{Pell's Equation}
In February of 1657, Pierre Fermat issued the following challenge:
\begin{quote}
Given a positive integer~$d$, find a positive integer~$y$ such that
$dy^2 + 1$ is a perfect square.
\end{quote}
In other words, find a solution to $x^2-dy^2 = 1$ with $y\in\N$.


Note Fermat's emphasis on {\em integer} solutions.  It is
easy to find rational solutions to the equation $x^2-dy^2 = 1$.
Simply divide the relation
$$
  (r^2+d)^2 - d(2r)^2 = (r^2-d)^2
$$
by $(r^2 - d)^2$ to arrive at
$$
  x = \frac{r^2+d}{r^2-d}, \qquad
  y = \frac{2r}{r^2-d}.
$$
Fermat said: ``Solutions in fractions, which can be given at
once from the merest elements of arithmetic, do not satisfy me.''

The equation $x^2 - dy^2=1$ is called {\bf Pell's equation}.  This is
because Euler (in about 1759) accidently called it ``Pell's equation''
and the name stuck, though Pell (1611--1685) had nothing 
to do with it.

If~$d$ is a perfect square, $d=n^2$, then 
$$
   (x+ny)(x-ny) = x^2-dy^2 = 1
$$
which implies that
$x+ny = x-ny = 1$, so
$$
  x = \frac{x+ny+x-ny}{2} = \frac{1+1}{2}=1.
$$
We will thus always assume that~$d$ is not a perfect square.
You can read about Pell's equation in Section 0.6 of Kato-Kurokawa-Saito 
and on pages 107--111 of Davenport.  Pell's equation is 
best understood in terms of units in real quadratic fields.

\subsection{Units in Real Quadratic Fields}
Let $d$ be a nonsquare positive integer, and set
\begin{align*}
  \Q(\sqrt{d}) &= \{a + b\sqrt{d} : a, b\in\Q\} \\
  \Z[\sqrt{d}] &= \{a + b\sqrt{d} : a, b\in\Z\}.
\end{align*}
Then $\Q(\sqrt{d})$ is a {\em real quadratic field} and
$\Z[\sqrt{d}]$ is a ring. 
There is a homomorphism called norm:
$$
   N : \Q(\sqrt{d})^* \ra \Q^*,
 \qquad
   N\left(a+b\sqrt{d}\right) = \left(a+b\sqrt{d}\right)\left(a-b\sqrt{d}\right) = a^2 - b^2 d.
$$
\begin{definition}
An element $x\in R$ is a {\em unit} if there exists $y\in{}R$
such that $xy=1$.
\end{definition}

\begin{proposition}
The units of $\Z[\sqrt{d}]$ are exactly the elements of 
norm~$\pm 1$ in $\Z[\sqrt{d}]$. 
\end{proposition}
\begin{proof}
Suppose $u\in\Z[\sqrt{d}]$ is a unit.  Then
$$
  1 = N(1) = N(u u^{-1}) = N(u)\cdot N(u^{-1}).
$$
Since $N(u), N(u^{-1})\in\Z$, we have $N(u) = N(u^{-1}) = \pm 1$ 
\end{proof}

Thus Fermat's challenge amounts to determing the group $U^+$ of
units in $\Z[\sqrt{d}]$ of the form $a+b\sqrt{d}$ with $a, b \geq 0$.
\begin{theorem}\label{thm:pell}
The group $U^+$ is an infinite cyclic group.  
It is generated by $p_m+ q_m\sqrt{d}$, where
$\frac{p_m}{q_m}$ is one of the partial convergents of the 
continued fraction expansion of $\sqrt{d}$.
(In fact, if~$m$ is the period of the continued fraction of $\sqrt{d}$ then
$n=m-1$ when~$m$ is even and $2n-1$ when $m$ is odd.)
\end{theorem}

The theorem implies that {\em Pell's equation always has a solution}!
Warning: the smallest solution is typically shockingly large.
For example, the value of~$x$ in the smallest solution to $x^2 -
1000099y^2 = 1$ has {\bf 1118 digits}.

The following example illustrates how to use Theorem~\ref{thm:pell} to
solve Pell's equation when $d=61$, where the simplest solution is
already quite large.
\begin{example}
Suppose $d=61$.  Then
$$\sqrt{d} = [7, \overline{1, 4, 3, 1, 2, 2, 1, 3, 4, 1, 14}],$$
which has odd period $n=11$. Thus the group $U^+$ is generated by
\begin{align*}
  x &= p_{21} = 1766319049\\
  y &= q_{21} =  226153980.
\end{align*}
That is, we have
$$U^+ = \langle u \rangle = \langle 1766319049 + 226153980\sqrt{61}\rangle,$$
and $x = 1766319049$, $y=226153980$ gives a solution to
$x^2 - d y^2 = 1$.  All the other solutions arise from $u^n$ for
some~$n$.  For example, 
  $$u^2 = 6239765965720528801 + 798920165762330040\sqrt{61}$$
leads to another solution.
\end{example}

\begin{remark}
To help with your homework, note that if the equation
$$
  x^2 - dy^2 = n
$$
has at least one (nonzero) solution $(x_0, y_0)\in\Z\cross\Z$, then it
must have infinitely many solutions.  This is because if 
$x_0^2 - dy_0^2 =n$ and~$u$ is a generator of the 
cyclic group $U^+$, then for any integer~$i$, 
$$
  N(u^i(x_0+ y_0\sqrt{d})) = N(u^i)\cdot N(x_0+y_0\sqrt{d}) = 1\cdot n = n,
$$
so 
$$
  x_1 + y_1\sqrt{d} = u^i(x_0 + y_0\sqrt{d})
$$
provides another solution to $x^2 + d y^2 = n$.
\end{remark}


\subsection{Some Proofs}
The rest of this lecture is devoted to proving most of
Theorem~\ref{thm:pell}.  We will prove that partial convergents to
continued fractions contribute infinitely many solutions to Pell's
equation.  We will not prove that every solution to Pell's equation is
a partial convergent, though this is true.\footnote{There is a
complete proof in Section 13.5 of Burton's {\em Elementary Number
Theory}.  It just involves more of the same sort of computations that we've
been doing with continued fractions.}

Fix a positive nonsquare integer~$d$.

\begin{definition}
A quadratic irrational $\alpha=a+b\sqrt{d}$ is {\em reduced} if 
$\alpha>1$ and if the conjugate of~$\alpha$, denoted by~$\alpha'$,
satisfies $-1<\alpha'<0$.
\end{definition}
For example, the number $\alpha=1+\sqrt{2}$ is reduced.

\begin{definition}
A continued fraction is {\em purely periodic}
if it is of the form $[\overline{a_0,a_1,\ldots,a_n}]$.
\end{definition}
The continued fraction $[\overline{2}]$ of $1+\sqrt{2}$ is 
purely periodic.

\begin{lemma}\label{lem:pure}
If~$\alpha$ is a reduced quadratic irrational, then the continued
fraction expansion of~$\alpha$ is purely periodic.  (The converse
is also true, and is easy to prove.)
\end{lemma}
\begin{proof}
The proof can be found on pages 102--103 of Davenport's book.
\end{proof}

\begin{lemma}\label{lemma:2a0}
The continued fraction expansion of $\sqrt{d}$ is of the form
 $$[a_0, \overline{a_1, \ldots, a_{n-1}, 2 a_0}].$$
\end{lemma}
\begin{proof}
Let $a_0$ be the floor of $\sqrt{d}$.  
Then $\alpha=\sqrt{d} + a_0$ is reduced because $\alpha>1$
and $\alpha'=-\sqrt{d}+a_0$ satisfies $-1<\alpha'<0$.
Let $[a_0, a_1, a_2, \ldots ]$ be the continued fraction
expansion of $\sqrt{d}$.  Then the continued
fraction expansion of $\sqrt{d}+a_0$ is 
$[2a_0, a_1, a_2, \ldots]$.
By Lemma~\ref{lem:pure}, the continued fraction expansion of 
$\sqrt{d}+a_0$ is purely periodic, so
$$[2a_0, a_1, a_2, \ldots] = [\overline{2a_0, a_1, a_2, \ldots, a_{n-1}}],$$
where~$n$ is the period.  It follows that $a_n = 2 a_0$, as claimed.
\end{proof}

The following proposition shows that there are infinitely many
solutions to Pell's equation that arise from continued fractions.
\begin{proposition}
Let $p_k/q_k$ be the partial convergents of the continued fraction
expansion of $\sqrt{d}$, and let~$n$ be the period of the expansion
of $\sqrt{d}$.  Then
$$p_{kn-1}^2 - d q_{kn-1}^2 = (-1)^{kn}$$
for $k=1,2,3,\ldots$.
\end{proposition}
\begin{proof}\footnote{This proof is from Section~13.5 of Burton's {\em Elementary Number Theory}.}
By Lemma~\ref{lemma:2a0}, 
for $k\geq 1$, the continued fraction of $\sqrt{d}$ can be written
in the form
$$\sqrt{d} = [a_0, a_1, a_2, \ldots, a_{kn-1}, r_{kn}]$$
where
$$r_{kn} = [2a_0, \overline{a_1, a_2, \ldots, a_{n}}] = a_0 + \sqrt{d}.$$
Because $\sqrt{d}$ is the last partial convergent of the continued fraction
above, we have
$$
  \sqrt{d} = \frac{r_{kn} p_{kn-1} + p_{kn-2}}{r_{kn} q_{kn-1} + q_{kn-2}}.
$$
Upon substituting $r_{kn} = a_0 + \sqrt{d}$ and simplifying, this reduces
to 
$$\sqrt{d}(a_0 a_{kn-1} + q_{kn-2} - p_{kn-1}) 
     = a_0 p_{kn-1} + p_{kn-2} - d q_{kn-1}.$$
Because the right-hand side is rational and $\sqrt{d}$ is irrational,
$$a_0 a_{kn-1} + q_{kn-2} = p_{kn-1},
\quad\text{ and }\quad
a_0 p_{kn-1} + p_{kn-2} = d q_{kn-1}.$$
Multiplying the first of these equations by $p_{kn-1}$
and the second by $-q_{kn-1}$, and then adding them, gives
$$
  p_{kn-1}^2 - d q_{kn-1}^2 = p_{kn-1}q_{kn-2} - q_{kn-1} p_{kn-2}.$$
But 
$$
 p_{kn-1}q_{kn-2} - q_{kn-1} p_{kn-2} = (-1)^{kn-2} = (-1)^{kn},
$$
which proves the proposition.
\end{proof}


\begin{exercises}

%\item\label{ex:cf1}
% Draw a diagram that illustrates the partial convergents
%of the following continued fractions:
%\begin{enumerate}
%\item $[13,1,8,3]$
%\item $[1,1,1,1,1,1,1,1]$
%\item $[1,2,3,4,5,6,7,8]$
%\end{enumerate}


\item\label{ex:cf2}
If $c_n=p_n/q_n$ is the $n$th convergent of the continued fraction
$[a_0,a_1,\ldots,a_n]$ and $a_0>0$, show that
$$
  [a_n,a_{n-1},\ldots, a_1, a_0] = \frac{p_n}{p_{n-1}}
$$
and
$$
   [a_n,a_{n-1},\ldots, a_2, a_1] = \frac{q_n}{q_{n-1}}.
$$
(Hint: In the first case, notice that
$\frac{p_n}{p_{n-1}} = a_n + \frac{p_{n-2}}{p_{n-1}} 
          = a_n + \frac{1}{\frac{p_{n-1}}{p_{n-2}}}.$)

\item\label{ex:cf3} 
There is a function~$j(\tau)$, denoted by {\tt
ellj} in PARI, which takes as input a complex number~$\tau$ with
positive imaginary part, and returns a complex number called the
``$j$-invariant of the associated elliptic curve''.  Suppose
that~$\tau$ is {\em approximately} $-0.5+ 0.3281996289i$ and that you
know that~$j=j(\tau)$ is a rational number.  Use continued fractions
and PARI to compute a reasonable guess for the rational number
$j=\mbox{\tt ellj}(\tau)$.  (Hint: In PARI $\sqrt{-1}$ is represented
by {\tt I}.)

\item\label{ex:cf4}
 Evaluate each of the following infinite continued fractions:
\begin{enumerate}
\item $[\overline{2,3}]$
\item $[2,\overline{1,2,1}]$
\item $[0,\overline{1,2,3}]$
\end{enumerate}

\item\label{ex:cf5}
Determine the infinite continued fraction of each of the following
numbers:
\begin{enumerate}
\item $\sqrt{5}$
\item $\ds\frac{1+\sqrt{13}}{2}$
\item $\ds\frac{5+\sqrt{37}}{4}$
\end{enumerate}

\item\label{ex:cf6}
\begin{enumerate}
\item For any positive integer~$n$, prove that
$\sqrt{n^2+1} = [n,\overline{2n}].$
\item
Find a convergent to
$\sqrt{5}$ that approximates $\sqrt{5}$
to within four decimal places.
      \end{enumerate}

\item\label{ex:cf7} 
A famous theorem of 
Hurwitz (1891) says that for any irrational
number~$x$, there exists infinitely many rational numbers $a/b$
such that 
$$\left| x - \frac{a}{b}\right| < \frac{1}{\sqrt{5}b^2}.$$
Taking $x=\pi$, obtain three rational numbers that satisfy this
inequality.

\item\label{ex:cf8} 
 The continued fraction expansion of~$e$ is
$$
  [2,1,2,1,1,4,1,1,6,1,1,8,1,1,\ldots].
$$
It is a theorem that the obvious pattern continues indefinitely.  Do
you think that the continued fraction expansion of $e^2$ also exhibits
a nice pattern?  If so, what do you think it is?

\item\label{ex:cf9} 
\begin{enumerate}
\item Show that there are infinitely many even integers~$n$
with the property that both $n+1$ and $\frac{n}{2}+1$ are perfect
squares. 
\item Exhibit two such integers that are greater than $389$.
\end{enumerate}


\item\label{ex:cf10}  
A primitive Pythagorean triple is a triple $x, y, z$ of integers
such that $x^2 + y^2 = z^2$.
Prove that there exists infinitely many primitive Pythagorean
triples $x, y, z$ in which~$x$ and~$y$ are consecutive integers.

\end{exercises}

