Monday's Programming Question
Last week's programming question introduced Lothar Collatz's theory: for any number it's possible to make a sequence of numbers that will eventually end in one by following a simple rule; if the number is even halve it by two, if it's odd times it by three and add one (e.g., starting with the number 5 the sequence would be 5 16 8 4 2 1).
This theory is still unproven and I thought it might be interesting to expand the hailstone sequence generating program into one that could prove it for a certain set of numbers. Unfortunately we could never write a program that could prove the theorem completely because we would have to test all the numbers to infinity. However in terms of Java, it's a nice way to take a piece of code you've already written, move it into a method, and write some new code to call that method a number of times.
The question is can your program find the number between 1 and 1000 that has the longest sequence of numbers (e.g., the number 125 has 109 numbers in its sequence) and display it to the screen?
Hint: Take your code from last week or my version and move it into a method. Then use the new method to calculate the hailstone sequence for the numbers 1 to 1000 and keep track of which one is the longest. Finally, you'll need to modify the code so that it only displays the sequence to the screen for the right number.


Comments
No comments yet. Leave a Comment