Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

7. Douglas Hofstadter's Pulitzer-prize-winning book Gdel, Escher, Bach contains many interesting mathematical puzzles. In Chapter XII, Hofstadter mentions a wonderful problem that is well within

image text in transcribed

7. Douglas Hofstadter's Pulitzer-prize-winning book Gdel, Escher, Bach contains many interesting mathematical puzzles. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements in Java. The problem can be expressed as follows: Pick some positive integer and call it n. If n is even, divide it by two. If n is odd, multiply it by three and add one. Continue this process until n is equal to 1. Hofstadter illustrates this process with the following example, starting with the number n 15 15 is odd, so I make 3n+1: 46 46 is even, so I take half: 23 23 is odd, so I make 3n+1: 70 70 is even, so I take half: 35 35 is odd, so I make 3n+1: 106 106 is even, so I take half: 53 53 is odd, so I make 3n+1: 160 160 is even, so I take half: 80 80 is even, so I take half: 40 40 is even, so I take half: 20 20 is even, so I take half: 10 10 is even, so I take half: 5 5 is odd, so I make 3n+1: 16 16 is even, so I take half: 8 8 is even, so I take half: 4 4 is even, so I take half: 2 2 is even, so I take half: 1 As you can see from this example, the numbers go up and down, but eventually-at least for all numbers that have ever been tried-come down to end in 1. In some respects, this process is reminiscent of the formation of hailstones, which get carried upward by the winds over and over again before they finally descend to the ground. Because of this analogy, this sequence of numbers is usually called the Hailstone sequence, although it goes by many other names as well. Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, followed by a line showing the number of steps taken to reach 1. HailstoneSequence Operations +public static ArrayList getHailstoneSequence(int n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

17. Refer to Exercise 14. Comment on the event G = (A n B).

Answered: 1 week ago

Question

=+5.5. Suppose that X has mean m and variance o2.

Answered: 1 week ago

Question

4. Does cultural aptitude impact ones emotional intelligence?

Answered: 1 week ago