function s = nollatz ( key )

%*****************************************************************************80
%
%% nollatz() computes the "Nollatz" sequence for a given starting point.
%
%  Discussion:
%
%    The Nollatz sequence is defined recursively as follows:
%
%      Let T be the current entry of the sequence, and U the next:
%
%      If T = 1, the sequence terminates (or U = 1, your choice);
%      Else if T is even, U = T / 2;
%      Else (if T is odd, and greater than 1) U = T + 1.
%
%    It can be shown that, for any starting point, the sequence always
%    terminates at 1 in finitely many steps.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer key: the starting point for the sequence.
%    key must be positive.
%
%  Output:
%
%    integer S[*]: the Nollatz sequence, starting with key.
%    Once the sequence reaches the value 1, no more entries are computed.
%
  if ( key <= 0 )
    s = [];
    return
  end

  n = 1;
  t = key;
  s(n) = t;

  while ( t ~= 1 )

    if ( 2 * round ( t / 2 ) == t )
      t = t / 2;
    else
      t = t + 1;
    end

    n = n + 1;
    s(n) = t;

  end

  return
end
