function c = collatz(n) %function c = collatz(n) % % Joe Student, ID 123456 % % Computes the Collatz sequence for a given starting value % % if n is even, divide it by 2 % if odd, triple it and add 1 % if n equals 1, stop % % Input % n -- starting value (positive integer) % % Output % c -- results, sequence of values starting with n, ending with 1 % error(nargchk( if n <= 0 error( end if floor(n) ~= n error( end c = n; toobig = ; while n > 1 if mod(n,2) == 0 n = else n = end if n > toobig c = [c inf]; %% set last result = inf and bail out break end c = [c n]; end