[ create a new paste ] login | about

Link: http://codepad.org/1hoOcaT5    [ raw code | output | fork ]

D, pasted on Jul 7:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import std.cstream;
import std.math;

void main() {
    int N = 10;
    real[] a = new real[N], b = new real[N], t = new real[N], pi = new real[N];
    a[0] = 1.0;
    b[0] = 1.0 / sqrt(2.0);
    t[0] = 1.0 / 4.0;
    for(int i=0; i<N-1; i++) {
        a[i+1] = (a[i] + b[i]) / 2.0;
        b[i+1] = sqrt(a[i] * b[i]);
        t[i+1] = t[i] - pow(2.0, cast(real)i) * pow(a[i] - a[i+1], 2.0);
    }
    for(int i=0; i<N; i++) {
        dout.writefln("%40.37f", pow(a[i] + b[i], 2.0) / (4.0 * t[i]));
    }
    dout.writefln("%40.37f", real.epsilon);
} 


Output:
1
2
3
4
5
6
7
8
9
10
11
 2.9142135623730950487637880730318329370
 3.1405792505221682487433904062967826576
 3.1415926462135422823386543544899041080
 3.1415926535897932385128089594061862044
 3.1415926535897932387296493939032870912
 3.1415926535897932387296493939032870912
 3.1415926535897932387296493939032870912
 3.1415926535897932387296493939032870912
 3.1415926535897932387296493939032870912
 3.1415926535897932387296493939032870912
 0.0000000000000000001084202172485504434


Create a new paste based on this one


Comments: