University of Pittsburgh
Spring 2004
CS0007:  Introduction to Computer Programming
  Assignment 4:  The Hailstone Series
PROJECT INFORMATION
DUE DATE
Wed Feb 25, midnight
VALUE
50 points
BACKGROUND READING
Chapter 3 JBD

Read the entire assignment before attempting this project.
 

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:
  1. Pick a positive integer. 
  2. If it's odd, triple the number and add one. 
  3. If it's even, divide the number by two. 
  4. Go back to 2.
Although the numbers bob up and down, 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.

Try this by hand for a few small numbers and get the idea (note how YOU go into a loop! For example, start with 9 and write out the sequence. I'll even give you some space: 
 
 
 
 
 
 

 
 

 

WHAT TO DO

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: 
  1. How many items are in the sequence? 
  2. 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, and it should include the initial value. 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). Question #2 simply asks you to keep track of the maximum value in the sequence, which is 8 in this small example.

Look above at the sequence you made with the starting value of 9, and answer the two questions. You should find that there are 18 items in the list, with 52 being the maximum. If you didn't, go back and try it again. If you can't do these sequences on paper, you'll have a dill of a pickle of a time writing a program to do it.
 

OUTPUT

In a nutshell, your program should begin by asking for a positive integer, and then it should print out the answers to the questions above.  Please don't do anything beyond that in the program you submit (although if you do more and want to show me, I'm happy to have a look!).
 
PROGRAM REQUIREMENTS

Just a few things:
  • Use good style (indentation, structure, appropriate variable names, and comments)
  • Your final program should not print out the entire sequence (some of them are very long)
    • although you might want to while you are working on the program
    • if you want, you can ask the user if s/he wants to see the sequence, then print only if a yes was given.
  • You should error check and guarantee the user has entered a positive value (do not worry about malformed input, however - assume an integer will be entered)
  • You should declare your variables as long instead of int.  This will increase the range of numbers your program can handle. 
 
TURNING THE ASSIGNMENT IN

As usual, copy your source (.java) and executable (.class) files into the assignment4 handin directory.  Please try to avoid copying other files over.
 
Last Updated: 2/11/04 by H. Chad Lane
© 2003-2004 University of Pittsburgh