The Brouncker algorithm for the continued fractions of a root of a quadratic.

>    with(linalg):

To start, we create a matrix from   x^2-7*y^2

>    A:=matrix(2,4,[1,0,1,0,0,1,0,-7]);

>   

A := matrix([[1, 0, 1, 0], [0, 1, 0, -7]])

The function "fun" produces a polynomial in x,y from a 2 x 2 matrix. We need its values to calculate  steps.

>   

>    fun:=m-> unapply(m[1,3]*x^2+2*m[1,4]*x*y+m[2,4]*y^2,x,y);

fun := m -> unapply(m[1,3]*x^2+2*m[1,4]*x*y+m[2,4]*y^2,x,y)

>    fun(A)(x,y);fun(A)(3,1);

x^2-7*y^2

2

The function ch(m,t,n) changes matrix m as follows:

If n=1 then it add t times row 1 to row 2.

If n=2 it adds t times row 2 to row 1.

Then it makes  a symmetric matrix operation on the last two columns.

>    ch:=proc(m,t,n) local tmp:
tmp:=copy(m); if n=1 then  tmp:=addrow(tmp,1,2,t); tmp:=addcol(tmp,3,4,t);
elif n=2 then  tmp:=addrow(tmp,2,1,t);tmp:=addcol(tmp,4,3,t);fi;op(tmp);end:

>   

We start work on x^2-7*y^2  .

We shall record the t-values as we proceed.

>    A:=matrix(2,4,[1,0,1,0,0,1,0,-7]);

A := matrix([[1, 0, 1, 0], [0, 1, 0, -7]])

We check the function at (1,1);

If it is less than 1,  then we try values at (t,1) and stop just before the values change sign.

>    fun(A)(1,1); fun(A)(2,1);fun(A)(3,1);

-6

-3

2

So set t=2 and n=1(because the value at (1,1) was less than 1.)

>    tvals:=[2];

tvals := [2]

>    A1:=ch(A,2,1);

A1 := matrix([[1, 0, 1, 2], [2, 1, 2, -3]])

Repeat with new matrix. Here, the value at (1,1) is bigger than 1 , so we try (1,t), until sign changes.

>    fun(A1)(1,1);fun(A1)(1,2);

2

-3

So we set t=1 and n=2 (because the value at (1,1) was bigger  than 1.)

>    tvals:=[op(tvals),1];

tvals := [2, 1]

>    A2:=ch(A1,1,2);

A2 := matrix([[3, 1, 2, -1], [2, 1, -1, -3]])

Repeat! Again, we get t=1, n=1.

>    fun(A2)(1,1);fun(A2)(2,1);

-3

1

>    tvals:=[op(tvals),1];

tvals := [2, 1, 1]

>    A3:=ch(A2,1,1);

A3 := matrix([[3, 1, 2, 1], [5, 2, 1, -3]])

This is special!  

We have the f(1,1) value exactly equal to 1 . In this case, we set t=1 and n equal to the opposite of before, so n=2.

>    fun(A3)(1,1);

1

>    tvals:=[op(tvals),1];

tvals := [2, 1, 1, 1]

>    A4:=ch(A3,1,2);

A4 := matrix([[8, 3, 1, -2], [5, 2, -2, -3]])

>    fun(A4)(1,1);fun(A4)(2,1);fun(A4)(3,1);fun(A4)(4,1);fun(A4)(5,1);

-6

-7

-6

-3

2

Now we get t=4, n=1.

>    tvals:=[op(tvals),4];

tvals := [2, 1, 1, 1, 4]

>    A5:=ch(A4,4,1);

A5 := matrix([[8, 3, 1, 2], [37, 14, 2, -3]])

>    seq(cat(A,i)=op(cat(A,i)),i=1..5);

