Help with infinite while loop
5 次查看(过去 30 天)
显示 更早的评论
I need to create a simple script that take a number, checks if it odd or even. If it's even: divide by two, if it's odd: multiply by 3 add 1. The script is supposed to repeat this check and calculation until the output reaches a value of one. I've tried the following while loop which calculates the first value but then infinity spits out this same value. Thanks in advance!
clear
disp('Please input student number')
ID = input('SI#: ');
if mod(ID,2) == 0
out = ID/2
else out = ID.*3+1
end
while out > 1
if mod(ID,2) == 0
out = ID/2
else out = ID.*3+1
end
end
6 个评论
Jan
2012-11-25
编辑:Jan
2012-11-25
It is not proved. See http://en.wikipedia.org/wiki/Collatz_conjecture. You can earn 500$ and the Fields medal when you prove it, but this would conflict with the term " algorithmically undecidable".
回答(3 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call C++ from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!