# Simple continued fraction

A simple or regular continued fraction is a continued fraction with numerators all equal one, and denominators built from a sequence $\{a_{i}\}$ of integer numbers. The sequence can be finite or infinite, resulting in a finite (or terminated) continued fraction like
    $a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{\ddots +{\cfrac {1}{a_{n}}}}}}}}}$
or an infinite continued fraction like
    $a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{\ddots }}}}}}$
Typically, such a continued fraction is obtained through an iterative process of representing a number as the sum of its integer part and the reciprocal of another number, then writing this other number as the sum of its integer part and another reciprocal, and so on. In the finite case, the iteration/recursion is stopped after finitely many steps by using an integer in lieu of another continued fraction. In contrast, an infinite continued fraction is an infinite expression. In either case, all integers in the sequence, other than the first, must be positive. The integers $a_{i}$ are called the coefficients or terms of the continued fraction.
Simple continued fractions have a number of remarkable properties related to the Euclidean algorithm for integers or real numbers. Every rational number ⁠$p$/$q$⁠ has two closely related expressions as a finite continued fraction, whose coefficients a$_i$ can be determined by applying the Euclidean algorithm to $(p,q)$. The numerical value of an infinite continued fraction is irrational; it is defined from its infinite sequence of integers as the limit of a sequence of values for finite continued fractions. Each finite continued fraction of the sequence is obtained by using a finite prefix of the infinite continued fraction's defining sequence of integers. Moreover, every irrational number $\alpha$ is the value of a unique infinite regular continued fraction, whose coefficients can be found using the non-terminating version of the Euclidean algorithm applied to the incommensurable values $\alpha$ and 1. This way of expressing real numbers (rational and irrational) is called their continued fraction representation.

## Motivation and notation

Consider, for example, the rational number ⁠415/93⁠, which is around 4.4624. As a first approximation, start with 4, which is the integer part; ⁠415/93⁠ = 4 + ⁠43/93⁠. The fractional part is the reciprocal of ⁠93/43⁠ which is about 2.1628. Use the integer part, 2, as an approximation for the reciprocal to obtain a second approximation of 4 + ⁠1/2⁠ = 4.5. Now, ⁠93/43⁠ = 2 + ⁠7/43⁠; the remaining fractional part, ⁠7/43⁠, is the reciprocal of ⁠43/7⁠, and ⁠43/7⁠ is around 6.1429. Use 6 as an approximation for this to obtain 2 + ⁠1/6⁠ as an approximation for ⁠93/43⁠ and 4 + ⁠1/2 + ⁠1/6⁠⁠, about 4.4615, as the third approximation. Further, ⁠43/7⁠ = 6 + ⁠1/7⁠. Finally, the fractional part, ⁠1/7⁠, is the reciprocal of 7, so its approximation in this scheme, 7, is exact (⁠7/1⁠ = 7 + ⁠0/1⁠) and produces the exact expression $4+{\cfrac {1}{2+{\cfrac {1}{6+{\cfrac {1}{7}}}}}}$ for ⁠415/93⁠.
That expression is called the continued fraction representation of ⁠415/93⁠. This can be represented by the abbreviated notation ⁠415/93⁠ = [4; 2, 6, 7]. (It is customary to replace only the first comma by a semicolon to indicate that the preceding number is the whole part.) Some older textbooks use all commas in the (n + 1)-tuple, for example, [4, 2, 6, 7].
If the starting number is rational, then this process exactly parallels the Euclidean algorithm applied to the numerator and denominator of the number. In particular, it must terminate and produce a finite continued fraction representation of the number. The sequence of integers that occur in this representation is the sequence of successive quotients computed by the Euclidean algorithm. If the starting number is irrational, then the process continues indefinitely. This produces a sequence of approximations, all of which are rational numbers, and these converge to the starting number as a limit. This is the (infinite) continued fraction representation of the number. Examples of continued fraction representations of irrational numbers are:
* √19 = [4;2,1,3,1,2,8,2,1,3,1,2,8,...] (sequence A010124 in the OEIS). The pattern repeats indefinitely with a period of 6.
* e = [2;1,2,1,1,4,1,1,6,1,1,8,...] (sequence A003417 in the OEIS). The pattern repeats indefinitely with a period of 3 except that 2 is added to one of the terms in each cycle.
* π = [3;7,15,1,292,1,1,1,2,1,3,1,...] (sequence A001203 in the OEIS). No pattern has ever been found in this representation.
* Φ = [1;1,1,1,1,1,1,1,1,1,1,1,...] (sequence A000012 in the OEIS). The golden ratio, the irrational number that is the "most difficult" to approximate rationally (see § A property of the golden ratio φ below).
* γ = [0;1,1,2,1,2,1,4,3,13,5,1,...] (sequence A002852 in the OEIS). The Euler–Mascheroni constant, which is expected but not known to be irrational, and whose continued fraction has no apparent pattern.
Continued fractions are, in some ways, more "mathematically natural" representations of a real number than other representations such as decimal representations, and they have several desirable properties:
* The continued fraction representation for a real number is finite if and only if it is a rational number. In contrast, the decimal representation of a rational number may be finite, for example ⁠137/1600⁠ = 0.085625, or infinite with a repeating cycle, for example ⁠4/27⁠ = 0.148148148148...
* Every rational number has an essentially unique simple continued fraction representation. Each rational can be represented in exactly two ways, since [a$_0$;a$_1$,... a$_{n−1}$,a$_n$] = [a$_0$;a$_1$,... a$_{n−1}$,(a$_n$−1),1]. Usually the first, shorter one is chosen as the canonical representation.
* The simple continued fraction representation of an irrational number is unique. (However, additional representations are possible when using generalized continued fractions; see below.)
* The real numbers whose continued fraction eventually repeats are precisely the quadratic irrationals. For example, the repeating continued fraction [1;1,1,1,...] is the golden ratio, and the repeating continued fraction [1;2,2,2,...] is the square root of 2. In contrast, the decimal representations of quadratic irrationals are apparently random. The square roots of all (positive) integers that are not perfect squares are quadratic irrationals, and hence are unique periodic continued fractions.
* The successive approximations generated in finding the continued fraction representation of a number, that is, by truncating the continued fraction representation, are in a certain sense (described below) the "best possible".

## Formulation

A continued fraction in canonical form is an expression of the form
    $a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{a_{3}+{\vphantom {\cfrac {1}{1}}}{_{\ddots }}}}}}}}$
where a$_i$ are integer numbers, called the coefficients or terms of the continued fraction.
When the expression contains finitely many terms, it is called a finite continued fraction. When the expression contains infinitely many terms, it is called an infinite continued fraction. When the terms eventually repeat from some point onwards, the continued fraction is called periodic.
Thus, all of the following illustrate valid finite simple continued fractions:
Examples of finite simple continued fractions :
| Formula | Numeric | Remarks |
| $\ a_{0}$ | $\ 2$ | All integers are a degenerate case |
| $\ a_{0}+{\cfrac {1}{a_{1}}}$ | $\ 2+{\cfrac {1}{3}}$ | Simplest possible fractional form |
| $\ a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}}}}}$ | $\ -3+{\cfrac {1}{2+{\cfrac {1}{18}}}}$ | First integer may be negative |
| $\ a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{a_{3}}}}}}}$ | $\ {\cfrac {1}{15+{\cfrac {1}{1+{\cfrac {1}{102}}}}}}$ | First integer may be zero |
For simple continued fractions of the form
    $r=a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{a_{3}+{\vphantom {\cfrac {1}{1}}}{_{\ddots }}}}}}}}$
the $a_{n}$ term can be calculated using the following recursive formula:
    $a_{n}=\left\lfloor {\frac {N_{n}}{N_{n+1}}}\right\rfloor$
where $N_{n+1}=N_{n-1}{\bmod {N}}_{n}$ and ${\begin{cases}N_{0}=r,\\N_{1}=1,\end{cases}}$
from which it can be understood that the $a_{n}$ sequence stops if $N_{n+1}=0$.

### Notations

Consider a continued fraction expressed as
    $x=a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{a_{3}+{\cfrac {1}{a_{4}}}}}}}}}$
Because such a continued fraction expression may take a significant amount of vertical space, a number of methods have been tried to shrink it.
Gottfried Leibniz sometimes used the notation
    ${\begin{aligned}x=a_{0}+{\dfrac {1}{a_{1}}}{ \atop +}\\[28mu]\ \end{aligned}}\!{\begin{aligned}{\dfrac {1}{a_{2}}}{ \atop +}\\[2mu]\ \end{aligned}}\!{\begin{aligned}{\dfrac {1}{a_{3}}}{ \atop +}\end{aligned}}\!{\begin{aligned}\\[2mu]{\dfrac {1}{a_{4}}},\end{aligned}}$
and later the same idea was taken even further with the nested fraction bars drawn aligned, for example by Alfred Pringsheim as
    $x=a_{0}+{ \atop {{\big |}\!}}\!{\frac {1}{\,a_{1}}}\!{{\!{\big |}} \atop}+{ \atop {{\big |}\!}}\!{\frac {1}{\,a_{2}}}\!{{\!{\big |}} \atop}+{ \atop {{\big |}\!}}\!{\frac {1}{\,a_{3}}}\!{{\!{\big |}} \atop}+{ \atop {{\big |}\!}}\!{\frac {1}{\,a_{4}}}\!{{\!{\big |}} \atop},$
or in more common related notations as
    $x=a_{0}+{1 \over a_{1}+}\,{1 \over a_{2}+}\,{1 \over a_{3}+}\,{1 \over a_{4}}$
or
    $x=a_{0}+{1 \over a_{1}}{ \atop +}{1 \over a_{2}}{ \atop +}{1 \over a_{3}}{ \atop +}{1 \over a_{4}}.$
Carl Friedrich Gauss used a notation reminiscent of summation notation,
    $x=a_{0}+{\underset {i=1}{\overset {4}{\mathrm {K} }}}~{\frac {1}{a_{i}}},$
or in cases where the numerator is always 1, eliminated the fraction bars altogether, writing a list-style
    $x=[a_{0};a_{1},a_{2},a_{3},a_{4}].$
Sometimes list-style notation uses angle brackets instead,
    $x=\left\langle a_{0};a_{1},a_{2},a_{3},a_{4}\right\rangle .$
The semicolon in the square and angle bracket notations is sometimes replaced by a comma.
One may also define infinite simple continued fractions as limits:
    $[a_{0};a_{1},a_{2},a_{3},\,\ldots \,]=\lim _{n\to \infty }\,[a_{0};a_{1},a_{2},\,\ldots ,a_{n}].$
This limit exists for any choice of $a_{0}$ and positive integers $a_{1},a_{2},\ldots$.

## Calculating continued fraction representations

Consider a real number ⁠$r$⁠. Let $i=\lfloor r\rfloor$ and let ⁠$f=r-i$⁠. When ⁠$f\neq 0$⁠, the continued fraction representation of $r$ is ⁠$[i;a_{1},a_{2},\ldots ]$⁠, where $[a_{1};a_{2},\ldots ]$ is the continued fraction representation of ⁠$1/f$⁠. When ⁠$r\geq 0$⁠, then $i$ is the integer part of $r$, and $f$ is the fractional part of ⁠$r$⁠.
In order to calculate a continued fraction representation of a number $r$, write down the floor of $r$. Subtract this value from $r$. If the difference is 0, stop; otherwise find the reciprocal of the difference and repeat. The procedure will halt if and only if $r$ is rational. This process can be efficiently implemented using the Euclidean algorithm when the number is rational.
The table below shows an implementation of this procedure for the number ⁠$3.245=649/200$⁠:
    Step Real Number Integer part Fractional part Simplified Reciprocal of f 1 $r={\frac {649}{200}}$ $i=3$ $f={\frac {649}{200}}-3$ $={\frac {49}{200}}$ ${\frac {1}{f}}={\frac {200}{49}}$ 2 $r={\frac {200}{49}}$ $i=4$ $f={\frac {200}{49}}-4$ $={\frac {4}{49}}$ ${\frac {1}{f}}={\frac {49}{4}}$ 3 $r={\frac {49}{4}}$ $i=12$ $f={\frac {49}{4}}-12$ $={\frac {1}{4}}$ ${\frac {1}{f}}={\frac {4}{1}}$ 4 $r=4$ $i=4$ $f=4-4$ $=0$ STOP
