function hail4 ( n )

%% HAIL4 does a double find, which requires indexing the index.

  tic

  x = linspace ( 1, n, n );
  y = x;
  z = zeros ( n, 1 );

  while ( any ( y ~= 1 ) )
%
%  Do the FIND in two steps.
%  I finds all the values that are not 1.
%
    i = find ( y ~= 1 );

    z(i) = z(i) + 1;
%
%  J finds all the values in I that are even.
%  K finds all the values in I that are odd.
%
    j = find ( mod ( y(i), 2 ) == 0 );
    k = find ( mod ( y(i), 2 ) == 1 );
%
%  I(J) is all the values that are not one, and are even.
%  I(K) is all the values that are not one, and are odd.
%
    y(i(j)) = y(i(j)) / 2;
    y(i(k)) = 3 * y(i(k)) + 1; 

  end

  figure ( 4 )
  plot ( x, z, 'r*' )
  title ( 'HAIL4' )

  toc

  return
end
