updown4.m
来自「matlab7.0这本书的源代码」· M 代码 · 共 28 行
M
28 行
% updown4.m
% up-down algorithm
if ~exist('Nums','var') % numbers to test
Nums = 25:50;
end
N = Nums; % duplicate numbers
Counts = zeros(size(N)); % preallocate array
not1 = N>1; % True for numbers greater than one
while any(not1)
odd = rem(N,2)~=0; % True for odd values
odd_not1 = odd & not1; % True for odd values greater than one
even_not1 = ~odd & not1; % True for even values greater than one
N(even_not1) = N(even_not1)/2; % Process evens
Counts(even_not1) = Counts(even_not1)+1;
N(odd_not1) = (3*N(odd_not1)+1)/2; % Process odds
Counts(odd_not1) = Counts(odd_not1)+2;
not1 = N>1; % Find remaining numbers
end
%results=[Nums' Counts']
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?