Continued Fraction

DOWNLOAD Mathematica Notebook EXPLORE THIS TOPIC IN the MathWorld Classroom Contribute to this entry

A "general" continued fraction representation of a real number x is one of the form

 x=a_0+(b_1)/(a_1+(b_2)/(a_2+(b_3)/(a_3+...))),
(1)

where a_0, a_1, ... and b_1, b_2 are integers. Such representations are sometimes written in the form

 x=a_0+(b_1)/(a_1+)(b_2)/(a_2+)...
(2)

for compactness.

Wallis first used the term "continued fraction" in his Arithmetica infinitorum of 1653 (Havil 2003, p. 93), although other sources list the publication date as 1655 or 1656. An archaic word for a continued fraction is anthyphairetic ratio.

While continued fractions are not the only possible representation of real numbers in terms of a sequence of integers (others include the decimal expansion and the so-called Engel expansion), they are a very common such representation that arises most frequently in number theory.

The simple continued fraction representation (which is usually what is meant when the term "continued fraction" is used without qualification) of a number x is given by

 x=a_0+1/(a_1+1/(a_2+1/(a_3+...))),
(3)

where a_0, a_1, a_2, ... are again integers and a_1,a_2,...>0. Simple continued fractions can be written in a compact abbreviated notation as

 x=[a_0,a_1,a_2,a_3,...].
(4)

Some care is needed, since some authors begin indexing the terms at a_1 instead of a_0, causing the parity of certain fundamental results in continued fraction theory to be reversed. The first n terms of the simple continued fraction of a number x can be computed in the Wolfram Language using the command ContinuedFraction[x, n].

Continued fractions with closed forms are given in the following table (cf. Euler 1775).

continued fractionvalueapproximateSloane
0+1/(1+1/(2+1/(3+1/(4+...))))(I_1(2))/(I_0(2))0.697774...A052119
1+2/(3+4/(5+6/(7+8/(9+...))))(sqrt(e)-1)^(-1)1.541494...A113011
1/(1+2/(2+3/(3+4/(4+...))))(e-1)^(-1)0.581976...A073333
1+1/(1+2/(1+3/(1+4/(1+...))))sqrt(2/(epi))[erfc(2^(-1/2))]^(-1)1.525135...A111129

Starting the indexing of a continued fraction with a_0,

 a_0=|_x_|
(5)

is the integer part of x, where |_x_| is the floor function,

 a_1=|_1/(x-a_0)_|
(6)

is the integral part of the reciprocal of x-a_0,

 a_2=|_1/(1/(x-a_0)-a_1)_|
(7)

is the integral part of the reciprocal of the remainder, etc. Writing the remainders according to the recurrence relation

r_0=x
(8)
r_n=1/(r_(n-1)-a_(n-1))
(9)

gives the concise formula

 a_n=|_r_n_|.
(10)

The quantities a_n are called partial quotients, and the quantity obtained by including n terms of the continued fraction

c_n=(p_n)/(q_n)
(11)
=[a_0,a_1,...,a_n]
(12)
=a_0+1/(a_1+1/(a_2+1/(...+1/(a_n))))
(13)

is called the nth convergent. For example, consider the computation of the continued fraction of pi, given by pi=[3,7,15,1,292,1,1,...].

termvaluePQsconvergentvalue
a_0|_pi_|=3[3]33.00000
a_1|_1/(pi-3)_|=7[3,7](22)/73.14286
a_2|_1/(1/(pi-3)-7)_|=15[3,7,15](333)/(106)3.14151

