13. The so-called Hailstone numbers are sequences of integers generated in the Collatz problem, which was first proposed by L. Collatz in 1937.
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
1. If n is 1 then the sequence ends.
2. If n is even then the next n of the sequence = n/2
If n is odd then the next n of the sequence = (3 * n) + 1
3. Repeat Step 2 with the next n
For example, for a starting number of 5, the sequence is 5, 16, 8, 4, 2, 1, 4, 2, 1, ...
Such sequences are called hailstone sequences because the values typically rise and fall, somewhat analogously to a hailstone inside a cloud. While a hailstone eventually becomes so heavy that it falls to ground, every starting positive integer ever tested has produced a hailstone sequence that eventually drops down to the number 1 and then "bounces" into the small loop 4, 2, 1, ....
Your task - write a program that will accept a positive integer, then generate and display the sequence that results from that seed value - stopping when the number gets to 1. Include the number of cycles required to get to 1.
Sample Run of the Program (input and output values in bold):
Enter an integer seed value for the Hailstone sequence: 5
5, 16, 8, 4, 2, 1 (6 cycles)
Here are some others to try:
A seed of 12 produces: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1 (10 cycles)
A seed of 7 produces: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 (17 cycles)
|