%! TEX root = NT.tex
% vim: tw=50
% 15/11/2023 10AM

\newpage
\section{Continued Fractions}

$\alpha \in \RR$ $\to$ decimal expansion $\alpha =
\sum \frac{a_i}{10^i}$, $a_i \in \{0, 1, 2,
\ldots, 9\}$. Useful properties: if $\alpha, \beta
\in \RR$ are distinct then it's easy to decide
whether $\alpha < \beta$ or $\alpha > \beta$ if
you know their decimal expansions.

Continued fractions give another way of
representing real numbers by sequences of
integers. Useful properties: allow us to find good
rational approximations for $\alpha \in \RR$. For
example, for $\alpha = \pi$:
\begin{align*}
  \left| \pi - \frac{314159}{100000} \right|
  &< 3 \times 10^{-6} \\
  \left| \pi - \frac{355}{113} \right|
  &< 3 \times 10^{-7}
\end{align*}
The second approximation is ``better'', as it's
closer to $\pi$ and $113$ is much smaller than
$100000$. $\frac{355}{113}$ is a truncation of the
continued fraction expansion of $\pi$.

\begin{notation*}
  Suppose $a_0, \ldots, a_n \in \RR$, $a_i > 0$ if
  $i > 0$. Then
  \[ [a_0, \ldots, a_n] = a_0 + \frac{1}{a_1 +
  \frac{1}{a_2 + \frac{1}{\ddots + \frac{1}{a_n}}}} \]
  A continued fraction.
\end{notation*}
\vspace{-1em}

So $\contf[a_0, a_1] = \fcontf[a_0, a_1]$,
$\contf[a_0, a_1, a_2] = \fcontf[a_0, a_1, a_2] =
\contf[a_0, \contf[a_1, a_2]]$. In general,
$\contf[a_0, \ldots, a_n] = \contf[a_0, \ldots, a_{i -
1}, \contf[a_i, \ldots, a_n]]$ for any $1 \le i
\le n$. Continued fraction algorithm: start with
$\theta \in \RR$. Produce a sequence $a_0, a_1,
\ldots$ of integers with $a_i \ge 1$ and a
sequence $\theta = \theta_0, \theta_1, \theta_2,
\ldots$ of real numbers such that if $\theta_{n +
1}$ is defined for $n \ge 0$, then $\theta =
\contf[a_0, a_1, \ldots, a_n, \theta_{n + 1}]$.
Either the algorithm will terminate: get finite
sequence $a_0, \ldots, a_n, \theta_{n - 1} = a_n$
such that $\theta = \contf[a_0, \ldots, a_n]$.

\glsnoundefn{cfe}{continued fraction
expansion}{continued fraction expansions}
Or the
algorithm does not terminate: then sequence
$(a_i)_{i \ge 0}$ is infinite and we write finally
$\theta = \contf[a_0, a_1, a_2, \ldots]$ and call
this the continued fraction expansion of $\theta$.
We'll show later that in this case,
\[ \theta = \lim_{n \to \infty} \contf[a_0,
\ldots, a_n] .\]

\begin{flashcard}[contf-algorithm]
\glsnoundefn{cfalg}{continued fraction
algorithm}{N/A}
\prompt{Continued fraction algorithm?}
\cloze{Step 0: $\theta = \theta_0$. Set $a_0 =
\left\lfloor \theta_0 \right\rfloor$. If $a_0 =
\theta_0$ then stop. Otherwise, $0 < \theta_0 -
a_0 < 1$ $\implies$ if we set $\theta_1 =
\frac{1}{\theta_0 - a_0}$, then $\theta_1 > 1$ and
$\theta = \contf[a_0, \theta_1]$.

Step 1: set $a_1 = \left\lfloor \theta_1
\right\rfloor$. If $a_1 = \theta_1$ then stop (and
$\theta = \contf[a_0, a_1]$). Otherwise, $0 <
\theta_1 - a_1 < 1$, so if we set $\theta_2 =
\frac{1}{\theta_1 - a_1}$, then $\theta_2 > 1$ and
$\theta = \contf[a_0, \contf[a_1, \theta_2]] =
\contf[a_0, a_1, \theta_2]$.

Step $n$, $n \ge 1$: Set $a_n = \left\lfloor
\theta_n \right\rfloor \ge 1$, as $\theta_n > 1$.
If $a_n = \theta_n$ then stop (and then $\theta =
\contf [a_0, \ldots, \theta_n] = \contf[a_0,
\ldots, a_n]$). Otherwise, $0 < \theta_n - a_n <
1$, so if we set $\theta_{n + 1} =
\frac{1}{\theta_n - a_n}$, then $\theta_{n + 1} >
1$ and $\theta = \contf[a_0, \ldots, a_{n - 1},
\theta_n] = \contf[a_0, \ldots, a_{n - 1},
\contf[a_n, \theta_{n + 1}]] = \contf[a_0, \ldots,
a_n, \theta_{n + 1}]$.}
\end{flashcard}

