function sequence=collatz(n) % Collatz problem. Generate a sequence of integers resolving to 1 % For any positive integer, n: % Divide n by 2 if n is even % Multiply n by 3 and add 1 if n is odd % Repeat for the result % Continue until the result is 1 sequence = n; next_value = n; while next_value >= 1 if rem(next_value,2)=0 next_value = next_value/2; else next_value = 3*nextvalue+1; end sequence = [sequence, next_value]; end