clear
close all



s=single(0);
i=1;

while ( s+1.0/i > s)
s=s+1.0/i;
i=i+1;
end;

fprintf('sum of 1/n = %f at i=%d \n',s,i)


% backward for better precision
s=single(0);
for i=10000000:-1:1
 s=s+1/i;
end


fprintf('backward sum of 1/n = %f \n',s)



s=single(0);
i=1;

while ( s+1.0/(i*i) > s)
s=s+1.0/(i*i);
i=i+1;
end;

fprintf('sum of 1/n^2 = %f at i=%d \n',s,i)


%>> a6_sums
%sum of 1/n = 15.403683 at i=2097152 
%backward sum of 1/n = 16.686031 
%sum of 1/n^2 = 1.644725 at i=4097 

% Exact values 
%  sum (1/n)   = infinity
%
%  "Basler Problem"
%  sum (1/n^2) =  pi^2 / 6  ~=~ 1.644934
%
