Help with infinite while loop

2 Ansichten (letzte 30 Tage)
Devan
Devan am 24 Nov. 2012
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 Kommentare
Matt J
Matt J am 24 Nov. 2012
OK. Glad I didn't spend my day trying to prove it :)
Jan
Jan am 25 Nov. 2012
Bearbeitet: Jan am 25 Nov. 2012
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".

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Matt Fig
Matt Fig am 24 Nov. 2012
Bearbeitet: Matt Fig am 24 Nov. 2012
The problem is that ID never changes in your loop so you are just doing the same thing over and over. Change all 'out' variables to 'ID' and your code should work fine.

Devan
Devan am 24 Nov. 2012
thanks!

Jan
Jan am 25 Nov. 2012
Bearbeitet: Jan am 25 Nov. 2012
It is a good idea to ask Google, in this case for "3N+1 Wiki". You find explanations, theorems, algorithms and visualizations, ... Enough material to solve the homework.

Kategorien

Mehr zu Call C++ from MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by