A "hailstone sequence" is defined as follows: Given any starting positive integer, the next term in the sequence is obtained from the previous by the following two rules:
3*previous + 1
).These sequences concern one of the most famous unsolved problems in mathematics: the
Collatz conjecture, named after German mathematician Lothar Collatz who posed the question in 1937. The Collatz
conjecture hypothesizes that, no matter the starting number, all such sequences eventually reach the number 1
.
Given a positive int
variable n
, write a program that prints out each term in the hailstone sequence starting
from n
until 1
is reached (i.e. 1
should always be the last term printed).
Stuck? You may find these lessons helpful: