% Newton's formula for computing pi

disp('First, the James Gregory formula');
k = 0;
x = 4;
fprintf('%d step: approximation = %16.15f\n', k, x);
for k=1:10;
    x = x + 4 * (-1)^k / (2*k+1);
    fprintf('%d step: approximation = %16.15f\n', k, x);
end
fprintf('Very slow convergence!\n\n');
disp('Second, we use the Beeler et. al. (1972) formula');
k = 0;
x = 2;
fprintf('%d step: approximation = %16.15f\n', k, x);
for k=1:10
    x = x + 2 * 2^k * (factorial(k))^2 / factorial(2*k+1);
    fprintf('%d step: approximation = %16.15f\n', k, x);
end

fprintf('\n');
disp('Third, we try the Ramanujan formula');
k = 0;
temp = 1103;
x = 9801/2/sqrt(2)/temp;
fprintf('%d step: approximation = %16.15f\n', k, x);
for k=1:10
    temp = temp + factorial(4*k) * (1103+26390*k) / (factorial(k))^4 / 396^(4*k);
    x = 9801/2/sqrt(2)/temp;
    fprintf('%d step: approximation = %16.15f\n', k, x);
end

fprintf('\nHow about more output digits in the Ramanujan formula?\n');
pause
disp('Second, we try the Ramanujan formula');
k = 0;
temp = 1103;
x = 9801/2/sqrt(2)/temp;
fprintf('%d step: approximation = %16.15f\n', k, x);
for k=1:10
    temp = temp + factorial(4*k) * (1103+26390*k) / (factorial(k))^4 / 396^(4*k);
    x = 9801/2/sqrt(2)/temp;
    fprintf('%d step: approximation = %24.23f\n', k, x);
end
fprintf('\nWhy? Think about it.\n');
First, the James Gregory formula
0 step: approximation = 4.000000000000000
1 step: approximation = 2.666666666666667
2 step: approximation = 3.466666666666667
3 step: approximation = 2.895238095238096
4 step: approximation = 3.339682539682540
5 step: approximation = 2.976046176046176
6 step: approximation = 3.283738483738484
7 step: approximation = 3.017071817071818
8 step: approximation = 3.252365934718877
9 step: approximation = 3.041839618929403
10 step: approximation = 3.232315809405594
Very slow convergence!

Second, we use the Beeler et. al. (1972) formula
0 step: approximation = 2.000000000000000
1 step: approximation = 2.666666666666667
2 step: approximation = 2.933333333333333
3 step: approximation = 3.047619047619047
4 step: approximation = 3.098412698412698
5 step: approximation = 3.121500721500721
6 step: approximation = 3.132156732156732
7 step: approximation = 3.137129537129537
8 step: approximation = 3.139469680646151
9 step: approximation = 3.140578169680336
10 step: approximation = 3.141106021601377

Third, we try the Ramanujan formula
0 step: approximation = 3.141592730013306
1 step: approximation = 3.141592653589794
2 step: approximation = 3.141592653589793
3 step: approximation = 3.141592653589793
4 step: approximation = 3.141592653589793
5 step: approximation = 3.141592653589793
6 step: approximation = 3.141592653589793
7 step: approximation = 3.141592653589793
8 step: approximation = 3.141592653589793
9 step: approximation = 3.141592653589793
10 step: approximation = 3.141592653589793

How about more output digits in the Ramanujan formula?
Second, we try the Ramanujan formula
0 step: approximation = 3.141592730013306
1 step: approximation = 3.14159265358979400417638
2 step: approximation = 3.14159265358979311599796
3 step: approximation = 3.14159265358979311599796
4 step: approximation = 3.14159265358979311599796
5 step: approximation = 3.14159265358979311599796
6 step: approximation = 3.14159265358979311599796
7 step: approximation = 3.14159265358979311599796
8 step: approximation = 3.14159265358979311599796
9 step: approximation = 3.14159265358979311599796
10 step: approximation = 3.14159265358979311599796

Why? Think about it.