The continued fraction for ⁠$3.245$⁠ is thus $[3;4,12,4],$ or, expanded:
${\frac {649}{200}}=3+{\cfrac {1}{4+{\cfrac {1}{12+{\cfrac {1}{4}}}}}}.$

## Reciprocals

The continued fraction representations of a positive rational number and its reciprocal are identical except for a shift one place left or right depending on whether the number is less than or greater than one respectively. In other words, the numbers represented by $[a_{0};a_{1},a_{2},\ldots ,a_{n}]$ and $[0;a_{0},a_{1},\ldots ,a_{n}]$ are reciprocals.
For instance if $a$ is an integer and $x<1$ then
    $x=0+{\frac {1}{a+{\frac {1}{b}}}}$ and ${\frac {1}{x}}=a+{\frac {1}{b}}$.
If $x>1$ then
    $x=a+{\frac {1}{b}}$ and ${\frac {1}{x}}=0+{\frac {1}{a+{\frac {1}{b}}}}$.
The last number that generates the remainder of the continued fraction is the same for both $x$ and its reciprocal.
For example,
    $2.25={\frac {9}{4}}=[2;4]$ and ${\frac {1}{2.25}}={\frac {4}{9}}=[0;2,4]$.

## Finite continued fractions

Every finite continued fraction represents a rational number, and every rational number can be represented in precisely two different ways as a finite continued fraction, with the conditions that the first coefficient is an integer and the other coefficients are positive integers. These two representations agree except in their final terms. In the longer representation the final term in the continued fraction is 1; the shorter representation drops the final 1, but increases the new final term by 1. The final element in the short representation is therefore always greater than 1, if present. In symbols:
    [a$_0$; a$_1$, a$_2$, ..., a$_{n−1}$, a$_n$, 1] = [a$_0$; a$_1$, a$_2$, ..., a$_{n−1}$, a$_n$ + 1].
    [a$_0$; 1] = [a$_0$ + 1].

## Infinite continued fractions and convergents

Every infinite continued fraction is irrational, and every irrational number can be represented in precisely one way as an infinite continued fraction.
An infinite continued fraction representation for an irrational number is useful because its initial segments provide rational approximations to the number. These rational numbers are called the convergents of the continued fraction. The larger a term is in the continued fraction, the closer the corresponding convergent is to the irrational number being approximated. Numbers like π have occasional large terms in their continued fraction, which makes them easy to approximate with rational numbers. Other numbers like e have only small terms early in their continued fraction, which makes them more difficult to approximate rationally. The golden ratio Φ has terms equal to 1 everywhere—the smallest values possible—which makes Φ the most difficult number to approximate rationally. In this sense, therefore, it is the "most irrational" of all irrational numbers. Even-numbered convergents are smaller than the original number, while odd-numbered ones are larger.
For a continued fraction [a$_0$; a$_1$, a$_2$, ...], the first four convergents (numbered 0 through 3) are
    ${\frac {a_{0}}{1}},\,{\frac {a_{1}a_{0}+1}{a_{1}}},\,{\frac {a_{2}(a_{1}a_{0}+1)+a_{0}}{a_{2}a_{1}+1}},\,{\frac {a_{3}{\bigla_{2}(a_{1}a_{0}+1)+a_{0}{\bigr )}+(a_{1}a_{0}+1)}{a_{3}(a_{2}a_{1}+1)+a_{1}}}.$
The numerator of the third convergent is formed by multiplying the numerator of the second convergent by the third coefficient, and adding the numerator of the first convergent. The denominators are formed similarly. Therefore, each convergent can be expressed explicitly in terms of the continued fraction as the ratio of certain multivariate polynomials called continuants.
If successive convergents are found, with numerators h$_1$, h$_2$, ... and denominators k$_1$, k$_2$, ... then the relevant recursive relation is that of Gaussian brackets:
    ${\begin{aligned}h_{n}&=a_{n}h_{n-1}+h_{n-2},\\[3mu]k_{n}&=a_{n}k_{n-1}+k_{n-2}.\end{aligned}}$
The successive convergents are given by the formula
    ${\frac {h_{n}}{k_{n}}}={\frac {a_{n}h_{n-1}+h_{n-2}}{a_{n}k_{n-1}+k_{n-2}}}.$
Thus to incorporate a new term into a rational approximation, only the two previous convergents are necessary. The initial "convergents" (required for the first two terms) are $^0$⁄$_1$ and $^1$⁄$_0$. For example, here are the convergents for [0;1,5,2,2].
    n −2−101234 a$_n$  01522 h$_n$ 010151127 k$_n$ 101161332
When using the Babylonian method to generate successive approximations to the square root of an integer, if one starts with the lowest integer as first approximant, the rationals generated all appear in the list of convergents for the continued fraction. Specifically, the approximants will appear on the convergents list in positions 0, 1, 3, 7, 15, ... , 2$^k$−1, ... For example, the continued fraction expansion for ${\sqrt {3}}$ is [1; 1, 2, 1, 2, 1, 2, 1, 2, ...]. Comparing the convergents with the approximants derived from the Babylonian method:
    n −2−101234567 a$_n$  11212121 h$_n$ 01125719267197 k$_n$ 10113411154156
    x$_0$ = 1 = ⁠1/1⁠
    x$_1$ = ⁠1/2⁠(1 + ⁠3/1⁠) = ⁠2/1⁠ = 2
    x$_2$ = ⁠1/2⁠(2 + ⁠3/2⁠) = ⁠7/4⁠
    x$_3$ = ⁠1/2⁠(⁠7/4⁠ + ⁠3/⁠7/4⁠⁠) = ⁠97/56⁠

### Properties

The Baire space is a topological space on infinite sequences of natural numbers. The infinite continued fraction provides a homeomorphism from the Baire space to the space of irrational real numbers (with the subspace topology inherited from the usual topology on the reals). The infinite continued fraction also provides a map between the quadratic irrationals and the dyadic rationals, and from other irrationals to the set of infinite strings of binary numbers (i.e. the Cantor set); this map is called the Minkowski question-mark function. The mapping has interesting self-similar fractal properties; these are given by the modular group, which is the subgroup of Möbius transformations having integer values in the transform. Roughly speaking, continued fraction convergents can be taken to be Möbius transformations acting on the (hyperbolic) upper half-plane; this is what leads to the fractal self-symmetry.
The limit probability distribution of the coefficients in the continued fraction expansion of a random variable uniformly distributed in (0, 1) is the Gauss–Kuzmin distribution.