A1 = matrix([[1, 0, 1, 2], [2, 1, 2, -3]]), A2 = matrix([[3, 1, 2, -1], [2, 1, -1, -3]]), A3 = matrix([[3, 1, 2, 1], [5, 2, 1, -3]]), A4 = matrix([[8, 3, 1, -2], [5, 2, -2, -3]]), A5 = matrix([[8, 3, 1...

Note that A1 and A4 have a  repeat of the second part .  So, now the pattern repeats!

>    convert(sqrt(7), confrac,8,ans7);

[2, 1, 1, 1, 4, 1, 1, 1]

>    ans7;

[2, 3, 5/2, 8/3, 37/14, 45/17, 82/31, 127/48]

>   

Case of sqrt(11)

>   

Case of sqrt(13)

Now try yet another, sqrt(13)

>    A:=matrix(2,4,[1,0,1,0,0,1,0,-13]);tvals:=[];

A := matrix([[1, 0, 1, 0], [0, 1, 0, -13]])

tvals := []

>    fun(A)(1,1);

-12

>    seq([t,fun(A)(t,1)],t=1..5);

[1, -12], [2, -9], [3, -4], [4, 3], [5, 12]

>    tvals:=[op(tvals),3];

tvals := [3]

>    A1:=ch(A,3,1);

A1 := matrix([[1, 0, 1, 3], [3, 1, 3, -4]])

>    fun(A1)(1,1);

3

>    seq([t,fun(A1)(1,t)],t=1..5);

[1, 3], [2, -3], [3, -17], [4, -39], [5, -69]

>    tvals:=[op(tvals),1];A2:=ch(A1,1,2);

tvals := [3, 1]

A2 := matrix([[4, 1, 3, -1], [3, 1, -1, -4]])

>    fun(A2)(1,1);

-3

>    seq([t,fun(A2)(t,1)],t=1..8);

[1, -3], [2, 4], [3, 17], [4, 36], [5, 61], [6, 92], [7, 129], [8, 172]

>    tvals:=[op(tvals),1];A3:=ch(A2,1,1);

tvals := [3, 1, 1]

A3 := matrix([[4, 1, 3, 2], [7, 2, 2, -3]])

>    fun(A3)(1,1);

4

>    seq([t,fun(A3)(1,t)],t=1..8);

[1, 4], [2, -1], [3, -12], [4, -29], [5, -52], [6, -81], [7, -116], [8, -157]

>    tvals:=[op(tvals),1];A4:=ch(A3,1,2);

tvals := [3, 1, 1, 1]

A4 := matrix([[11, 3, 4, -1], [7, 2, -1, -3]])

>    fun(A4)(1,1);

-1

>    seq([t,fun(A4)(t,1)],t=1..8);

[1, -1], [2, 9], [3, 27], [4, 53], [5, 87], [6, 129], [7, 179], [8, 237]

>    tvals:=[op(tvals),1];A5:=ch(A4,1,1);

tvals := [3, 1, 1, 1, 1]

A5 := matrix([[11, 3, 4, 3], [18, 5, 3, -1]])

>    fun(A5)(1,1);

9

>    seq([t,fun(A5)(1,t)],t=1..8);

[1, 9], [2, 12], [3, 13], [4, 12], [5, 9], [6, 4], [7, -3], [8, -12]

>    tvals:=[op(tvals),6];A6:=ch(A5,6,2);

tvals := [3, 1, 1, 1, 1, 6]

A6 := matrix([[119, 33, 4, -3], [18, 5, -3, -1]])

>    fun(A6)(1,1);

-3

>    seq([t,fun(A6)(t,1)],t=1..8);

[1, -3], [2, 3], [3, 17], [4, 39], [5, 69], [6, 107], [7, 153], [8, 207]

>    tvals:=[op(tvals),1];A7:=ch(A6,1,1);

tvals := [3, 1, 1, 1, 1, 6, 1]

A7 := matrix([[119, 33, 4, 1], [137, 38, 1, -3]])

>    fun(A7)(1,1);

>    seq([t,fun(A7)(1,t)],t=1..8);

3

[1, 3], [2, -4], [3, -17], [4, -36], [5, -61], [6, -92], [7, -129], [8, -172]

>    tvals:=[op(tvals),1];A8:=ch(A7,1,2);

tvals := [3, 1, 1, 1, 1, 6, 1, 1]

A8 := matrix([[256, 71, 3, -2], [137, 38, -2, -3]])

>    fun(A8)(1,1);

-4

>    seq([t,fun(A8)(t,1)],t=1..8);

[1, -4], [2, 1], [3, 12], [4, 29], [5, 52], [6, 81], [7, 116], [8, 157]

>    tvals:=[op(tvals),1];A9:=ch(A8,1,1);

tvals := [3, 1, 1, 1, 1, 6, 1, 1, 1]

A9 := matrix([[256, 71, 3, 1], [393, 109, 1, -4]])

>    fun(A9)(1,1);##  Special Case

1

>    tvals:=[op(tvals),1];A10:=ch(A9,1,2);

tvals := [3, 1, 1, 1, 1, 6, 1, 1, 1, 1]

A10 := matrix([[649, 180, 1, -3], [393, 109, -3, -4]])

>    fun(A10)(1,1);

-9

>    seq([t,fun(A10)(t,1)],t=1..8);

[1, -9], [2, -12], [3, -13], [4, -12], [5, -9], [6, -4], [7, 3], [8, 12]

>   

>    tvals:=[op(tvals),6];A11:=ch(A10,6,1);

tvals := [3, 1, 1, 1, 1, 6, 1, 1, 1, 1, 6]

A11 := matrix([[649, 180, 1, 3], [4287, 1189, 3, -4]])

>    seq(cat(A,i)=op(cat(A,i)),i=1..11);

A1 = matrix([[1, 0, 1, 3], [3, 1, 3, -4]]), A2 = matrix([[4, 1, 3, -1], [3, 1, -1, -4]]), A3 = matrix([[4, 1, 3, 2], [7, 2, 2, -3]]), A4 = matrix([[11, 3, 4, -1], [7, 2, -1, -3]]), A5 = matrix([[11, 3,...
A1 = matrix([[1, 0, 1, 3], [3, 1, 3, -4]]), A2 = matrix([[4, 1, 3, -1], [3, 1, -1, -4]]), A3 = matrix([[4, 1, 3, 2], [7, 2, 2, -3]]), A4 = matrix([[11, 3, 4, -1], [7, 2, -1, -3]]), A5 = matrix([[11, 3,...

Note that A11 has same second part as A1. So, now we repeat.

3

>    unassign('ans13');convert(sqrt(13),confrac,15,ans13);

[3, 1, 1, 1, 1, 6, 1, 1, 1, 1, 6, 1, 1, 1, 1]

>    ans13;

[3, 4, 7/2, 11/3, 18/5, 119/33, 137/38, 256/71, 393/109, 649/180, 4287/1189, 4936/1369, 9223/2558, 14159/3927, 23382/6485]

>   

>   

>