7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
The length
of this particular sequence is 16 (we count steps in the process and so
don't count the final "1"). We will also refer to the largest value
encountered, 52 in this example, as the peak.The basic conjecture is that, no matter what N you start with, you always eventually end up at 1. This has never been proven.
For this lab, you are to test this for all 1<=N<= 1000 and 1001<=N<=2000. You are also to find the N in each of these ranges which gives you the sequence with the maximum length, and also find the N which gives you the maximum peak value.
The starter zipfile is here.
Here is some output from my version, for the range N <= 1000:
max peak value = 250504 occurred at N=703Here is the output for the range N<= 10000:
longest sequence length = 178 occurred at N=871
max peak value = 27114424 occurred at N=9663It is possible, for a single N, to find the length and the peak value in one loop, but it is tricky. So, although it is "wasteful" in some sense, I recommend separate functions of N to find length() and peak().
longest sequence length = 261 occurred at N=6171
while (N != 1) {This should terminate for all N you try, but that doesn't exactly tell you much. It's found in the lab starter file as collatz(int N). Modify this to create collatz_length(int N), returning the sequence length, and collatz_peak(int N), returning the peak value.
if (N % 2 == 0) // Check if N is even!
N = N/2;
else
N = 3*N+1;
}
Suppose you're working on the maximum length. What you have to do is call collatz_length(N) for all N in the range, eg 1<=N<=1000 (inside a loop, such as that in my range method). Start with two variables, for the max length and the N at which it occurred; I called them maxLen and maxLenPos. For each N you try, if len is the length of its sequence, check something like this:
if (len > maxLen) {maxLen = len; maxLenPos = N; }That is, save both the length and the value of N. At the end, print out maxLen and maxLenPos. Then do the same for peak; I used variables "maxPeak" and position "maxPeakPos".
Email me your completed project.