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
Mostra commenti meno recenti
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 Commento
Sachin Ganjare
il 29 Ott 2012
Do format the code properly, so that it will be readable.
Risposta accettata
Più risposte (1)
Sachin Ganjare
il 29 Ott 2012
0 voti
m=1 should be outside the while loop.
Hope it helps!!
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!