% sum over a series to calculate pi.

tic;

n = 500000;
total = 0.0;
k = 0;

for id =1:2:n
    k = k + 1;
    if mod(k,2)==0   tmp = -1.0/double(id);
    else  tmp = 1.0/double(id);
    end

    total = total + tmp;
end

  total = 4.0 * total;
  fprintf('%15.12f', total);

toc;

