Java: Ulam Sequence

The Ulam Sequence

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.

Does every sequence stop?

It's unknown if all sequences end at 1. Perhaps there are sequences that get into a loop, or increase indefinitely.

Programming 1 - Write a loop that shows the Ulam sequence

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).

Programming 2 - Write a GUI program

Write a program that shows the Ulam sequence.

  1. Menu. It should have a menubar (JMenuBar) with one menu (JMenu), "File", on it. The File menu has only one menu item (JMenuItem), "Quit", which stops the program. Write an ActionListener for the quit menu item that calls System.exit(0).
  2. Input. A number is input in a JTextField, and the corresponding Ulam sequence is computed.
  3. Computation. You will have to define an ActionListener that calls a function to compute the Ulam sequence. You can use the computeUlamString() method from Ulam Sequence - Answer, replacing the ", " with "\n" so that the numbers will be one per line.
  4. Output. The result should be displayed, one number per line, in a JTextArea. The JTextArea should be placed inside a JScrollPane so that scrollbars appear if there are more lines

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.