Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Oct. de 2012
You have "m=1" as the first thing happening inside the while loop, so you keep resetting the count to 1.

Más respuestas (1)

Sachin Ganjare
Sachin Ganjare el 29 de Oct. de 2012
m=1 should be outside the while loop.
Hope it helps!!

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by