Continued Fractions
Introduction
Continued fractions can represent real numbers as the limit of a convergent sequence of rational numbers. The rational numbers in this sequence are easy to compute and provide the best approximations to the real number, so they are often used in algorithm competitions. Additionally, continued fractions are closely related to the Euclidean algorithm and can be applied to a series of number theory problems.
About Algorithm Implementations Related to Continued Fractions
This article will provide a series of algorithm implementations for continued fractions. Some algorithms may not guarantee that the integers involved in intermediate calculations stay within the range of 32-bit or 64-bit integer variables. For such cases, please refer to the corresponding Python implementations or replace the integer variables in the C++ implementations with high-precision integer classes. To focus on key points, some code in this article may call functions implemented earlier without repeating their implementations.
Continued Fractions
Continued fractions (continued fraction) are themselves a form of notation.
Finite Continued Fractions
For a sequence \(\{a_k\}_{i=0}^n\), the continued fraction \([a_0,a_1,\cdots,a_n]\) represents the expansion
A continued fraction is meaningful if and only if the corresponding expansion is meaningful. These \(a_k\) are called the terms or coefficients of the continued fraction.
Notation
More general continued fractions allow numerators in the expansion to not always be \(1\), and the corresponding continued fraction notation also needs modification, which is beyond the scope of this article. Additionally, some literature writes the first comma "\(,\)" as a semicolon "\(;\)", which has no difference in meaning from the notation used here.
Of course, continued fractions can also be extended to infinite sequences.
Infinite Continued Fractions
For an infinite sequence \(\{a_k\}_{i=0}^\infty\), the continued fraction \([a_0,a_1,\cdots]\) represents the limit
A continued fraction is meaningful if and only if the corresponding limit is meaningful. Here, \(x_k=[a_0,a_1,\cdots,a_k]\) is called the \(k\)-th convergent of \(x\), while \(r_k=[a_k,a_{k+1},\cdots]\) is called the \(k\)-th remainder or complete quotient of \(x\). Correspondingly, the term \(a_k\) is sometimes called the \(k\)-th partial quotient.
Simple Continued Fractions
In number theory, we mainly consider continued fractions whose terms are all integers.
Simple Continued Fractions
For a continued fraction \([a_0,a_1,\cdots]\), if \(a_0\) is an integer and \(a_1,a_2,\cdots\) are all positive integers, it is called a simple continued fraction, also simply referred to as a continued fraction. If the sequence \(\{a_i\}\) is finite, it is called a finite (simple) continued fraction; otherwise, it is called an infinite (simple) continued fraction. Moreover, \(a_0\) is called its integer part.
Unless otherwise specified, the continued fractions mentioned in this article refer to simple continued fractions. It can be proven that infinite simple continued fractions are always convergent, and the remainders of simple continued fractions are always positive.
Continued fractions have the following basic properties:
Properties
Let the real number \(x=[a_0,a_1,a_2,\cdots]\). Then, the following properties hold:
- For any \(k\in\mathbf Z\), \(x+k=[a_0+k,a_1,a_2,\cdots]\);
- For a real number \(x>1\), \(a_0>0\), and its reciprocal \(x^{-1}=[0,a_0,a_1,a_2,\cdots]\).
Finite continued fractions correspond to rational numbers. Each rational number has exactly two ways to be represented as a continued fraction, with lengths necessarily one odd and one even. The only difference between these two representations is whether the last term is \(1\), i.e.,
These two continued fractions are called the continued fraction representations of the rational number \(x\).[^one-representation] The one with the last term not equal to \(1\) is called the standard representation, and the one with the last term equal to \(1\) is called the non-standard representation.
Example
The continued fraction representation of the rational number \(x=\dfrac{5}{3}\) is
Infinite continued fractions correspond to irrational numbers. Moreover, each irrational number has a unique way to be represented as a continued fraction, called the continued fraction representation of the irrational number.
Computing the Continued Fraction Representation
To find the continued fraction representation of a real number \(x\), note that if its remainder \(r_k\) is not an integer, it must satisfy
Moreover, \(r_{k+1}>1\). Therefore, starting from \(r_0=x\), we can recursively compute
The sequence \(\{a_k\}\) produced by this process is always uniquely determined, unless some remainder \(r_k\) becomes an integer. If \(r_k\) becomes an integer, the process should terminate, and we can choose to output the corresponding standard or non-standard representation.
In algorithm competitions, we often deal with rational numbers \(x=\dfrac{p}{q}\). In this case, each remainder \(r_k\) is a rational number \(\dfrac{p_k}{q_k}\), and for \(k>0\), since \(r_k>1\), we always have \(p_k>q_k\). Specifically computing the above recurrence relation, we find
At this point, the computation process is essentially performing the Euclidean algorithm on \(p\) and \(q\). This also shows that for a rational number \(r=\dfrac{p}{q}\), the length of its continued fraction representation is \(O(\log\min\{p, q\})\). The complexity of computing the continued fraction representation of \(\dfrac{p}{q}\) is also \(O(\log\min\{p, q\})\).
Reference Implementation
Given the numerator \(p\) and denominator \(q\) of a fraction, output the coefficient sequence \([a_0,a_1,\cdots,a_n]\) of its continued fraction.
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 | |
Convergents
The concept of convergents was introduced in the definition of continued fractions. The convergents of a real number are the convergents of its continued fraction representation: in the continued fraction representation of a real number \(x\), keeping only the first \(k\) terms yields the continued fraction \(x_k\), called the \(k\)-th convergent of the real number \(x\). The convergents \(x_k\) of a real number \(x\) are all rational numbers, and the sequence \(\{x_k\}\) converges to the real number \(x\).
Example: Convergents of the Golden Ratio
The first few convergents of the continued fraction \(x=[1,1,1,1,\cdots]\) are
It can be proven by induction that
where \(\{F_k\}\) is the Fibonacci sequence. According to its closed-form formula,
where \(\phi=\dfrac{1+\sqrt{5}}{2}\) is the golden ratio. As \(k\) tends to infinity,
Thus, the continued fraction \(x=[1,1,1,1,\cdots]\) represents the golden ratio \(\phi\).
These convergents approach the corresponding real number, so they can be used to approximate that real number. For this purpose, it is necessary to understand the properties of convergents.
Recurrence Relations
First, we need to address the computation of these convergents. Although convergents are always obtained by adding one term to the continued fraction, we do not need to recalculate their values each time. In fact, convergents have the following recurrence relation:
Recurrence Formula
For a continued fraction \(x=[a_0,a_1,a_2,\cdots]\), let its \(k\)-th convergent \(x_k\) be written as the fraction \(\dfrac{p_k}{q_k}\). Then,
The starting points of the recurrence are the (formal) fractions
Proof
The numerator and denominator of the convergent \(x_k\) can be viewed as multivariate polynomials in \(a_0, a_1, \cdots, a_k\):
According to the definition of convergents,
Comparing with the above expression, we can set \(Q_k(a_0, \cdots, a_k) = P_{k-1}(a_1, \cdots, a_k)\), so the convergent can be written as
and the polynomial \(P_k\) has the recurrence relation
Since
the starting points of the recurrence are
If we set
we can verify that the above recurrence relation also holds for \(k=0,1\). This is equivalent to defining the formal fractions \(r_{-1}=\dfrac{1}{0}\) and \(r_{-2}=\dfrac{0}{1}\).
The polynomial sequence \(P_k\) satisfying the above recurrence relation is called continuant[^continuant]. It can be written in determinant form:
This is the determinant of a tridiagonal matrix. Expanding from the top-left corner, we can verify that it has the above recurrence relation and initial conditions. Conversely, expanding from the bottom-right corner yields the recurrence relation
which is exactly what we wanted to prove.
Notation
When writing the convergent \(x_k\) as \(\dfrac{p_k}{q_k}\) in this article, it is always assumed that the numerator \(p_k\) and denominator \(q_k\) are given by the recurrence relation above. It will be shown later that this always yields the reduced form of the convergent.
This recurrence implies that
lies between \(x_{k-1}\) and \(x_{k-2}\).
As a corollary of the recurrence relation for convergents, we have the following reversal theorem and reciprocal theorem:
Reversal Theorem
Let the \(k\)-th convergent of the real number \(x=[a_0,a_1,a_2,\cdots]\) be \(\dfrac{p_k}{q_k}\). Then, the ratios of the numerators and denominators of adjacent convergents are
If \(a_0=0\), the first continued fraction should be understood as truncated at the second-to-last term, i.e., \([a_k,a_{k-1},\cdots,a_2]\).
Proof
In the recurrence relations for \(p_k\) and \(q_k\), dividing both sides by \(p_{k-1}\) and \(q_{k-1}\) respectively, we get
Iterating these two equations yields two continued fractions. Substituting the initial values \(\dfrac{p_0}{p_{-1}}=a_0\) and \(\dfrac{q_1}{q_0}=a_1\) completes the proof. For the case \(a_0=0\), interpreting the obtained continued fraction as a formal expression, its remainder
Thus, the last two terms can be directly omitted. If a rigorous proof is needed, note that this expression can be viewed as the limit as \(a_0\rightarrow 0\).
Reciprocal Theorem
The reciprocal of a convergent of a real number \(x>0\) is a convergent of \(x^{-1}\).
Proof
Assume \(x>1\) and has continued fraction representation \([a_0,a_1,a_2,\cdots]\). Then, the continued fraction representation of \(x^{-1}\) is \([0,a_0,a_1,a_2,\cdots]\). Their convergents can be obtained from the recurrence relations. For \(x\), the initial conditions are \(x_{-2}=\dfrac{0}{1}\) and \(x_{-1}=\dfrac{1}{0}\); for \(y=x^{-1}\), the initial conditions are \(y_{-1}=\dfrac{1}{0}\) and \(y_{0}=\dfrac{0}{1}\). Therefore, \(x_{-2}=(y_{-1})^{-1}\) and \(x_{-1}=(y_0)^{-1}\). By the recurrence relation, we get \(x_k=y_{k+1}^{-1}\). This shows that the reciprocal of a convergent of \(x\) is a convergent of \(y=x^{-1}\). A similar discussion applies to the case \(0<x\le 1\).
Using the recurrence relation obtained in this section, we can derive the following algorithm for computing convergents:
Reference Implementation
Given the coefficients \(a_0,a_1,\cdots,a_n\) of a continued fraction, find the numerator and denominator sequences \((p_0,q_0),(p_1,q_1),\cdots,(p_n,q_n)\) of its convergents.
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 | |
Error Estimation
Using the recurrence formula for convergents, we can estimate the error when approximating a real number with convergents.
First, we can compute the difference between adjacent convergents:
Difference of Convergents
Let \(x_k=\dfrac{p_k}{q_k}\) be the \(k\)-th convergent of the real number \(x\). Then,
Therefore, the difference between two adjacent convergents is
Proof
According to the recurrence relation,
This gives \(p_{k+1}q_k − p_kq_{k+1} = (−1)^k\). Dividing both sides by \(q_{k+1}q_k\) yields the conclusion about \(x_{k+1}-x_k\).
Thus, odd-indexed convergents are always greater than their adjacent terms, and even-indexed convergents are always less than their adjacent terms: convergents alternate.
If we only consider even-indexed (or odd-indexed) convergents, the sequence is also monotonically increasing (or decreasing). This is because
is positive when \(k\) is even (odd). At the same time, since the recurrence \(q_{k}=a_kq_{k-1}+q_{k-2}\) holds, the denominator \(q_k\) grows at least as fast as the Fibonacci sequence. Therefore, the difference between adjacent terms must tend to zero. This shows that the even-indexed and odd-indexed convergents approach the same limit from below and above, respectively. This proves that infinite simple continued fractions always converge. The dynamics of convergents approaching the corresponding real number can be seen in the figure below:
Upper (Lower) Convergent
For a real number \(x\) and its convergent \(x_k\), if \(x_k>x\) (\(x_k<x\)), then \(x_k\) is called an upper (lower) convergent of \(x\).
As already shown, upper convergents are the odd-indexed convergents, and lower convergents are the even-indexed convergents.
Using the difference formula, we can write the real number \(x\) as an alternating series:
The convergents and remainders in the definition of continued fractions are the partial sums and remainder of this series.
Using the difference formula, we can also directly estimate the error when approximating a real number with convergents:
Error
Let \(x_k=\dfrac{p_k}{q_k}\neq x\) be the \(k\)-th convergent of the real number \(x\). Then,
where \(r_{k+1}\) is the \((k+1)\)-th remainder of the real number \(x\). Furthermore,
Proof
Since \(x=[a_0,a_1,\cdots,a_k,r_{k+1}]\), and the difference formula for convergents also holds for formal continued fractions, we have
where \(r_{k+1}q_k+q_{k-1}\) is the denominator of the \((k+1)\)-th convergent of this formal continued fraction obtained via the recurrence formula.
To complete the subsequent inequality estimates, note that when \(x_k\neq x\), we always have
so
Therefore, we have the inequality
To obtain the outer bounds, note that \(q_k\le q_{k+1}\).
A simple corollary of the difference formula in this section is that the convergents \(\dfrac{p_k}{q_k}\) are all in reduced form.
Corollary
For any real number \(x\), with the numerator and denominator of the convergent \(x_k=\dfrac{p_k}{q_k}\) given by the recurrence formula, \(\dfrac{p_k}{q_k}\) is a reduced fraction, i.e., \(\gcd(p_k,q_k)=1\).
Proof
Apply Bézout's theorem to the difference formula.
In fact, solutions to binary linear Diophantine equations can be found using continued fractions.
Solving Binary Linear Diophantine Equations
Given \(A, B, C \in \mathbf Z\). Find \(x, y \in \mathbf Z\) such that \(Ax + By = C\).
Solution
Although this problem is usually solved using the extended Euclidean algorithm, it can also be solved via continued fractions.
Let \(\dfrac{A}{B}=[a_0, a_1, \cdots, a_k]\). It was shown above that \(p_k q_{k-1} - p_{k-1} q_k = (-1)^{k-1}\). Replacing \(p_k\) and \(q_k\) with \(A\) and \(B\), we get
where \(g = \gcd(A, B)\). If \(g\) divides \(C\), then one solution is \(x = (-1)^{k-1}\dfrac{C}{g} q_{k-1}\) and \(y = (-1)^{k}\dfrac{C}{g} p_{k-1}\); otherwise, there is no solution.
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 | |
Diophantine Approximation
An important application of continued fraction theory is Diophantine approximation theory. Diophantine approximation refers to approximating real numbers with rational numbers. Of course, due to the density of rational numbers, without restrictions, we can obtain approximations with arbitrarily small error. Therefore, restrictions must be placed on the rational numbers used, such as only choosing rational numbers with denominators less than a certain value. This section discusses the relationship between best approximations under such restrictions and continued fractions.
Approximating Real Numbers with Convergents
First, using the error estimate for convergents, we immediately obtain the following result:
Theorem (Dirichlet)
For an irrational number \(x\), there exist infinitely many reduced fractions \(\dfrac{p}{q}\) such that
holds.
Proof
According to the error estimate for convergents, for the \(k\)-th convergent \(x_k=\dfrac{p_k}{q_k}\) of the irrational number \(x\), we have
Examining the proof of the error formula shows that equality does not hold for any irrational number \(x\). Therefore, the numerators and denominators of all its convergents satisfy the requirement.
This theorem can also be seen as a corollary of Dirichlet's approximation theorem. This is almost the best possible result. The exponent \(2\) in the denominator on the right side of the inequality cannot be improved, but the constant can be made better. Hurwitz's theorem states that the right side can be reduced to \(\dfrac{1}{\sqrt{5}q^2}\), and this is the best bound.
Hurwitz Theorem
For an irrational number \(x\), there exist infinitely many reduced fractions \(\dfrac{p}{q}\) such that
holds, and the \(\sqrt{5}\) on the right side cannot be replaced by a larger real number.
Proof (Borel)
Borel actually proved that among three consecutive convergents of an irrational number \(x\), at least one must satisfy the above condition. Since there are infinitely many convergents, all of which are reduced fractions, the first part of Hurwitz's theorem necessarily holds.
By contradiction. Suppose there exists an irrational number \(x\) and its convergents \(x_{k-1},x_k,x_{k+1}\) such that
hold. Since adjacent convergents must lie on opposite sides of \(x\), by the difference formula,
This can be written as an inequality about the ratio \(\dfrac{q_{k}}{q_{k-1}}\):
Since the left side is rational and the right side is irrational, equality cannot hold. Also, since \(q_k\ge q_{k-1}\), we can solve
Similarly, we can prove
But according to the recurrence relation and combining the two inequalities,
which contradicts the definition of a simple continued fraction. Thus, Borel's conclusion holds.
To show that the bound obtained is the best, we need to find an \(x\) such that for any \(C>\sqrt{5}\), there are only finitely many reduced fractions \(\dfrac{p}{q}\) satisfying
We now prove that \(\phi=\dfrac{\sqrt{5}+1}{2}\) is such an \(x\).[^sqrt5]
Let \(\phi'=\dfrac{-\sqrt{5}+1}{2}\) be the conjugate of \(\phi\). Both are roots of the equation \(x^2-x-1=0\). Thus, for any real number \(x\),
Substituting a reduced fraction \(\dfrac{p}{q}\), we get
For \(C>\sqrt{5}\), we can directly solve \(q<\sqrt{C(C-\sqrt{5})}\), so there cannot be infinitely many solutions satisfying the inequality.
The proofs of these theorems show that convergents provide very good Diophantine approximations. However, they may not be the best approximations. To discuss best approximations, we need to specify the measure of approximation quality. There are often two choices.
Cases Where Best Approximation Results May Not Hold
The next two sections will present some results about best approximations. These results may not hold for some trivial cases. For example, both definitions of best approximations require strict inequalities, but for a half-integer \(x=n+\dfrac12\) with \(n\in\mathbf Z\), its continued fraction form can be \([n,1,1]\). In this case, its first two convergents \(x_0=n\) and \(x_1=n+1\) both have denominator \(1\) and are equidistant from \(x\). This shows that neither is a best approximation. For the statements in this section, readers should assume such cases are excluded. If readers are not concerned with the last few convergents or only care about approximations of irrational numbers, they can ignore these additional complexities.
Best Approximations of the First Kind: Intermediate Fractions
Best approximations of the first kind use
to measure the quality of approximation.
Best Approximation of the First Kind
For a real number \(x\) and a rational number \(\dfrac{p}{q}\), if for any \(\dfrac{p'}{q'}\neq \dfrac{p}{q}\) with \(0<q'\le q\) we have
then the rational number \(\dfrac{p}{q}\) is called a best approximation of the first kind to the real number \(x\).
Best approximations of the first kind are not necessarily convergents but a broader class of fractions.
Intermediate Fractions
Let the real number \(x\) have convergents \(x_{k+1}=[a_0,a_1,\cdots,a_k,a_{k+1}]\), and let integer
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用