Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Take a number. If it is even, halve it. If it is odd, multiply it by and add . Repeat until you reach . How many steps does it take from ?
Repeat while n is not 1, and use if and else inside the loop.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. A while loop that stops when n is 1.
That is steps.
Answer: steps. (Nobody knows if every starting number reaches 1. It has been checked for very large numbers, but there is no proof.)
Log in to join the discussion.
Loading…