7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1Many mathematicians have looked at this sequence, and this sequence as a result has been associated with all these names: Collatz, Kakutani, Thwaites, Hasse, Ulam. Because the numbers tend to move up and down for a while before reaching 1, just as hailstones move up and down in a cumulonimbus cloud before reaching the ground, this has also been called the "hailstone" sequence.
The basic conjecture is that, no matter what N you start with, you always eventually end up at 1.
For this lab, you are to test this for all N<= 1000. You are also to find the N in that range which gives you the longest sequence, and also find the N which gives you the largest sequence value. For N=7, the length of the sequence is 16 or 17 depending on whether you count the final 1 or not, and the largest value is 52.
Starter files are here.
Here is some output from my version, for the range N <= 1000:
max = 250504 occurred at N=703 longest sequence length = 178 occurred at N=871Here is the output for the range N<= 10000:
max = 27114424 occurred at N=9663 longest sequence length = 261 occurred at N=6171
while (N != 1) { if (N % 2 == 0) // Check if N is even! N = N/2; else N = 3*N+1; }This should terminate for all N you try, but that doesn't exactly tell you much. Put the basic loop into a method, eg collatz(int N).
The loop method from Step 1 (I called it collatz(N)) can just set some private fields of the Collatz class, but I found it easier to have collatz(N) return a Stats object, which has max and len fields.
What you have to do is call collatz(N) for all N<=1000 (inside a loop). Start with two variables for the max length and the starting N at which it occurred; I called them maxlen and posN. For each N you try, if len is the length of its sequence, check something like this:
if (len > maxlen) {maxlen = len; posN = N; }That is, save both the length and the value of N. At the end, print out maxlen and posN. Then do the same for max; note that you are looking for the maximum "max" value, that is, the "maxmax".
Email me your completed Collatz.java file, or a zipfile of the entire project.