### Some useful theorems

If $\ a_{0}\ ,$ $a_{1}\ ,$ $a_{2}\ ,$ $\ \ldots \$ is an infinite sequence of positive integers, define the sequences $\ h_{n}\$ and $\ k_{n}\$ recursively:
| $h_{n}=a_{n}\ h_{n-1}+h_{n-2}\ ,$ | $h_{-1}=1\ ,$ | $h_{-2}=0\ ;$ |
| $k_{n}=a_{n}\ k_{n-1}+k_{n-2}\ ,$ | $k_{-1}=0\ ,$ | $k_{-2}=1~.$ |
   Theorem 1. For any positive real number $\ x\$
       $\left[\ a_{0};\ a_{1},\ \dots ,a_{n-1},x\ \right]={\frac {x\ h_{n-1}+h_{n-2}}{\ x\ k_{n-1}+k_{n-2}\ }},\quad \left[\ a_{0};\ a_{1},\ \dots ,a_{n-1}+x\ \right]={\frac {h_{n-1}+xh_{n-2}}{\ k_{n-1}+xk_{n-2}\ }}$
   Theorem 2. The convergents of $\ [\ a_{0}\ ;$ $a_{1}\ ,$ $a_{2}\ ,$ $\ldots \ ]\$ are given by
       $\left[\ a_{0};\ a_{1},\ \dots ,a_{n}\ \right]={\frac {h_{n}}{\ k_{n}\ }}~.$
   or in matrix form,${\begin{bmatrix}h_{n}&h_{n-1}\\k_{n}&k_{n-1}\end{bmatrix}}={\begin{bmatrix}a_{0}&1\\1&0\end{bmatrix}}\cdots {\begin{bmatrix}a_{n}&1\\1&0\end{bmatrix}}$
   Theorem 3. If the $\ n$th convergent to a continued fraction is $\ {\frac {h_{n}}{k_{n}}}\ ,$ then
       $k_{n}\ h_{n-1}-k_{n-1}\ h_{n}=(-1)^{n}\ ,$
   or equivalently
       ${\frac {h_{n}}{\ k_{n}\ }}-{\frac {h_{n-1}}{\ k_{n-1}\ }}={\frac {(-1)^{n+1}}{\ k_{n-1}\ k_{n}\ }}~.$
Corollary 1: Each convergent is in its lowest terms (for if $\ h_{n}\$ and $\ k_{n}\$ had a nontrivial common divisor it would divide $\ k_{n}\ h_{n-1}-k_{n-1}\ h_{n}\ ,$ which is impossible).
Corollary 2: The difference between successive convergents is a fraction whose numerator is unity:
    ${\frac {h_{n}}{k_{n}}}-{\frac {h_{n-1}}{k_{n-1}}}={\frac {\ h_{n}\ k_{n-1}-k_{n}\ h_{n-1}\ }{\ k_{n}\ k_{n-1}\ }}={\frac {(-1)^{n+1}}{\ k_{n}\ k_{n-1}\ }}~.$
Corollary 3: The continued fraction is equivalent to a series of alternating terms:
    $a_{0}+\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{\ k_{n}\ k_{n+1}\ }}~.$
Corollary 4: The matrix
    ${\begin{bmatrix}h_{n}&h_{n-1}\\k_{n}&k_{n-1}\end{bmatrix}}={\begin{bmatrix}a_{0}&1\\1&0\end{bmatrix}}\cdots {\begin{bmatrix}a_{n}&1\\1&0\end{bmatrix}}$
has determinant $(-1)^{n+1}$, and thus belongs to the group of $\ 2\times 2\$ unimodular matrices $\ \mathrm {GL} (2,\mathbb {Z} )~.$
Corollary 5: The matrix${\begin{bmatrix}h_{n}&h_{n-2}\\k_{n}&k_{n-2}\end{bmatrix}}={\begin{bmatrix}h_{n-1}&h_{n-2}\\k_{n-1}&k_{n-2}\end{bmatrix}}{\begin{bmatrix}a_{n}&0\\1&1\end{bmatrix}}$ has determinant $(-1)^{n}a_{n}$, or equivalently,${\frac {h_{n}}{\ k_{n}\ }}-{\frac {h_{n-2}}{\ k_{n-2}\ }}={\frac {(-1)^{n}}{\ k_{n-2}\ k_{n}\ }}a_{n}$meaning that the odd terms monotonically decrease, while the even terms monotonically increase.
Corollary 6: The denominator sequence $k_{0},k_{1},k_{2},\dots$ satisfies the recurrence relation $k_{-1}=0,k_{0}=1,k_{n}=k_{n-1}a_{n}+k_{n-2}$, and grows at least as fast as the Fibonacci sequence, which itself grows like $O(\phi ^{n})$ where $\phi =1.618\dots$ is the golden ratio.
   Theorem 4. Each ($\ s$th) convergent is nearer to a subsequent ($\ n$th) convergent than any preceding ($\ r$th) convergent is. In symbols, if the $\ n$th convergent is taken to be $\ \left[\ a_{0};\ a_{1},\ \ldots ,\ a_{n}\ \right]=x_{n}\ ,$ then
       $\left|\ x_{r}-x_{n}\ \right|>\left|\ x_{s}-x_{n}\ \right|$
   for all $\ r<s<n~.$
Corollary 1: The even convergents (before the $\ n$th) continually increase, but are always less than $\ x_{n}~.$
Corollary 2: The odd convergents (before the $\ n$th) continually decrease, but are always greater than $\ x_{n}~.$
   Theorem 5.
       ${\frac {1}{\ k_{n}\ (k_{n+1}+k_{n})\ }}<\left|\ x-{\frac {h_{n}}{\ k_{n}\ }}\ \right|<{\frac {1}{\ k_{n}\ k_{n+1}\ }}~.$
