from
Collatz
by Matt Fig
Tools for examining the Collatz sequence.
|
| []=collatzrange(n,d)
|
function []=collatzrange(n,d)
% collatzrange(n,d)
% Checks for exceptions to the Collatz conjecture for numbers
% between n and d inclusive. Returns if an exception is found
% or not.
% Author: Matt Fig Montana State University
% Contact: popkenai@yahoo.com
h = [n:d];
while any(find(h > 1))
h(find(h==1)) = 0;
kk = find(odd(h));
jj = find(~odd(h));
h(kk) = 3*h(kk)+1;
h(jj) = .5*h(jj);
end
h(find(h==0)) = 1;
if any(h-1)
ff = find(h~=1); dd = h(ff)
disp('Its pizza time, exception found. The exception is:')
fprintf('\n\t\t\t\t\t %.0f ',dd)
return
end
sprintf('\n There is no exception in that range, try again.\n')
function val=odd(jj)
val = rem(jj,2);
|
|
Contact us at files@mathworks.com