function u = lollatz_permutation ( tset )

%*****************************************************************************80
%
%% lollatz_permutation() computes the "Lollatz" permutation for given values.
%
%  Discussion:
%
%    Let T be an entry in the input set.
%
%    If mod(T,3) == 0
%      U = 2*T/3
%    Else if mod(T,3) == 1
%      U = (4*T-1)/3
%    Else if mod(T,3) == 2
%      U = (4*T+1)/3
%
%    Note that this is actually the first transformation function that
%    Lothar Collatz is known to have considered, as recorded in his
%    notebook for 1 July 1932.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer T(N): the starting values.
%
%  Output:
%
%    integer U(N): the permuted values.
%
  n = length ( tset );
  u = zeros ( n, 1 );
  i = 0;

  for t = tset

    if ( mod ( t, 3 ) == 0 )
      t = 2 * t / 3;
    elseif ( mod ( t, 3 ) == 1 )
      t = ( 4 * t - 1 ) / 3;
    elseif ( mod ( t, 3 ) == 2 )
      t = ( 4 * t + 1 ) / 3;
    end

    i = i + 1;
    u(i) = t;

  end

  return
end
