Answered step by step
Verified Expert Solution
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 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 :
If is equal to you've reached the end of the sequence and can stop.
If is even, divide it by two.
If 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 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:
is odd, so I multiply by and add to get
is even, so I divide it by to get
is odd, so I multiply by and add to get
is even, so I divide it by to get
is odd, so I multiply by and add to get
is even, so I divide it by to get
is odd, so I multiply by and add to get
is even, so I divide it by to get
is even, so I divide it by to get
is even, so I divide it by to get
is even, so I divide it by to get
is even, so I divide it by to get
is odd, so I multiply by and add to get
is even, so I divide it by to get
is even, so I divide it by to get
is even, so I divide it by to get
is even, so I divide it by to get
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started