function y = f_3nplus1(n)
% ``Three n plus 1''.
% Study the 3n+1 sequence.
y = n;
while n > 1
   if rem(n,2)==0  % Could use mod(n,2)==0
      n = n/2;     % Even numbers/2
   else
      n = 3*n+1;   % Odd numbers*3 + 1
   end
   y = [y n];
end
end

