CSC 490
Programming Assignment #1
Summer’17
th
Due Date: June 1
Late submission is not accepted,.
You must submit a hard copy of the source code, and the output
(make sure you have several runs for the output). All pages must be
stapled, with your name, course number and project number.
You can use any editor of your choice, i.e. NetBeans, Eclips, JGraps,
etc..
Problem Statement:
Douglas Hofstadter’s Pulitzer-prize-winning book Gödel, Escher, Bach
contains many interesting mathematical puzzles, many of which can
be expressed in the form of computer programs.
Hofstadter mentions a wonderful problem that is well within the scope
of the control statements. 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 on
On page 401 of the Vintage edition, Hofstadter illustrates this process
with the following example, starting with the number 15:
15
46
23
70
35
106
53
160
80
40
20
10
5
16
is odd, so I make 3n + 1:
is even, so I take half:
is odd, so I make 3n+1:
is even, so I take half:
is odd, so I make 3n + 1:
is even, so I take half:
is odd, so I make 3n+1:
is even, so I take half:
is even, so I take half:
is even, so I take half:
is even, so I take half:
is even, so I take half:
is odd, so I make 3n+1:
is even, so I take half:
46
23
70
35
106
53
160
80
40
20
10
5
16
8
8
4
2
is even, so I take half:
is even, so I take half:
is even, so I take half:
4
2
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—comes
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 ConsoleProgram and/or a GUI interface, that reads in a number
from the user and then displays the Hailstone sequence for that
number, just as in Hofstadter’s book, followed by a line showing the
number of steps taken to reach 1.
For example, your program should be able to produce a sample run
that looks like this:
The fascinating thing about this problem is that no one has yet been
able to prove that it always stops. The number of steps in the process
can certainly get very large. How many steps, for example, does your
program take when n is 27
The program output must show all steps performed and must have
several runs, try an input with 15 as the first run and another input with
25 , etc..
Purchase answer to see full
attachment