2—Hailstone Sequences [12 marks]

A hailstone sequence is a sequence of integers found by applying the following rule:

Hailstone Iteration: For an integer n in a hailstone sequence, the next item in the sequence is

• 3n + 1 if n is odd, or

• n / 2 if n is even.

For example, the hailstone sequence starting at 6 is 6 ⇒ 3 ⇒ 10 ⇒ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1. The

sequence ends when 1 is reached. The name "hailstone sequence" stems from the way the values in the

sequence go up and down, as a hailstone does in the clouds before it falls to earth. Collatz (1937)

conjectured that for any starting integer, 1 would eventually be reached. So far there is no proof, making

the Collatz conjecture a famous unresolved problem in mathematics.

Write a complete Java program that will:

1) prompt the user to enter an integer N (in this document we call the input number N, but of course

in your program it must have a proper identifier);

2) scan the integers from 1 to N, where N is the user’s input, and find the hailstone sequence starting

at each integer and ending at 1 (see the example below);

3) determine the largest hailstone number in each sequence, and the hailstone sequence containing

the largest number overall;

4) print out the hailstone sequence with the largest number.

For example, if N = 5, the following hailstone sequences will be produced:

1: 1 [highest number: 1]

2: 2 ⇒ 1 [highest number: 2]

3: 3 ⇒ 10 ⇒ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1 [highest number: 16]

4: 4 ⇒ 2 ⇒ 1 [highest number: 4]

5: 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1 [highest number: 16]

The highest number reached is 16. It appears in sequences for starting numbers 3 and 5. Where two

sequences have the same largest hailstone number, choose the one with the smaller starting number. In

this case the starting number 3 gives the largest hailstone number.

The structure of your program should look like this:

That is, main calls getPositiveInt to input from the user the maximum starting number of interest. In

the call to getPositiveInt, use an upper bound of one million. Then main calls hailstoneMax N

times in a loop to calculate the max hailstone number for each starting number from 1 to N. The method

main itself determines which starting number gives the maximum hailstone number overall. It calls

hailstoneSequence once to generate a String containing the entire sequence for this maximum, and

then it prints the sequence out. Finally main calls sayTheEnd to print out the programmer’s name and

the date, and to say the program has finished.

What do these methods do in detail?

public static int getPositiveInt(final String PROMPT, final int BOUND):

This method calls JOptionPane.showInputDialog, using the PROMPT passed down as a

parameter. It reads in a String from the user. It removes blanks from the String as described in

the last assignment. Then it converts the String without blanks into an int. If the int is

between 1 and BOUND inclusive (that is, 1 and BOUND are acceptable inputs as well as any integer

between them), getPositiveInt returns the input int to the calling program. If not, it prompts

the user again, with a warning that states the allowable bounds. It will only prompt three times,

after which it returns 1 if the input is still not valid. To document what it is doing,

getPositiveInt prints to System.out both the prompt message and user’s input each time.

public static long hailstoneMax(final int START): This method generates the

entire hailstone sequence starting at START and ending at 1. Along the way it determines the

COMP 1010 Winter 2010 Assignment 3 Page 4 of 6

maximum hailstone number encountered. It

returns only that number. The returned value

must be a long because an int is not big

enough to hold some of the values to be

generated. Do all the computations in this

method using long arithmetic (see sidebar). In

generating the sequence, hailstoneMax uses

the hailstone iteration given at the beginning of

this question many times. It should call the

method hailstoneNext to compute the next

number in the sequence each time.

public static long

hailstoneNext(long current): This

method applies the hailstone iteration given at

the beginning of this question. Given the current

hailstone number, it generates the next one,

either by dividing by 2, or by multiplying by 3

and adding 1. Then it returns the next value. It

uses long arithmetic throughout.

public static String

hailstoneSequence(final int START):

This method generates the entire hailstone

sequence starting at START and ending at 1. It

concatenates all the hailstone numbers together

into a String, each pair separated by " ==> ".

public static void sayTheEnd( ): This

method simply produces the final output that

terminates the program. It has no parameters, so

the brackets are empty. It is void, meaning it

does not return a value. You can include the

method you developed for the last assignment, or

you can call a method in another class, as you did

in question 3 of the last assignment. It must be

called, but the code for it will not be marked.

Hand-in: Submit your .java file. Also, submit two sets of output using the following inputs (including the

blanks):

40 000

1 000 000

A hailstone sequence is a sequence of integers found by applying the following rule:

Hailstone Iteration: For an integer n in a hailstone sequence, the next item in the sequence is

• 3n + 1 if n is odd, or

• n / 2 if n is even.

