function a = r8_to_i4scf ( r, n )

%*****************************************************************************80
%
%% r8_to_i4scf() approximates an R8 with an I4 simple continued fraction.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    04 August 2017
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Norman Richert,
%    Strang's Strange Figures,
%    American Mathematical Monthly,
%    Volume 99, Number 2, February 1992, pages 101-107.
%
%  Input:
%
%    real R, the real value.
%
%    integer N, the number of convergents to compute.
%
%  Output:
%
%    integer A(1:N+1), the partial quotients.
%
  a = zeros ( 1, n + 1 );

  if ( r == 0.0 )
    return
  end

  r2 = r;
%
%  FIX(R2) rounds towards zero.  
%  So negative R2 should be handled correctly.
%
  a(1) = fix ( r2 );

  for i = 1 : n
    r2 = 1.0 / ( r2 - a(i) );
    a(i+1) = fix ( r2 );
  end

  return
end
