%
% Sum a series for pi until it has better accuracy than 355/113.
%
%  Using exact arithmetic, you need at least 3,748,630 terms.
%
%  Reference:
%
%    John D Cook,
%    Asimov's question about pi,
%    https://www.johndcook.com/blog/
%    4 September 2019
%
%    Isaac Asimov,
%    Asimov on Numbers,
%    1977
%    
  x1 = 355.0 / 113.0;
  e1 = abs ( pi - x1 );
%
%  Sum terms until error is less than e.
%
  k = 0;
  s = 1.0;
  x2 = 0.0;
  while ( true )
    t = s * 4.0 / ( 2 * k + 1 );
    x2 = x2 + t;
    e2 = abs ( pi - x2 );
    s = - s;
    k = k + 1;
    if ( e2 <= e1 )
      break;
    end
  end

  fprintf ( 1, '  Pi      = %20.16g\n', pi );
  fprintf ( 1, '  355/113 = %20.16g, ||Pi-22/7||   = %e\n', x1, e1 );
  fprintf ( 1, '  Series  = %20.16g, ||Pi-Series|| = %e\n', x2, e2 );
  fprintf ( 1, '  Summed %d terms\n', k );