For example, the hailstone sequence starting at 6 is 6 ⇒ 3 ⇒ 10 ⇒ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1. The

sequence ends when 1 is reached. The name "hailstone sequence" stems from the way the values in the

sequence go up and down, as a hailstone does in the clouds before it falls to earth. Collatz (1937)

conjectured that for any starting integer, 1 would eventually be reached. So far there is no proof, making

the Collatz conjecture a famous unresolved problem in mathematics.

Write a complete Java program that will:

1) prompt the user to enter an integer N (in this document we call the input number N, but of course

in your program it must have a proper identifier);

2) scan the integers from 1 to N, where N is the user’s input, and find the hailstone sequence starting

at each integer and ending at 1 (see the example below);

3) determine the largest hailstone number in each sequence, and the hailstone sequence containing

the largest number overall;

4) print out the hailstone sequence with the largest number.

For example, if N = 5, the following hailstone sequences will be produced:

1: 1 [highest number: 1]

2: 2 ⇒ 1 [highest number: 2]

3: 3 ⇒ 10 ⇒ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1 [highest number: 16]

4: 4 ⇒ 2 ⇒ 1 [highest number: 4]

5: 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1 [highest number: 16]

The highest number reached is 16. It appears in sequences for starting numbers 3 and 5. Where two

sequences have the same largest hailstone number, choose the one with the smaller starting number. In

this case the starting number 3 gives the largest hailstone number.

The structure of your program should look like this:

That is, main calls getPositiveInt to input from the user the maximum starting number of interest. In

the call to getPositiveInt, use an upper bound of one million. Then main calls hailstoneMax N

times in a loop to calculate the max hailstone number for each starting number from 1 to N. The method

main itself determines which starting number gives the maximum hailstone number overall. It calls

hailstoneSequence once to generate a String containing the entire sequence for this maximum, and

then it prints the sequence out. Finally main calls sayTheEnd to print out the programmer’s name and

the date, and to say the program has finished.

What do these methods do in detail?

public static int getPositiveInt(final String PROMPT, final int BOUND):

This method calls JOptionPane.showInputDialog, using the PROMPT passed down as a

parameter. It reads in a String from the user. It removes blanks from the String as described in

the last assignment. Then it converts the String without blanks into an int. If the int is

between 1 and BOUND inclusive (that is, 1 and BOUND are acceptable inputs as well as any integer

between them), getPositiveInt returns the input int to the calling program. If not, it prompts

the user again, with a warning that states the allowable bounds. It will only prompt three times,

after which it returns 1 if the input is still not valid. To document what it is doing,

getPositiveInt prints to System.out both the prompt message and user’s input each time.

public static long hailstoneMax(final int START): This method generates the

entire hailstone sequence starting at START and ending at 1. Along the way it determines the

COMP 1010 Winter 2010 Assignment 3 Page 4 of 6

maximum hailstone number encountered. It

returns only that number. The returned value

must be a long because an int is not big

enough to hold some of the values to be

generated. Do all the computations in this

method using long arithmetic (see sidebar). In

generating the sequence, hailstoneMax uses

the hailstone iteration given at the beginning of

this question many times. It should call the

method hailstoneNext to compute the next

number in the sequence each time.

public static long

hailstoneNext(long current): This

method applies the hailstone iteration given at

the beginning of this question. Given the current

hailstone number, it generates the next one,

either by dividing by 2, or by multiplying by 3

and adding 1. Then it returns the next value. It

uses long arithmetic throughout.

public static String

hailstoneSequence(final int START):

This method generates the entire hailstone

sequence starting at START and ending at 1. It

concatenates all the hailstone numbers together

into a String, each pair separated by " ==> ".

public static void sayTheEnd( ): This

method simply produces the final output that

terminates the program. It has no parameters, so

the brackets are empty. It is void, meaning it

does not return a value. You can include the

method you developed for the last assignment, or

you can call a method in another class, as you did

in question 3 of the last assignment. It must be

called, but the code for it will not be marked.

Hand-in: Submit your .java file. Also, submit two sets of output using the following inputs (including the

blanks):

40 000

1 000 000

### Recently Asked Questions

- An SVM is trained with the following data X0 X1 y -1 -1 -1 1 1 1 0 2 1 a) Show the kernel matrices for i) the linear kernel ii) polynomial kernel of degree 3

- Companies use the Process (Functional) facility layout to produce products or services that require a high degree of variety or customization. Other companies

- Divided Walls Construction (DWC) has determined that the yield to maturity (YTM) on new bonds is 5 percent, and its cost of common stock is 11 percent. If