MATLAB: How to solve this odd/even loop question (hailstone sequence)

evenhailstoneodd

n = 71
If n is even, divide it by 2 to get n / 2.
If n is odd, multiply it by 3 and add 1 to obtain 3n + 1.
Repeat process until n = 1.
I need to find out how many steps it takes to reach 1..
thanks!

Best Answer

I think this is your requiremnt. Please check
num = 71;
count = 0;
while(num~=1)
count = count+1;
if(mod(num,2)==0)
num = num/2;
else
num = 3*num + 1;
end
end
fprintf('count = %d', count);