An interesting unsolved problem in mathematics concerns what is called the "hailstone sequence." This sequence is defined as follows: Start with any positive integer. If that number is odd, then multiply it by three and add one. If it is even, divide it by two. Then repeat.
For example, starting with 5 we get the hailstone sequence:
Here, the subsequence 4,2,1 is reached, resulting in a loop. It is conjectured that no matter what number you start with, you will always end up in the 4,2,1 loop. It has, in fact, been shown to hold for all starting values up to 1,200,000,000,000. However, the conjecture still has not been proven to hold for all numbers.
Write a program that prompts the user for an integer, and then generates and displays the hailstone sequence starting with that integer. Each successive number in the hailstone sequence should be displayed on a separate line. If the sequence gets to 1 (meaning it is stuck in the 4,2,1 loop), then your program should print out the word "STUCK!" and terminate. In addition, you should keep track of and display the length of the hailstone sequence (up to the point where 1 is reached). For example, consider the following sample output, with user input in bold:
Enter the starting number of a hailstone sequence:
0
The number must be positive. Try again:
-5
The number must be positive. Try again:
5
16
8
4
2
1
STUCK!
Hailstone sequence length: 6
As this sample output demonstrates, your program should verify that the user's input is a positive integer, reprompting as necessary.
Using your program, answer the following questions: