Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In 1 9 7 9 , Douglas Hofstadter, Professor of Cognitive Science at the University of Indiana, wrote G del , Escher, Bach. The book

In 1979, Douglas Hofstadter, Professor of Cognitive Science at the University of Indiana, wrote
Gdel, Escher, Bach. The book won the Pulitzer Prize for Literature and has over the years
become one of the classics of computer science. Much of its charm comes from the
mathematical oddities and puzzles it contains, many of which can be expressed in the form of
computer programs. Of these, one of the most interesting concerns the sequence of numbers
formed by repeatedly executing the following rules some positive integer n :
If n is equal to 1, you've reached the end of the sequence and can stop.
If n is even, divide it by two.
If n is odd, multiply it by three and add one.
Although is also goes by several other names, this sequence is often called the hailstone
sequence because the values tend to go up and down before coming back to 1, much as
hailstones in the clouds in which they form.
Write a C++ program that reads in a number from the user and then generates the hailstone sequence
from that point, as in the following sample run:
Enter a number: 15
15 is odd, so I multiply by 3 and add 1 to get 46
46 is even, so I divide it by 2 to get 23
23 is odd, so I multiply by 3 and add 1 to get 70
70 is even, so I divide it by 2 to get 35
35 is odd, so I multiply by 3 and add 1 to get 106
106 is even, so I divide it by 2 to get 53
53 is odd, so I multiply by 3 and add 1 to get 160
160 is even, so I divide it by 2 to get 80
80 is even, so I divide it by 2 to get 40
40 is even, so I divide it by 2 to get 20
20 is even, so I divide it by 2 to get 10
10 is even, so I divide it by 2 to get 5
5 is odd, so I multiply by 3 and add 1 to get 16
16 is even, so I divide it by 2 to get 8
8 is even, so I divide it by 2 to get 4
4 is even, so I divide it by 2 to get 2
2 is even, so I divide it by 2 to get 1
One of the fascinating things about the hailstone sequence is that no one has yet been able to
prove that the process always stops. The number of steps in the process can get very large, but
somehow, it always seems to climb back down to one.
image text in transcribed

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

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago