function r = vdc_sequence ( i1, i2 )

%*****************************************************************************80
%
%% vdc_sequence() computes a sequence of elements of the van der Corput sequence.
%
%  Discussion:
%
%    This function could be rewritten to take advantage of MATLAB's
%    vectorization capabilities.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 August 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    Input, integer I1, I2, the indices of the first and last elements.
%
%  Output:
%
%    Output, real R(|I2+1-I1|), elements I1 through I2 of the van der
%    Corput sequence.
%
  n = abs ( i2 - i1 ) + 1;
  r = zeros ( n, 1 );

  if ( i1 <= i2 )
    i3 = +1;
  else
    i3 = -1;
  end

  j = 0;

  for i = i1 : i3 : i2

    j = j + 1;   
%
%  Isolate the sign, and only work with the integer part of I.
%
    s = sign ( i );
    t = abs ( floor ( i ) );
%
%  Carry out the computation.
%
    base_inv = 0.5;

    r(j) = 0.0;

    while ( t ~= 0 )
      digit = mod ( t, 2 );
      r(j) = r(j) + digit * base_inv;
      base_inv = base_inv / 2.0;
      t = floor ( t / 2 );
    end
%
%  Recover the sign.
%
    r(j) = r(j) * s;

  end

  return
end

