I am trying to replace a integer n if odd by 3 times value plus one or if even replace by half the value and count the steps but the counting does not seem to occur properly
3 次查看(过去 30 天)
显示 更早的评论
n = input('input the number');
while(n~=1)
m=1;
if(n>1)
if(rem(n,2)==0)
n=n/2;
else
n=3*n+1;
end
end
m=m+1;
end
1 个评论
采纳的回答
Walter Roberson
2012-10-29
You have "m=1" as the first thing happening inside the while loop, so you keep resetting the count to 1.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!