Preview

Hello

Good Essays
Open Document
Open Document
888 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Hello
—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

You May Also Find These Documents Helpful

Related Topics