function seq = collatz(x)
%COLLATZ Collatz sequence
%  SEQ = COLLATZ(X) returns the sequence of numbers SEQ
%  for the Collatz conjecture starting from the positive
%  integer number X.

% the sequence begins with whatever value x is
seq = x;

% the sequence continues until x is 1
while x ~= 1
  % if x is even, divide it by 2
  % otherwise, triple it and add 1
  if rem(x, 2) == 0
    x = x / 2;
  else
    x = 3 * x + 1;
  end

  % add the new value of x to the end of the sequence
  seq = [seq, x];
end

end
