Code covered by the BSD License  

Highlights from
Collatz

from Collatz by Matt Fig
Tools for examining the Collatz sequence.

collatz=collatzbackwardward(n,cnt)
function collatz=collatzbackwardward(n,cnt)
% COLLATZBACKWARD(n,cnt)
% Calculates the backward collatz numbers along the odd 
% branches from integer n and shows where the branch  
% points are, out to cnt terms. The branch points are 
% places where there is both a possible even "future"
% number and an odd "future" number.  Here only the
% odd braches are followed.
% Author:  Matt Fig  Montana State University
% Contact:  popkenai@yahoo.com
format compact;
i = 1;   b = zeros(2,cnt+1);   Notallow = [1 2 4];
if find(n==Notallow), disp('Will not work for n=1,2,4'), return, end
while n~=4 & i<= cnt+1
      b(1,i) = n;   h = (n-1)/3;
      if h==floor(h) &  h~=4 & h~=2   
         b(2,i) = n;
         n=h;
      else 
         n = 2*n;
      end
         i = i+1;
end
collatz = b';  

Contact us at files@mathworks.com