Corollary 1: A convergent is nearer to the limit of the continued fraction than any fraction whose denominator is less than that of the convergent.
Corollary 2: A convergent obtained by terminating the continued fraction just before a large term is a close approximation to the limit of the continued fraction.
   Theorem 6: Consider the set of all open intervals with end-points $[0;a_{1},\dots ,a_{n}],[0;a_{1},\dots ,a_{n}+1]$. Denote it as ${\mathcal {C}}$. Any open subset of $[0,1]\setminus \mathbb {Q}$ is a disjoint union of sets from ${\mathcal {C}}$.
Corollary: The infinite continued fraction provides a homeomorphism from the Baire space to $[0,1]\setminus \mathbb {Q}$.

## Semiconvergents

If
    ${\frac {h_{n-1}}{k_{n-1}}},{\frac {h_{n}}{k_{n}}}$
are consecutive convergents, then any fractions of the form
    ${\frac {h_{n-1}+mh_{n}}{k_{n-1}+mk_{n}}},$
where $m$ is an integer such that $0\leq m\leq a_{n+1}$, are called semiconvergents, secondary convergents, or intermediate fractions. The $(m+1)$-st semiconvergent equals the mediant of the $m$-th one and the convergent ${\tfrac {h_{n}}{k_{n}}}$. Sometimes the term is taken to mean that being a semiconvergent excludes the possibility of being a convergent (i.e., $0<m<a_{n+1}$), rather than that a convergent is a kind of semiconvergent.
It follows that semiconvergents represent a monotonic sequence of fractions between the convergents ${\tfrac {h_{n-1}}{k_{n-1}}}$ (corresponding to $m=0$) and ${\tfrac {h_{n+1}}{k_{n+1}}}$ (corresponding to $m=a_{n+1}$). The consecutive semiconvergents ${\tfrac {a}{b}}$ and ${\tfrac {c}{d}}$ satisfy the property $ad-bc=\pm 1$.
If a rational approximation ${\tfrac {p}{q}}$ to a real number $x$ is such that the value $\left|x-{\tfrac {p}{q}}\right|$ is smaller than that of any approximation with a smaller denominator, then ${\tfrac {p}{q}}$ is a semiconvergent of the continued fraction expansion of $x$. The converse is not true, however.

## Best rational approximations

One can choose to define a best rational approximation to a real number x as a rational number ⁠n/d⁠, d > 0, that is closer to x than any approximation with a smaller or equal denominator. The simple continued fraction for x can be used to generate all of the best rational approximations for x by applying these three rules:
* Truncate the continued fraction, and reduce its last term by a chosen amount (possibly zero).
* The reduced term cannot have less than half its original value.
* If the final term is even, half its value is admissible only if the corresponding semiconvergent is better than the previous convergent. (See below.)
For example, 0.84375 has continued fraction [0;1,5,2,2]. Here are all of its best rational approximations.
    Continued fraction  [0;1]  [0;1,3]  [0;1,4]  [0;1,5]  [0;1,5,2]  [0;1,5,2,1]  [0;1,5,2,2]  Rational approximation 1⁠3/4⁠⁠4/5⁠⁠5/6⁠⁠11/13⁠⁠16/19⁠⁠27/32⁠ Decimal equivalent 10.750.8~0.83333~0.84615~0.842110.84375 Error +18.519%−11.111%−5.1852%−1.2346%+0.28490%−0.19493%0%
The strictly monotonic increase in the denominators as additional terms are included permits an algorithm to impose a limit, either on size of denominator or closeness of approximation.
The "half rule" mentioned above requires that when a$_k$ is even, the halved term a$_k$/2 is admissible if and only if |x − [a$_0$ ; a$_1$, ..., a$_{k−1}$]| > |x − [a$_0$ ; a$_1$, ..., a$_{k−1}$, a$_k$/2]|. This is equivalent to:
    [a$_k$; a$_{k−1}$, ..., a$_1$] > [a$_k$; a$_{k+1}$, ...].
The convergents to x are "best approximations" in a much stronger sense than the one defined above. Namely, n/d is a convergent for x if and only if |dx − n| has the smallest value among the analogous expressions for all rational approximations m/c with c ≤ d; that is, we have |dx − n| < |cx − m| so long as c < d. (Note also that |d$_k$x − n$_k$| → 0 as k → ∞.)

### Best rational within an interval

A rational that falls within the interval (x, y), for 0 < x < y, can be found with the continued fractions for x and y. When both x and y are irrational and
    x = [a$_0$; a$_1$, a$_2$, ..., a$_{k−1}$, a$_k$, a$_{k+1}$, ...]
    y = [a$_0$; a$_1$, a$_2$, ..., a$_{k−1}$, b$_k$, b$_{k+1}$, ...]
where x and y have identical continued fraction expansions up through a$_{k−1}$, a rational that falls within the interval (x, y) is given by the finite continued fraction,
    z(x,y) = [a$_0$; a$_1$, a$_2$, ..., a$_{k−1}$, min(a$_k$, b$_k$) + 1]
This rational will be best in the sense that no other rational in (x, y) will have a smaller numerator or a smaller denominator.
If x is rational, it will have two continued fraction representations that are finite, x$_1$ and x$_2$, and similarly a rational y will have two representations, y$_1$ and y$_2$. The coefficients beyond the last in any of these representations should be interpreted as +∞; and the best rational will be one of z(x$_1$, y$_1$), z(x$_1$, y$_2$), z(x$_2$, y$_1$), or z(x$_2$, y$_2$).
For example, the decimal representation 3.1416 could be rounded from any number in the interval [3.14155, 3.14165). The continued fraction representations of 3.14155 and 3.14165 are
    3.14155 = [3; 7, 15, 2, 7, 1, 4, 1, 1] = [3; 7, 15, 2, 7, 1, 4, 2]
    3.14165 = [3; 7, 16, 1, 3, 4, 2, 3, 1] = [3; 7, 16, 1, 3, 4, 2, 4]
and the best rational between these two is
    [3; 7, 16] = ⁠355/113⁠ = 3.1415929....
Thus, ⁠355/113⁠ is the best rational number corresponding to the rounded decimal number 3.1416, in the sense that no other rational number that would be rounded to 3.1416 will have a smaller numerator or a smaller denominator.

### Interval for a convergent

