Question: Consider the following operation on an arbitrary positive integer: If the number is even, divide it by two If the number is odd, triple it
Consider the following operation on an arbitrary positive integer:
- If the number is even, divide it by two
- If the number is odd, triple it and add three
We can formulate the above operation with a function f as follows:
We can formulate the above operation with a function f as follows:
Suppose we create the sequence Sn = (n, f(n), f(f(n)), f(f(f(n))), ...), i.e., by starting at n and applying at each next step function f to the result of the function at the previous step.
There is something quite interesting with this sequence. For any chosen number n, the sequence Sn will eventually fall in a loop 6, 3, 12, 6, ... We omit the repeated part after the first appearence of 3. The length of the sequence is defined over the shown part. Look at the following examples:
| n | Sequence Sn | Length |
| 1 | 1, 6, 3 | 3 |
| 2 | 2, 1, 6, 3 | 4 |
| 3 | 3 | 1 |
| 4 | 4, 2, 1, 6, 3 | 5 |
| 5 | 5, 18, 9, 30, 15, 48, 24, 12, 6, 3 | 10 |
| 6 | 6, 3 | 2 |
| 7 | 7, 24, 12, 6, 3 | 5 |
Observe that the numbers in all sequences may fluctuate until they eventually end in 3. The sequence Sn defined by function f(n) in the above way is called a hailstone sequence (because hailstones also travel up and down in the atmosphere before eventually falling to the earth). Note also that, the most classic hailstone sequence is called Collatz conjecture. The problem we discussed here is just a modified version, but for simplicity we still call it hailstone sequence as they have quite similar property.
#includeusing namespace std; /* * printHailstoneSequence(n) prints the hailstone sequence for n. */ void printHailstoneSequence(int n) { //Finish this function. Type your code below. } /* * shortestHailstoneSequenceLength(start, end) returns the length of the shortest hailstone sequence in the range [start, end]. */ int shortestHailstoneSequenceLength(int start, int end) { //Finish this function. Type your code below. return 0; //You probably will want to change this line to something else. } /* * printHistogram(start, end) prints the histogram of the distribution of hailstone sequence lengths for the sequences in [start, end]. */ void printHistogram(int start, int end) { //Finish this function. Type your code below. } /* * main() is the entry point of the program. * Do NOT modify it in any way. */ int main() { //Define the variable for getting the user's choice. int choice; //Print the menu. cout > choice; //Perform a basic input validation. while(choice3) { cout > choice; } cout > n; //Perform a basic input validation. while(n10000) { cout > n; } //Output the result. cout > start >> end; //Perform a basic input validation. while(start>end || start10000 || end>10000) { cout > start >> end; } //Output the result. cout > start >> end; //Perform a basic input validation. while(start>end || end-start>=100 || start10000 || end>10000) { cout > start >> end; } //Output the result. cout
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