\begin{notation*}
  \glssymboldefn{acf}{$a_i$}{$a_i$}
  $(a_i)_{i \ge 0}$ are called the partial
  quotients of $\theta \in \RR$.
\end{notation*}
\vspace{-1em}

So $\theta_1 = \frac{c_1}{c_2}$, where $c_1, c_2
\in \NN$, $c_1 > c_2$, $\gcdbrack(c_1, c_2) = 1$.
Apply Euclid's algorithm to $c_1, c_2$. Get:
\begin{align*}
  c_1
  &= d_1 c_2 + c_3
  &&c_2 > c_3 > 0  \\
  c_2
  &= d_2 c_3 + c_4
  &&c_3 > c_4 > 0  \\
  &~~~\vdots \\
  c_{n - 1}
  &= d_{n - 1} c_n + c_{n + 1}
  &&c_n > c_{n + 1} > 0  \\
  c_n
  &= d_n c_{n + 1}
  &&c_{n + 1} = 1, c_{n + 2} = 0  \\
\end{align*}

\begin{claim*}
  If $1 \le i \le n$, then $\theta_i =
  \frac{c_i}{c_{i + 1}}$. (In particular,
  \gls{cfalg} doesn't terminate before Step $n$).
\end{claim*}
\vspace{-1em}

If $i = 1$, $\theta_1 = \frac{c_1}{c_2}$. If
$\theta_i = \frac{c_i}{c_{i + 1}}$, $i < n$, then
$c_i = d_i c_{i + 1} + c_{i + 2}$. Hence
\[ \frac{c_i}{c_{i + 1}} = \theta_i = d_i +
\frac{c_{i + 2}}{c_{i + 1}}, \qquad \frac{c_{i +
2}}{c_{i + 1}} < 1 .\]
So $a_i = \left\lfloor \theta_i \right\rfloor =
d_i$, $\theta_{i + 1} = \frac{1}{\theta_i - a_i} =
\frac{c_{i + 1}}{c_{i + 2}}$. So the claim is true
by induction.

Algorithm terminates at step $n$: $\theta_n =
\frac{c_n}{c_{n + 1}} = d_n \in \ZZ$ hence
$\left\lfloor \theta_n \right\rfloor = \theta_n =
a_n$. \fakeqedhere

\begin{definition}
  Suppose $(a_i)_{i \ge 0}$ is a sequence of
  integers, $a_i \ge 1$ if $i \ge 1$. Then we
  define sequences $(p_n)_{n \ge 0}$, $(q_n)_{n
  \ge 0}$ recursively by
  \begin{align*}
    p_0
    &= a_0
    &
    p_1
    &= a_0 a_1 + 1
    &
    p_n
    &= a_n p_{n - 1} + p_{n - 2} \\
    q_0
    &= 1
    &
    q_1
    &= a_1
    &
    q_n
    &= a_n q_{n - 1} + q_{n - 2}
  \end{align*}
  for $n \ge 2$.
\end{definition}

\begin{remark*}
  \phantom{}
  \begin{enumerate}[(1)]
    \item We can define $p_{-1} = 1$, $q_{-1} =
      0$. Then the recurrence relation holds also
      for $n = 1$.
    \item We can write the recurrence relation as
      a matrix equation:
      \[
      \begin{pmatrix}
        p_n & p_{n - 1} \\
        q_n & q_{n - 1}
      \end{pmatrix}
      =
      \begin{pmatrix}
        p_{n - 1} & p_{n - 2} \\
        q_{n - 1} & q_{n - 2}
      \end{pmatrix}
      \begin{pmatrix}
        a_n & 1 \\
        1 & 0
      \end{pmatrix}
      \]
      Hence
      \[
      \begin{pmatrix}
        p_n & p_{n - 1} \\
        q_n & q_{n - 1}
      \end{pmatrix}
      =
      \begin{pmatrix}
        a_0 & 1 \\
        1 & 0
      \end{pmatrix}
      \begin{pmatrix}
        a_1 & 1 \\
        1 & 0
      \end{pmatrix}
      \cdots
      \begin{pmatrix}
        a_n & 1 \\
        1 & 0
      \end{pmatrix}
      \]
    \item The sequenc e$0 < q_1 < q_2 < q_3 <
      \cdots$ is strictly increasing, as $\acf_n
      \ge 1$ when $n \ge 1$. Hence $q_n \ge q_{n -
      1} + q_{n - 2}$ when $n \ge 1$.
      \glsnoundefn{convergent}{convergent}{convergents}
    \item If $\contf[a_0, a_1, \ldots]$ is the
      \gls{cfe} of $\theta \in \RR$, then $\left(
      \frac{p_n}{q_n} \right)_{n \ge 0}$ is called
      the sequence of convergents of $\theta$.
  \end{enumerate}
\end{remark*}

\begin{proposition}
  $(a_i)_{i \ge 0}$ sequence of integers, $a_i
  \ge 1$ if $i \ge 1$. Then:
  \begin{enumerate}[(1)]
    \item $\forall n \ge 0$, $\contf[a_0, \ldots,
      a_n] = \frac{p_n}{q_n}$.
    \item $\forall n \ge 1$, $p_n q_{n - 1} - p_{n
      - 1} q_n = (-1)^{n - 1}$, $(p_n, q_n) = 1$,
      $\frac{p_n}{q_n} - \frac{p_{n - 1}}{q_{n -
      1}} = \frac{(-1)^{n - 1}}{q_n q_{n - 1}}$.
    \item If $\beta \in \RR$, $\beta > 0$ and $n
      \ge 0$, then
      \[ \contf[a_0, \ldots, a_n, \beta] =
      \frac{\beta p_n + p_{n - 1}}{\beta q_n +
      q_{n - 1}} ,\]
      and this number lies strictly between
      $\frac{p_n}{q_n}$ and $\frac{p_{n - 1}}{q_{n
      - 1}}$.
  \end{enumerate}
  Important special case: If $\theta$ has
  \gls{cfe} $\contf[a_0, a_1, \ldots]$ then
  \[ \theta = \contf[a_0, \ldots, a_n, \theta_{n +
  1}] = \frac{\theta_{n + 1} p_n + p_{n -
  1}}{\theta_{n + 1} q_n + q_{n - 1}} .\]
\end{proposition}

\begin{proof}
  \phantom{}
  \begin{enumerate}[(1)]
    \item Follows from (3) (case $\beta = a_{n +
      1}$).
    \item Take determinants in the matrix
      expression for
      \[
      \begin{pmatrix}
        p_n & p_{n - 1} \\
        q_n & q_{n - 1}
      \end{pmatrix}
      \]
      and we deduce $p_n q_{n - 1} - q_{n - 1} q_n
      = (-1)^{n - 1}$. This shows $\gcdbrack(p_n,
      q_n) = 1$ and
      \[ \frac{p_n}{q_n} - \frac{p_{n - 1}}{q_{n -
      1}} = \frac{(-1)^{n - 1}}{q_n q_{n - 1}} .\]
  \end{enumerate}
  \renewcommand\qed{}
\end{proof}
