function st = collatzst (n) %COLLATZST Compute the stopping time of n. % argmsg = nargchk (1, 1, nargin); if (~isempty (argmsg)) disp (argmsg); return; end st = zeros (size (n)); isodd = logical (mod (n, 2)); isnotone = (n - 1) > 0; idxnotone = find (isnotone); idxodds = find (isnotone & isodd); idxevens = find (isnotone & (~isodd)); while (any (isnotone)) n(idxodds) = 3 * n(idxodds) + 1; n(idxevens) = n(idxevens) / 2; st(idxnotone) = st(idxnotone) + 1; isodd = logical (mod (n, 2)); isnotone = (n - 1) > 0; idxnotone = find (isnotone); idxodds = find (isnotone & isodd); idxevens = find (isnotone & (~isodd)); end return;