Syracuse Sequence
- while loops are more useful when you don't know
exactly how many times your loop will execute. So, here is an exercise
for you to develop a program which will terminate
based on some kind of condition.
-
The Syracuse (also called Collatz or Hailstone) sequence is
generated by starting with a natural number and repeatedly applying the
following function until reaching 1:
syr(x) = | x/2 | if x is even
|
| 3x + 1 | if x is odd
|
For example, the Syracuse sequence starting with 5 is: 5, 16, 8, 4, 2,
1.
-
Write a program that gets a starting value from the user and then prints
the Syracuse sequence for that starting value.
-
The program may run as follows.