A rational number, which can be expressed as finite continued fraction in two ways,
    z = [a$_0$; a$_1$, ..., a$_{k−1}$, a$_k$, 1] = [a$_0$; a$_1$, ..., a$_{k−1}$, a$_k$ + 1] = ⁠p$_k$/q$_k$⁠
will be one of the convergents for the continued fraction expansion of a number, if and only if the number is strictly between (see this proof)
    x = [a$_0$; a$_1$, ..., a$_{k−1}$, a$_k$, 2] = ⁠2p$_k$ - p$_{k-1}$/2q$_k$ - q$_{k-1}$⁠ and
    y = [a$_0$; a$_1$, ..., a$_{k−1}$, a$_k$ + 2] = ⁠p$_k$ + p$_{k-1}$/q$_k$ + q$_{k-1}$⁠
The numbers x and y are formed by incrementing the last coefficient in the two representations for z. It is the case that x < y when k is even, and x > y when k is odd.
For example, the number ⁠355/113⁠ has the continued fraction representations
    ⁠355/113⁠ = [3; 7, 15, 1] = [3; 7, 16]
and thus ⁠355/113⁠ is a convergent of any number strictly between
    [3; 7, 15, 2]=⁠688/219⁠ ≈ 3.1415525 [3; 7, 17]=⁠377/120⁠ ≈ 3.1416667

### Legendre's theorem on continued fractions

In his Essai sur la théorie des nombres (1798), Adrien-Marie Legendre derives a necessary and sufficient condition for a rational number to be a convergent of the continued fraction of a given real number. A consequence of this criterion, often called Legendre's theorem within the study of continued fractions, is as follows:
Theorem. If α is a real number and p, q are positive integers such that $\left|\alpha -{\frac {p}{q}}\right|<{\frac {1}{2q^{2}}}$, then p/q is a convergent of the continued fraction of α.
This theorem forms the basis for Wiener's attack, a polynomial-time exploit of the RSA cryptographic protocol that can occur for an injudicious choice of public and private keys (specifically, this attack succeeds if the prime factors of the public key n = pq satisfy p < q < 2p and the private key d is less than (1/3)n$^{1/4}$).

## Comparison

Consider x = [a$_0$; a$_1$, ...] and y = [b$_0$; b$_1$, ...]. If k is the smallest index for which a$_k$ is unequal to b$_k$ then x < y if (−1)$^k$(a$_k$ − b$_k$) < 0 and y < x otherwise.
If there is no such k, but one expansion is shorter than the other, say x = [a$_0$; a$_1$, ..., a$_n$] and y = [b$_0$; b$_1$, ..., b$_n$, b$_{n+1}$, ...] with a$_i$ = b$_i$ for 0 ≤ i ≤ n, then x < y if n is even and y < x if n is odd.

## Continued fraction expansion of π and its convergents

To calculate the convergents of π we may set a$_0$ = ⌊π⌋ = 3, define u$_1$ = ⁠1/π − 3⁠ ≈ 7.0625 and a$_1$ = ⌊u$_1$⌋ = 7, u$_2$ = ⁠1/u$_1$ − 7⁠ ≈ 15.9966 and a$_2$ = ⌊u$_2$⌋ = 15, u$_3$ = ⁠1/u$_2$ − 15⁠ ≈ 1.0034. Continuing like this, one can determine the infinite continued fraction of π as
    [3;7,15,1,292,1,1,...] (sequence A001203 in the OEIS).
The fourth convergent of π is [3;7,15,1] = ⁠355/113⁠ = 3.14159292035..., sometimes called Milü, which is fairly close to the true value of π.
Let us suppose that the quotients found are, as above, [3;7,15,1]. The following is a rule by which we can write down at once the convergent fractions which result from these quotients without developing the continued fraction.
The first quotient, supposed divided by unity, will give the first fraction, which will be too small, namely, ⁠3/1⁠. Then, multiplying the numerator and denominator of this fraction by the second quotient and adding unity to the numerator, we shall have the second fraction, ⁠22/7⁠, which will be too large. Multiplying in like manner the numerator and denominator of this fraction by the third quotient, and adding to the numerator the numerator of the preceding fraction, and to the denominator the denominator of the preceding fraction, we shall have the third fraction, which will be too small. Thus, the third quotient being 15, we have for our numerator (22 × 15 = 330) + 3 = 333, and for our denominator, (7 × 15 = 105) + 1 = 106. The third convergent, therefore, is ⁠333/106⁠. We proceed in the same manner for the fourth convergent. The fourth quotient being 1, we say 333 times 1 is 333, and this plus 22, the numerator of the fraction preceding, is 355; similarly, 106 times 1 is 106, and this plus 7 is 113. In this manner, by employing the four quotients [3;7,15,1], we obtain the four fractions:
    ⁠3/1⁠, ⁠22/7⁠, ⁠333/106⁠, ⁠355/113⁠, ....
To sum up, the pattern is ${\text{Numerator}}_{i}={\text{Numerator}}_{(i-1)}\cdot {\text{Quotient}}_{i}+{\text{Numerator}}_{(i-2)}$ ${\text{Denominator}}_{i}={\text{Denominator}}_{(i-1)}\cdot {\text{Quotient}}_{i}+{\text{Denominator}}_{(i-2)}$
These convergents are alternately smaller and larger than the true value of π, and approach nearer and nearer to π. The difference between a given convergent and π is less than the reciprocal of the product of the denominators of that convergent and the next convergent. For example, the fraction ⁠22/7⁠ is greater than π, but ⁠22/7⁠ − π is less than ⁠1/7 × 106⁠ = ⁠1/742⁠ (in fact, ⁠22/7⁠ − π is just more than ⁠1/791⁠ = ⁠1/7 × 113⁠).
The demonstration of the foregoing properties is deduced from the fact that if we seek the difference between one of the convergent fractions and the next adjacent to it we shall obtain a fraction of which the numerator is always unity and the denominator the product of the two denominators. Thus the difference between ⁠22/7⁠ and ⁠3/1⁠ is ⁠1/7⁠, in excess; between ⁠333/106⁠ and ⁠22/7⁠, ⁠1/742⁠, in deficit; between ⁠355/113⁠ and ⁠333/106⁠, ⁠1/11978⁠, in excess; and so on. The result being, that by employing this series of differences we can express in another and very simple manner the fractions with which we are here concerned, by means of a second series of fractions of which the numerators are all unity and the denominators successively be the product of every two adjacent denominators. Instead of the fractions written above, we have thus the series:
    ⁠3/1⁠ + ⁠1/1 × 7⁠ − ⁠1/7 × 106⁠ + ⁠1/106 × 113⁠ − ...
