]>
Book Contents Previous Section 9.11 Complex Next Section 9.13 CycleIndicators Book Index

## 9.12 ContinuedFraction

Continued fractions have been a fascinating and useful tool in mathematics for well over three hundred years. Axiom implements continued fractions for fractions of any Euclidean domain. In practice, this usually means rational numbers. In this section we demonstrate some of the operations available for manipulating both finite and infinite continued fractions. It may be helpful if you review StreamXmpPage to remind yourself of some of the operations with streams.

The ContinuedFraction domain is a field and therefore you can add, subtract, multiply and divide the fractions.

The continuedFraction continuedFraction ContinuedFraction operation converts its fractional argument to a continued fraction.

c := continuedFraction(314159/100000)

3+1||7+1||15+1||1+1||25+1||1+1||7+1||4

Type: ContinuedFraction Integer

This display is a compact form of the bulkier

3 + 1
-------------------------------
7 + 1
---------------------------
15 + 1
----------------------
1 + 1
------------------
25 + 1
-------------
1 + 1
---------
7 + 1
-----
4

You can write any rational number in a similar form. The fraction will be finite and you can always take the ``numerators'' to be 1. That is, any rational number can be written as a simple, finite continued fraction of the form

a(1) + 1
-------------------------
a(2) + 1
--------------------
a(3) +
.
.
.
1
-------------
a(n-1) + 1
----
a(n)

The a i are called partial quotients and the operation partialQuotients partialQuotients ContinuedFraction creates a stream of them.

partialQuotients c

[3,7,15,1,25,1,7,…]

Type: Stream Integer

By considering more and more of the fraction, you get the convergents convergents ContinuedFraction. For example, the first convergent is a 1, the second is a 1+1/a 2 and so on.

convergents c

[3,22 7,333 106,355 113,9208 2931,9563 3044,76149 24239,…]

Type: Stream Fraction Integer

Since this is a finite continued fraction, the last convergent is the original rational number, in reduced form. The result of approximants approximants ContinuedFraction is always an infinite stream, though it may just repeat the ``last'' value.

approximants c

[3,22 7,333 106,355 113,9208 2931,9563 3044,76149 24239,…]

Type: Stream Fraction Integer

Inverting c only changes the partial quotients of its fraction by inserting a 0 at the beginning of the list.

pq := partialQuotients(1/c)

[0,3,7,15,1,25,1,…]

Type: Stream Integer

Do this to recover the original continued fraction from this list of partial quotients. The three-argument form of the continuedFraction continuedFraction ContinuedFraction operation takes an element which is the whole part of the fraction, a stream of elements which are the numerators of the fraction, and a stream of elements which are the denominators of the fraction.

continuedFraction(first pq,repeating [1],rest pq)

1||3+1||7+1||15+1||1+1||25+1||1+1||7

Type: ContinuedFraction Integer

The streams need not be finite for continuedFraction continuedFraction ContinuedFraction. Can you guess which irrational number has the following continued fraction? See the end of this section for the answer.

z:=continuedFraction(3,repeating [1],repeating [3,6])

3+1||3+1||6+1||3+1||6+1||3+1||6+1||3

Type: ContinuedFraction Integer

In 1737 Euler discovered the infinite continued fraction expansion

e - 1 1
----- = ---------------------
2 1 + 1
-----------------
6 + 1
-------------
10 + 1
--------
14 + ...

We use this expansion to compute rational and floating point approximations of e. For this and other interesting expansions, see C. D. Olds, Continued Fractions, New Mathematical Library, (New York: Random House, 1963), pp. 134--139.

By looking at the above expansion, we see that the whole part is 0 and the numerators are all equal to 1. This constructs the stream of denominators.

dens:Stream Integer := cons(1,generate((x+->x+4),6))

[1,6,10,14,18,22,26,…]

Type: Stream Integer

Therefore this is the continued fraction expansion for (e-1)/2.

cf := continuedFraction(0,repeating [1],dens)

1||1+1||6+1||10+1||14+1||18+1||22+1||26

Type: ContinuedFraction Integer

These are the rational number convergents.

ccf := convergents cf

[0,1,6 7,61 71,860 1001,15541 18089,342762 398959,…]

Type: Stream Fraction Integer

You can get rational convergents for e by multiplying by 2 and adding 1.

eConvergents := [2*e + 1 for e in ccf]

[1,3,19 7,193 71,2721 1001,49171 18089,1084483 398959,…]

Type: Stream Fraction Integer

You can also compute the floating point approximations to these convergents.

eConvergents :: Stream Float

[1.0,3.0,2.7142857142 857142857,2.7183098591 549295775,2.7182817182 817182817,2.7182818287 356957267,2.7182818284 585634113,…]

Type: Stream Float

Compare this to the value of e computed by the exp exp Float operation in Float.

exp 1.0

2.7182818284 590452354

Type: Float

In about 1658, Lord Brouncker established the following expansion for 4/π,

1 + 1
-----------------------
2 + 9
-------------------
2 + 25
---------------
2 + 49
-----------
2 + 81
-------
2 + ...

Let's use this expansion to compute rational and floating point approximations for π.

cf := continuedFraction(1,[(2*i+1)**2 for i in 0..],repeating [2])

1+1||2+9||2+25||2+49||2+81||2+121||2+169||2

Type: ContinuedFraction Integer
ccf := convergents cf

[1,3 2,15 13,105 76,315 263,3465 2578,45045 36979,…]

Type: Stream Fraction Integer
piConvergents := [4/p for p in ccf]

[4,8 3,52 15,304 105,1052 315,10312 3465,147916 45045,…]

Type: Stream Fraction Integer

As you can see, the values are converging to π=3.14159265358979323846..., but not very quickly.

piConvergents :: Stream Float

[4.0,2.6666666666 666666667,3.4666666666 666666667,2.8952380952 380952381,3.3396825396 825396825,2.9760461760 461760462,3.2837384837 384837385,…]

Type: Stream Float

You need not restrict yourself to continued fractions of integers. Here is an expansion for a quotient of Gaussian integers.

continuedFraction((- 122 + 597*%i)/(4 - 4*%i))

-90+59 i+1||1-2 i+1||-1+2 i

Type: ContinuedFraction Complex Integer

This is an expansion for a quotient of polynomials in one variable with rational number coefficients.

r : Fraction UnivariatePolynomial(x,Fraction Integer)
Type: Void
r := ((x - 1) * (x - 2)) / ((x-3) * (x-4))

x 2-3 x+2 x 2-7 x+12

Type: Fraction UnivariatePolynomial(x,Fraction Integer)
continuedFraction r

1+1||1 4 x-9 8+1||16 3 x-40 3

Type: ContinuedFraction UnivariatePolynomial(x,Fraction Integer)

To conclude this section, we give you evidence that

z = 3 + 1
-----------------------
3 + 1
-------------------
6 + 1
---------------
3 + 1
-----------
6 + 1
-------
3 + ...

is the expansion of 11.

[i*i for i in convergents(z) :: Stream Float]

[9.0,11.1111111111 11111111,10.9944598337 9501385,11.0002777777 77777778,10.9999860763 98799786,11.0000006979 29731039,10.9999999650 15834446,…]

Type: Stream Float
Book Contents Previous Section 9.11 Complex Next Section 9.13 CycleIndicators Book Index