%"Three n plus 1".
% Study the 3n+1 sequence.
% threenplus1(n) plots the sequence starting with n.
% threenplus1 with no arguments starts with n = 1.
% uicontrols decrement or increment the starting n.
% Is it possible for this to run forever?
if ~isequal(get(gcf,'tag'),'3n+1')
shg
clf reset
uicontrol( ...
'position',[260 5 25 22], ...
'string','<', ...
'callback','threenplus1(''<'')')
uicontrol( ...
'position',[300 5 25 22], ...
'string','>', ...
'callback','threenplus1(''>'')')
uicontrol( ...
'position',[480 5 40 22], ...
'string','close', ...
'callback','close(gcf)')
set(gcf,'tag','3n+1')
end
if nargin == 0
n = 1
elseif isequal(n,'<')
n = get(gcf,'userdata') - 1
elseif isequal(n,'>')
n = get(gcf,'userdata') + 1
end
if n <1, n = 1end
set(gcf,'userdata',n)
y = n
while n >1
if rem(n,2)==0
n = n/2
else
n = 3*n+1
end
y = [y n]
end
semilogy(y,'.-')
axis tight
ymax = max(y)
ytick = [2.^(0:ceil(log2(ymax))-1) ymax]
if length(ytick) >8, ytick(end-1) = []end
set(gca,'ytick',ytick)
title(['n = ' num2str(y(1))])
n=27a=1while n~=1,
if mod(n,2)==1,
n = n*3 + 1
else
n = n / 2
end
if n>a,
a=n
end
end
a
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)