The first term, as we see, is the first fraction; the first and second together give the second fraction, ⁠22/7⁠; the first, the second and the third give the third fraction ⁠333/106⁠, and so on with the rest; the result being that the series entire is equivalent to the original value.

## Non-simple continued fraction

A non-simple continued fraction is an expression of the form
    $x=b_{0}+{\cfrac {a_{1}}{b_{1}+{\cfrac {a_{2}}{b_{2}+{\cfrac {a_{3}}{b_{3}+{\cfrac {a_{4}}{b_{4}+\ddots \,}}}}}}}}$
where the a$_n$ (n > 0) are the partial numerators, the b$_n$ are the partial denominators, and the leading term b$_0$ is called the integer part of the continued fraction.
To illustrate the use of non-simple continued fractions, consider the following example. The sequence of partial denominators of the simple continued fraction of π does not show any obvious pattern:
    $\pi =[3;7,15,1,292,1,1,1,2,1,3,1,\ldots ]$
or
    $\pi =3+{\cfrac {1}{7+{\cfrac {1}{15+{\cfrac {1}{1+{\cfrac {1}{292+{\cfrac {1}{1+{\cfrac {1}{1+{\cfrac {1}{1+{\cfrac {1}{2+{\cfrac {1}{1+{\cfrac {1}{3+{\cfrac {1}{1+\ddots }}}}}}}}}}}}}}}}}}}}}}$
However, several non-simple continued fractions for π have a perfectly regular structure, such as:
    $\pi ={\cfrac {4}{1+{\cfrac {1^{2}}{2+{\cfrac {3^{2}}{2+{\cfrac {5^{2}}{2+{\cfrac {7^{2}}{2+{\cfrac {9^{2}}{2+\ddots }}}}}}}}}}}}={\cfrac {4}{1+{\cfrac {1^{2}}{3+{\cfrac {2^{2}}{5+{\cfrac {3^{2}}{7+{\cfrac {4^{2}}{9+\ddots }}}}}}}}}}=3+{\cfrac {1^{2}}{6+{\cfrac {3^{2}}{6+{\cfrac {5^{2}}{6+{\cfrac {7^{2}}{6+{\cfrac {9^{2}}{6+\ddots }}}}}}}}}}$
    $\displaystyle \pi =2+{\cfrac {2}{1+{\cfrac {1}{1/2+{\cfrac {1}{1/3+{\cfrac {1}{1/4+\ddots }}}}}}}}=2+{\cfrac {2}{1+{\cfrac {1\cdot 2}{1+{\cfrac {2\cdot 3}{1+{\cfrac {3\cdot 4}{1+\ddots }}}}}}}}$
    $\displaystyle \pi =2+{\cfrac {4}{3+{\cfrac {1\cdot 3}{4+{\cfrac {3\cdot 5}{4+{\cfrac {5\cdot 7}{4+\ddots }}}}}}}}$
The first two of these are special cases of the arctangent function with π = 4 arctan (1) and the fourth and fifth one can be derived using the Wallis product.
    $\pi =3+{\cfrac {1}{6+{\cfrac {1^{3}+2^{3}}{6\cdot 1^{2}+1^{2}{\cfrac {1^{3}+2^{3}+3^{3}+4^{3}}{6\cdot 2^{2}+2^{2}{\cfrac {1^{3}+2^{3}+3^{3}+4^{3}+5^{3}+6^{3}}{6\cdot 3^{2}+3^{2}{\cfrac {1^{3}+2^{3}+3^{3}+4^{3}+5^{3}+6^{3}+7^{3}+8^{3}}{6\cdot 4^{2}+\ddots }}}}}}}}}}$
The continued fraction of $\pi$ above consisting of cubes uses the Nilakantha series and an exploit from Leonhard Euler.

## Other continued fraction expansions
### Periodic continued fractions

The numbers with periodic continued fraction expansion are precisely the irrational solutions of quadratic equations with rational coefficients; rational solutions have finite continued fraction expansions as previously stated. The simplest examples are the golden ratio φ = [1;1,1,1,1,1,...] and √2 = [1;2,2,2,2,...], while √14 = [3;1,2,1,6,1,2,1,6...] and √42 = [6;2,12,2,12,2,12...]. All irrational square roots of integers have a special form for the period; a symmetrical string, like the empty string (for √2) or 1,2,1 (for √14), followed by the double of the leading integer.

### A property of the golden ratio φ

Because the continued fraction expansion for φ doesn't use any integers greater than 1, φ is one of the most "difficult" real numbers to approximate with rational numbers. Hurwitz's theorem states that any irrational number k can be approximated by infinitely many rational ⁠m/n⁠ with
    $\left|k-{m \over n}\right|<{1 \over n^{2}{\sqrt {5}}}.$
While virtually all real numbers k will eventually have infinitely many convergents ⁠m/n⁠ whose distance from k is significantly smaller than this limit, the convergents for φ (i.e., the numbers ⁠5/3⁠, ⁠8/5⁠, ⁠13/8⁠, ⁠21/13⁠, etc.) consistently "toe the boundary", keeping a distance of almost exactly ${\scriptstyle {1 \over n^{2}{\sqrt {5}}}}$ away from φ, thus never producing an approximation nearly as impressive as, for example, ⁠355/113⁠ for π. It can also be shown that every real number of the form ⁠a + bφ/c + dφ⁠, where a, b, c, and d are integers such that a d − b c = ±1, shares this property with the golden ratio φ; and that all other real numbers can be more closely approximated.

### Regular patterns in continued fractions

While there is no discernible pattern in the simple continued fraction expansion of π, there is one for e, the base of the natural logarithm:
    $e=e^{1}=[2;1,2,1,1,4,1,1,6,1,1,8,1,1,10,1,1,12,1,1,\dots ],$
which is a special case of this general expression for positive integer n:
    $e^{1/n}=[1;n-1,1,1,3n-1,1,1,5n-1,1,1,7n-1,1,1,\dots ]\,\!.$
