A mathematician named Ulam proposed generating a sequence of numbers from any positive integer n (n > 1) as follows:
Here are some examples for the first few integers.
2 -> 1 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 4 -> 2 -> 1 5 -> 16 -> 8 -> 4 -> 2 -> 1 6 -> 3 -> etc as for 3 above. 7 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> see 5 above.
Ulam hasn't been the only one to work on this problem. See The 3x+1 problem and its generalizations for more information.
It's unknown if all sequences end at 1. Perhaps there are sequences that get into a loop, or increase indefinitely.
Write a loop that starts with a value of n and computes all values in the sequence. (An example Ulam Sequence - Answer shows a solution written in a method).
Write a program that shows the Ulam sequence.
|
No more Happy Trails. In the button listener, use
a try...catch
statement to catch conversion
errors (NumberFormatException
).
Check that the input number is at least 1.
Display the error message in the output area.
Iterative programming. There are a lot of little parts to this program. Write a small part (eg, just an empty window with no menus) and get it running. Add the parts one by one, getting each running before moving on to the next.