Continued fractions provide, in some sense, a series of "best" estimates for an irrational number. Functions can also be written as continued fractions, providing a series of better and better rational approximations. Continued fractions have also proved useful in the proof of certain properties of numbers such as e and pi (pi). Because quadratic surds have periodic continued fractions (e.g., Pythagoras's constant sqrt(2)=1.41421356... has continued fraction [1, 2, 2, 2, 2, ...]), an exact representation for a tabulated numerical value can sometimes be found if it is suspected to represent an unknown quadratic surd.

Continued fractions are also useful for finding near commensurabilities between events with different periods. For example, the Metonic cycle used for calendrical purposes by the Greeks consists of 235 lunar months which very nearly equal 19 solar years, and 235/19 is the sixth convergent of the ratio of the lunar phase (synodic) period and solar period (365.2425/29.53059). Continued fractions can also be used to calculate gear ratios, and were used for this purpose by the ancient Greeks (Guy 1990).

Let the continued fraction for x be written [a_0;a_1,...,a_n]. Then the limiting value is almost always Khinchin's constant

 K=lim_(n->infty)(a_1a_2...a_n)^(1/n)=2.68545...
(14)

(OEIS A002210).

Similarly, taking the nth root of the denominator q_n of the nth convergent as n->infty almost always gives the Lévy constant

 lim_(n->infty)q_n^(1/n)=e^(pi^2/(12ln2))=3.275823...
(15)

(OEIS A086702).

Let P_n/Q_n be convergents of a nonsimple continued fraction. Then

P_(-1)=1    Q_(-1)=0
(16)
P_0=a_0    Q_0=1
(17)

and subsequent terms are calculated from the recurrence relations

P_j=a_jP_(j-1)+b_jP_(j-2)
(18)
Q_j=a_jQ_(j-1)+b_jQ_(j-2)
(19)

for j=1, 2, ..., n. It is also true that

 P_nQ_(n-1)-P_(n-1)Q_n=(-1)^(n-1)product_(k=1)^nb_k.
(20)

The error in approximating a number by a given convergent is roughly the multiplicative inverse of the square of the denominator of the first neglected term.

A finite simple continued fraction representation terminates after a finite number of terms. To "round" a continued fraction, truncate the last term unless it is +/-1, in which case it should be added to the previous term (Gosper 1972, Item 101A). To take one over a continued fraction, add (or possibly delete) an initial 0 term. To negate, take the negative of all terms, optionally using the identity

 [-a,-b,-c,-d,...]=[-a-1,1,b-1,c,d,...].
(21)

A particularly beautiful identity involving the terms of the continued fraction is

 ([a_0,a_1,...,a_n])/([a_0,a_1,...,a_(n-1)])=([a_n,a_(n-1),...,a_1,a_0])/([a_n,a_(n-1),...,a_1]).
(22)

Finite simple fractions represent rational numbers and all rational numbers are represented by finite continued fractions. There are two possible representations for a finite simple fraction:

 [a_0,...,a_n]={[a_0,...,a_(n-1),a_n-1,1]   for a_n>1; [a_0,...,a_(n-2),a_(n-1)+1]   for a_n=1.
(23)

On the other hand, an infinite simple fraction represents a unique irrational number, and each irrational number has a unique infinite continued fraction.

Consider the convergents c_n=p_n/q_n of a simple continued fraction, and define

 p_(-2)=0    q_(-2)=1
(24)
 p_(-1)=1    q_(-1)=0
(25)
 p_0=a_0    q_0=1.
(26)

Then subsequent terms can be calculated from the recurrence relations

 p_n=a_np_(n-1)+p_(n-2)
(27)
 q_n=a_nq_(n-1)+q_(n-2).
(28)

The continued fraction fundamental recurrence relation for simple continued fractions is

 p_nq_(n-1)-p_(n-1)q_n=(-1)^(n+1).
(29)

It is also true that if a_0!=0,

(p_n)/(p_(n-1))=[a_n,a_(n-1),...,a_0]
(30)
(q_n)/(q_(n-1))=[a_n,...,a_1].
(31)

Furthermore,

 (p_n)/(q_n)=(p_(n+1)-p_(n-1))/(q_(n+1)-q_(n-1)).
(32)

Also, if a convergent c_n=p_n/q_n>1, then

 (q_n)/(p_n)=[0,a_0,a_1,...,a_n].
(33)

Similarly, if c_n=p_n/q_n<1, then a_0=0 and

 (q_n)/(p_n)=[a_1,...,a_n].
(34)

The convergents c_n=p_n/q_n also satisfy

c_n-c_(n-1)=((-1)^(n+1))/(q_nq_(n-1))
(35)
c_n-c_(n-2)=(a_n(-1)^n)/(q_nq_(n-2)).
(36)
CFConvergents

Plotted above on semilog scales are c_n-pi (n even; left figure) and pi-c_n (n odd; right figure) as a function of n for the convergents of pi. In general, the even convergents c_(2n) of an infinite simple continued fraction for a number x form an increasing sequence, and the odd convergents c_(2n+1) form a decreasing sequence (so any even convergent is less than any odd convergent). Summarizing,

 c_0<c_2<c_4<...<c_(2n-2)<c_(2n)<...<x
(37)
 x<...<c_(2n+1)<c_(2n-1)<c_5<c_3<c_1.
(38)

Furthermore, each convergent for n>=3 lies between the two preceding ones. Each convergent is nearer to the value of the infinite continued fraction than the previous one. In addition, for a number x=[a_0;a_1,...],

 1/((a_(n+1)+2)q_n^2)<|x-(p_n)/(q_n)|<1/(a_(n+1)q_n^2).
(39)

The square root of a squarefree integer has a periodic continued fraction of the form

 sqrt(n)=[a_0,a_1,...,a_n,2a_0^_]
(40)

(Rose 1994, p. 130). Furthermore, if D is not a square number, then the terms of the continued fraction of sqrt(D) satisfy

 0<a_n<2sqrt(D).
(41)

Logarithms log_(b_1)b_0 can be computed by defining b_2, ... and the positive integer n_1, ... such that

 b_1^(n_1)<b_0<b_1^(n_1+1)    b_2=(b_0)/(b_1^(n_1))
(42)
 b_2^(n_2)<b_1<b_2^(n_2+1)    b_3=(b_1)/(b_2^(n_2))
(43)

and so on. Then

 log_(b_1)b_0=[n_1,n_2,n_3,...].
(44)
ContinuedFractionLattice

A geometric interpretation for a reduced fraction y/x consists of a string through a lattice of points with ends at (1,0) and (x,y) (Klein 1896, 1932; Steinhaus 1999, p. 40; Gardner 1984, pp. 210-211, Ball and Coxeter 1987, pp. 86-87; Davenport 1992). This interpretation is closely related to a similar one for the greatest common divisor. The pegs it presses against (x_i,y_i) give alternate convergents y_i/x_i, while the other convergents are obtained from the pegs it presses against with the initial end at (0,1). The above plot is for e-2, which has convergents 0, 1, 2/3, 3/4, 5/7, ....

Continued fractions can be used to express the positive roots of any polynomial equation. Continued fractions can also be used to solve linear Diophantine equations and the Pell equation. Euler showed that if a convergent series can be written in the form

 c_1+c_1c_2+c_1c_2c_3+...,
(45)

then it is equal to the continued fraction

 (c_1)/(1-(c_2)/(1+c_2-(c_3)/(1+c_3-...)))
(46)

(Borwein et al. 2004, p. 30).

Gosper has invented an algorithm for performing analytic addition, subtraction, multiplication, and division using continued fractions. It requires keeping track of eight integers which are conceptually arranged at the polyhedron vertices of a cube. Although this algorithm has not appeared in print, similar algorithms have been constructed by Vuillemin (1987) and Liardet and Stambul (1998).

Gosper's algorithm for computing the continued fraction for (ax+b)/(cx+d) from the continued fraction for x is described by Gosper (1972), Knuth (1998, Exercise 4.5.3.15, pp. 360 and 601), and Fowler (1999). (In line 9 of Knuth's solution, X_k<-|_A/C_| should be replaced by X_k<-min(|_A/C_|,|_(A+B)/(C+D)_|).) Gosper (1972) and Knuth (1981) also mention the bivariate case (axy+bx+cy+d)/(Axy+Bx+Cy+D).

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.