% Here, I'm going to find out how % long it takes 3x+1 to get to 1. % This is my threshold value, beyond % which I say "it never gets to 1." threshold = 500; % How many numbers should I try, % and where should I start? numstart = 1; numend = 50000; numsteps = zeros(1,numend); avgsofar = zeros(1,numend); for x=numstart:numend, count = 0; y = x; while y > 1 && count < threshold, % x is the start number. iterate, changing y! if mod(y,2) y = 3*y + 1; end y = y/2; count = count+1; end if y == 1 % disp([num2str(x) ' went to 1 in ' num2str(count) ' steps.']); else disp(['**** ' num2str(x) ' did not go to 1!!! ****']); end numsteps(x) = count; avgsofar(x) = sum(numsteps) / x; end X = 1:length(numsteps); Y = 6.95*log(X+1); plot(X,numsteps,'.',X,avgsofar,'-',X,Y,'-');