Another, more complex pattern appears in this continued fraction expansion for positive odd n:
    $e^{2/n}=\left[1;{\frac {n-1}{2}},6n,{\frac {5n-1}{2}},1,1,{\frac {7n-1}{2}},18n,{\frac {11n-1}{2}},1,1,{\frac {13n-1}{2}},30n,{\frac {17n-1}{2}},1,1,\dots \right]\,\!,$
with a special case for n = 1:
    $e^{2}=[7;2,1,1,3,18,5,1,1,6,30,8,1,1,9,42,11,1,1,12,54,14,1,1\dots ,3k,12k+6,3k+2,1,1\dots ]\,\!.$
Other continued fractions of this sort are
    $\tanh(1/n)=[0;n,3n,5n,7n,9n,11n,13n,15n,17n,19n,\dots ]$
where n is a positive integer; also, for integer n:
    $\tan(1/n)=[0;n-1,1,3n-2,1,5n-2,1,7n-2,1,9n-2,1,\dots ]\,\!,$
with a special case for n = 1:
    $\tan(1)=[1;1,1,3,1,5,1,7,1,9,1,11,1,13,1,15,1,17,1,19,1,\dots ]\,\!.$
If I$_n$(x) is the modified, or hyperbolic, Bessel function of the first kind, we may define a function on the rationals ⁠p/q⁠ by
    $S(p/q)={\frac {I_{p/q}(2/q)}{I_{1+p/q}(2/q)}},$
which is defined for all rational numbers, with p and q in lowest terms. Then for all nonnegative rationals, we have
    $S(p/q)=[p+q;p+2q,p+3q,p+4q,\dots ],$
with similar formulas for negative rationals; in particular we have
    $S(0)=S(0/1)=[1;2,3,4,5,6,7,\dots ].$
Many of the formulas can be proved using Gauss's continued fraction.

### Typical continued fractions

Most irrational numbers do not have any periodic or regular behavior in their continued fraction expansion. Nevertheless, for almost all numbers on the unit interval, they have the same limit behavior.
The arithmetic average diverges: $\lim _{n\to \infty }{\frac {1}{n}}\sum _{k=1}^{n}a_{k}=+\infty$, and so the coefficients grow arbitrarily large: $\limsup _{n}a_{n}=+\infty$. In particular, this implies that almost all numbers are well-approximable, in the sense that$\liminf _{n\to \infty }\left|x-{\frac {p_{n}}{q_{n}}}\right|q_{n}^{2}=0$Khinchin proved that the geometric mean of a$_i$ tends to a constant (known as Khinchin's constant):$\lim _{n\rightarrow \infty }\left(a_{1}a_{2}...a_{n}\right)^{1/n}=K_{0}=2.6854520010\dots$Paul Lévy proved that the nth root of the denominator of the nth convergent converges to Lévy's constant $\lim _{n\rightarrow \infty }q_{n}^{1/n}=e^{\pi ^{2}/(12\ln 2)}=3.2758\ldots$Lochs' theorem states that the convergents converge exponentially at the rate of$\lim _{n\to \infty }{\frac {1}{n}}\ln \left|x-{\frac {p_{n}}{q_{n}}}\right|=-{\frac {\pi ^{2}}{6\ln 2}}$

## Applications
### Pell's equation

Continued fractions play an essential role in the solution of Pell's equation. For example, for positive integers p and q, and non-square n, it is true that if p$^2$ − nq$^2$ = ±1, then ⁠p/q⁠ is a convergent of the regular continued fraction for √n. The converse holds if the period of the regular continued fraction for √n is 1, and in general the period describes which convergents give solutions to Pell's equation.

### Dynamical systems

Continued fractions also play a role in the study of dynamical systems, where they tie together the Farey fractions which are seen in the Mandelbrot set with Minkowski's question-mark function and the modular group Gamma.
The backwards shift operator for continued fractions is the map h(x) = 1/x − ⌊1/x⌋ called the Gauss map, which lops off digits of a continued fraction expansion: h([0; a$_1$, a$_2$, a$_3$, ...]) = [0; a$_2$, a$_3$, ...]. The transfer operator of this map is called the Gauss–Kuzmin–Wirsing operator. The distribution of the digits in continued fractions is given by the zero'th eigenvector of this operator, and is called the Gauss–Kuzmin distribution.

## History

* 300 BCE Euclid's Elements contains an algorithm for the greatest common divisor, whose modern version generates a continued fraction as the sequence of quotients of successive Euclidean divisions that occur in it.
* 499 The Aryabhatiya contains the solution of indeterminate equations using continued fractions
* 1572 Rafael Bombelli, L'Algebra Opera – method for the extraction of square roots which is related to continued fractions
* 1613 Pietro Cataldi, Trattato del modo brevissimo di trovar la radice quadra delli numeri – first notation for continued fractions
    Cataldi represented a continued fraction as $a_{0}$ & ${\frac {n_{1}}{d_{1}\cdot }}$ & ${\frac {n_{2}}{d_{2}\cdot }}$ & ${\frac {n_{3}}{d_{3}\cdot }}$ with the dots indicating where the following fractions went.
* 1695 John Wallis, Opera Mathematica – introduction of the term "continued fraction"
* 1737 Leonhard Euler, De fractionibus continuis dissertatio – Provided the first then-comprehensive account of the properties of continued fractions, and included the first proof that the number e is irrational.
* 1748 Euler, Introductio in analysin infinitorum. Vol. I, Chapter 18 – proved the equivalence of a certain form of continued fraction and a generalized infinite series, proved that every rational number can be written as a finite continued fraction, and proved that the continued fraction of an irrational number is infinite.
* 1761 Johann Lambert – gave the first proof of the irrationality of π using a continued fraction for tan(x).
* 1768 Joseph-Louis Lagrange – provided the general solution to Pell's equation using continued fractions similar to Bombelli's
* 1770 Lagrange – proved that quadratic irrationals expand to periodic continued fractions.
* 1813 Carl Friedrich Gauss, Werke, Vol. 3, pp. 134–138 – derived a very general complex-valued continued fraction via a clever identity involving the hypergeometric function
* 1892 Henri Padé defined Padé approximant
* 1972 Bill Gosper – First exact algorithms for continued fraction arithmetic.