Partial Quotients of Continued Fraction Expansion of Irrational Square Root

Theorem
Let $n \in \Z$ such that $n$ is not a square.

Let the continued fraction expansion of $\sqrt n$ be expressed as:
 * $\left[{a_0, a_1, a_2, \ldots}\right]$

Then the partial quotients of this continued fraction expansion can be calculated as:


 * $a_r = \left\lfloor{\dfrac{\left\lfloor{\sqrt n}\right\rfloor + P_r} {Q_r} }\right\rfloor$

where:


 * $P_r = \begin{cases} 0 & : r = 0 \\

a_{r - 1} Q_{r - 1} - P_{r - 1} & : r > 0 \\ \end{cases}$


 * $Q_r = \begin{cases} 1 & : r = 0 \\

\dfrac {n - {P_r}^2} {Q_{r - 1} } & : r > 0 \\ \end{cases}$

Proof
The proof proceeds by strong induction.

For all $r \in \Z_{\ge 0}$, let $P \left({r}\right)$ be the proposition:
 * $a_r = \left\lfloor{\dfrac{\left\lfloor{\sqrt n}\right\rfloor + P_r} {Q_r} }\right\rfloor$

where:


 * $P_r = \begin{cases} 0 & : r = 0 \\

a_{r - 1} Q_{r - 1} - P_{r - 1} & : r > 0 \\ \end{cases}$


 * $Q_r = \begin{cases} 1 & : r = 0 \\

\dfrac {n - {P_r}^2} {Q_{r - 1} } & : r > 0 \\ \end{cases}$

Edge Cases
By definition of the structure of a continued fraction:
 * $\sqrt n = a_0 + \dfrac 1 {x_1}$

for some $a_0 \in \Z$ and $x_1 \in \R_{>0}$.

Thus:

Thus $P \left({0}\right)$ is seen to hold.

We have that:
 * $\sqrt n = a_0 + \dfrac 1 {x_1}$

Then:

By definition of the structure of a continued fraction:
 * $x_1 = a_1 + \dfrac 1 {x_2}$

for some $a_1 \in \Z$ and $x_2 \in \R_{>0}$.

Hence:

Thus $P \left({1}\right)$ is seen to hold.

Basis for the Induction
We have that:
 * $x_1 = a_1 + \dfrac 1 {x_2}$

Then:

By definition of the structure of a continued fraction:
 * $x_2 = a_2 + \dfrac 1 {x_3}$

for some $a_2 \in \Z$ and $x_3 \in \R_{>0}$.

Thus:

This is the basis for the induction.

Induction Hypothesis
Now it needs to be shown that, if $P \left({k - 1}\right)$ and $P \left({k}\right)$ ares true, for all $k \ge 2$, then it logically follows that $P \left({k + 1}\right)$ is true.

This is the induction hypothesis:
 * $a_k = \left\lfloor{\dfrac{a_0 + P_k} {Q_k} }\right\rfloor$

where:


 * $P_k = a_{k - 1} Q_{k - 1} - P_{k - 1}$
 * $Q_k = \dfrac {n - {P_k}^2} {Q_{k - 1} }$

and:
 * $x_k = \dfrac{\sqrt n + P_k} {Q_k}$

from which it is to be shown that:


 * $a_{k + 1} = \left\lfloor{\dfrac{a_0 + P_{k + 1} } {Q_{k + 1} } }\right\rfloor$

where:


 * $P_{k + 1} = a_k Q_k - P_k$
 * $Q_{k + 1} = \dfrac {n - {P_{k + 1} }^2} {Q_k}$

and:
 * $x_{k + 1} = \dfrac{\sqrt n + P_{k + 1} } {Q_{k + 1} }$

Induction Step
This is the induction step:

By definition of the structure of a continued fraction:
 * $x_k = a_k + \dfrac 1 {x_{k+1} }$

where:
 * $x_{k+1} \in \R_{>0}$
 * $a_k = \left\lfloor{\dfrac{a_0 + P_k} {Q_k} }\right\rfloor$

by the induction hypothesis.

Then:

By definition of the structure of a continued fraction:
 * $x_{k + 1} = a_{k + 1} + \dfrac 1 {x_{k + 2} }$

for some $a_{k + 1} \in \Z$ and $x_{k + 2} \in \R_{>0}$.

Thus:

So $P \left({k}\right) \implies P \left({k + 1}\right)$ and the result follows by the Second Principle of Mathematical Induction.

Hence the result.