MAPLE output for the Gauss-Legendre Method for estimating Pi.

Let's just run the loop one time and see what happens...

> restart; a:=1; b:=1/sqrt(2); t:=1/4; x:=1;

a := 1

b := 1/2*sqrt(2)

t := 1/4

x := 1

> for i from 1 to 1 do

> y:=a;

> a:=(a+b)/2;

> b:=sqrt(b*y);

> t:=t-x*(y-a)^2;

> x:=2*x;

> od;

y := 1

a := 1/2+1/4*sqrt(2)

b := 1/2*2^(3/4)

t := 1/4-(1/2-1/4*sqrt(2))^2

x := 2

> evalf(a); evalf(b); evalf(t); evalf(x);

.8535533905

.8408964155

.2285533906

2.

> evalf(((a+b)^2)/(4*t));

3.140579250

This is not a bad estimate for Pi, but let's run the loop for two iterations (this time I will hide all of the output except the answer):

> restart; a:=1: b:=1/sqrt(2): t:=1/4: x:=1:

> for i from 1 to 2 do

> y:=a:

> a:=(a+b)/2:

> b:=sqrt(b*y):

> t:=t-x*(y-a)^2:

> x:=2*x:

> od:

> evalf(((a+b)^2)/(4*t));

3.141592648

Pretty good, we are correct to 7 places already! Let's iterate three times and check out our error.

> restart; a:=1: b:=1/sqrt(2): t:=1/4: x:=1:

> for i from 1 to 3 do

> y:=a:

> a:=(a+b)/2:

> b:=sqrt(b*y):

> t:=t-x*(y-a)^2:

> x:=2*x:

> od:

> evalf(((a+b)^2)/(4*t)); evalf(Pi-%);

3.141592655

-.1e-8

Wow! Look at that precision. Let's see what iterating four times looks like.

> restart; a:=1: b:=1/sqrt(2): t:=1/4: x:=1:

> for i from 1 to 4 do

> y:=a:

> a:=(a+b)/2:

> b:=sqrt(b*y):

> t:=t-x*(y-a)^2:

> x:=2*x:

> od:

> evalf(((a+b)^2)/(4*t));

3.141592655

With this many digits, there is no difference from the last output, let's increase our number of digits and look at our error.

> Digits:=20:

> evalf(((a+b)^2)/(4*t)); evalf(Pi-%);

3.1415926535897932382

.3e-18

After only four iterations (and with very minimal computation time) we have Pi correct to 18 places!