用 Matlab 函数实现 Collatz 猜想!拜托了!!在线等!!

用 Matlab 函数实现 Collatz 猜想!拜托了!!在线等!!,第1张

function threenplus1(n)

%"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=1

while n~=1,

    if mod(n,2)==1,

        n = n*3 + 1

    else

        n = n / 2

    end

    if n>a,

        a=n

    end

end

a


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11422794.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存