% Script P2_1_1
% Pi via summation

clc
disp('   n     T_error      RT_error ')
disp('----------------------------------')

R= 0;  T= 0;
nMax= 1000;
rSign= 1;  % sign of one term in the R series: +1 or -1

for n= 1:nMax
    T= T + 1/n^2;
    R= R + rSign/(2*n-1);
    rSign= -rSign;  % sign changes for each term
    
    if (rem(n,100)==0)
        T_err= abs( sqrt(6*T) - pi);
        R_err= abs( 4*R - pi);
        disp(sprintf('%5d  %10.1e   %10.1e ', n, T_err, R_err))
    end
end