Introduction
The January 1984 issue of
Scientific American contains an
article describing an interesting sequence of numbers known as the
Hailstone
Series. The series is formed like this:
- Pick a positive integer.
- If it's odd, triple the number and add one.
- If it's even, divide the number by two.
- Go back to 2.
Although the numbers usually bob up and down, they eventually they
reach a repeating "ground" state:
4 2 1 4 2 1 ... This has
been proven for every number up to about
1.2E12
Example
For example, here is the sequence generated for an initial value of 26:
- 26 - even, so divide it by 2...
- 13 - odd, triple it (getting 39), and add 1...
- 40 - even, divide by 2...
- 20 - again
- 10 - etc., etc.
- 5
- 16
- 8
- 4
- 2
- 1
- 4
- ...
Make sure you undertand how each successive number was obtained, using
the rules listed at the top.
Problem Statement
You will write a program to generate the
Hailstone Series for
initial values entered by the user. Your program should answer the
following questions after the ground state has been reached:
- How many items are in the sequence?
- What is the largest number the sequence reaches along the way?
For the first question you should stop counting once any member of the
sequence
4 2 1 is reached. If a member of the ground state is
entered as the initial value, then the length of the sequence is just
1.
For example, if 8 is entered as the initial value, then the answer
to question #1 is 2 (i.e., it stopped counting when 4 was encountered).
Go back and look at the sequence you made with the starting value
of 26, and answer the two questions. You should find that there are 9
items in the list, with 40 being the maximum.
Goal Analysis and A Staged Solution
(this section has been removed for now because students are doing this
assignment - please email me if you want the original file)