This is one of four major individual assignments that you are required to complete in this class. You MUST design and code this project on your own. You can ask for debugging help, particularly from the tutors, TA or instructor, but this project is to be your own work. The project relies on knowledge and skills that you will develop in Recitation Laboratories 1-8. If you are having trouble with these recitations after your in-class session, please be sure that you come to the lab at another time and sit down with a tutor.
| The Three-Halves Problem |
|---|
|
For example, if we start with n=17, the game produces the following sequence of integers, called a run, of length 10:
In this case and in every other case that individuals have tried, the sequence reaches 1, and repeats 2, 1, 2, 1, ... indefinitely, so once 1 is reached, the sequence is regarded as at an end. In calculating the length, the initial number counts, and numbers count up to and including the first time 1 is reached. (Check that 1 is replaced by 2 and then by 1, so nothing more happens after one of these sequences gets to 1.) Researchers conjecture that the sequence converges to 1 for any starting positive integer n, but no one has ever been able to prove this. For this assignment, you are to write a program to investigate this game and to practice with features of Java. The work should proceed in three phases.
There should also be a ThreeHalvesTest class that reads an integer n, creates an instance of ThreeHalvesRun with input n, and uses the toString and an explicit call to runPrint to print the run. The output after reading 27 could look as follows:
Project 3, Phase I: Simple run from initial integer
Enter integer ---> 27
Initial integer: 27, Length: 71, Maximum value: 4616
27, 41, 62, 31, 47, 71, 107, 161, 242, 121,
182, 91, 137, 206, 103, 155, 233, 350, 175, 263,
395, 593, 890, 445, 668, 334, 167, 251, 377, 566,
283, 425, 638, 319, 479, 719, 1079, 1619, 2429, 3644,
1822, 911, 1367, 2051, 3077, 4616, 2308, 1154, 577, 866,
433, 650, 325, 488, 244, 122, 61, 92, 46, 23,
35, 53, 80, 40, 20, 10, 5, 8, 4, 2,
1
You should modify your class ThreeHalvesRun as follows:
There should be a new class ThreeHalves with just a few things in it:
Finally, as before, there should also be a ThreeHalvesTest class, with a main method that reads an integer n, creates an instance of ThreeHalves with input parameter n. Then it should fetch the array created by ThreeHalves, and pass the array to a static method:
Notice that in this case the above static method would find the maximum element in any array that implements the Comparable interface.
The output after reading 28 should be the same as above, since it gives the integer less than 28 producing the longest run, and this will be 27. In fact all integers up to 55 keep giving 27 as the answer, but 55 gives 54 as an answer, since there is a run of length 72 starting with 54.
Entering 1000 should produce:
Project 3, Phase II: Longest run of any integer less than initial integer
Enter integer ---> 1000
Initial integer: 871, Length: 114, Maximum value: 95498
871, 1307, 1961, 2942, 1471, 2207, 3311, 4967, 7451, 11177,
16766, 8383, 12575, 18863, 28295, 42443, 63665, 95498, 47749, 71624,
35812, 17906, 8953, 13430, 6715, 10073, 15110, 7555, 11333, 17000,
8500, 4250, 2125, 3188, 1594, 797, 1196, 598, 299, 449,
674, 337, 506, 253, 380, 190, 95, 143, 215, 323,
485, 728, 364, 182, 91, 137, 206, 103, 155, 233,
350, 175, 263, 395, 593, 890, 445, 668, 334, 167,
251, 377, 566, 283, 425, 638, 319, 479, 719, 1079,
1619, 2429, 3644, 1822, 911, 1367, 2051, 3077, 4616, 2308,
1154, 577, 866, 433, 650, 325, 488, 244, 122, 61,
92, 46, 23, 35, 53, 80, 40, 20, 10, 5,
8, 4, 2, 1
Notice that with just the change of the single method in a single class, one is answering a completely different question:
This time entering any number less than 256 should produce the answer integer 27 with maximum length 4616. However, initial number 256 produces the result:
Project 3, Phase III: Maximum value in any run less than initial integer
Enter integer ---> 256
Initial integer: 255, Length: 33, Maximum value: 6560
255, 383, 575, 863, 1295, 1943, 2915, 4373, 6560, 3280,
1640, 820, 410, 205, 308, 154, 77, 116, 58, 29,
44, 22, 11, 17, 26, 13, 20, 10, 5, 8,
4, 2, 1
Here the run length is much shorter than for 27, but the maximum value is larger.
Notice also that there are other integers between 27 and 255 with maximum value 4616, but your software is supposed to produce the first (smallest) one.
If 1000 is entered, the result is:
Project 3, Phase III: Maximum value in any run less than initial integer
Enter integer ---> 1000
Initial integer: 703, Length: 109, Maximum value: 125252
703, 1055, 1583, 2375, 3563, 5345, 8018, 4009, 6014, 3007,
4511, 6767, 10151, 15227, 22841, 34262, 17131, 25697, 38546, 19273,
28910, 14455, 21683, 32525, 48788, 24394, 12197, 18296, 9148, 4574,
2287, 3431, 5147, 7721, 11582, 5791, 8687, 13031, 19547, 29321,
43982, 21991, 32987, 49481, 74222, 37111, 55667, 83501, 125252, 62626,
31313, 46970, 23485, 35228, 17614, 8807, 13211, 19817, 29726, 14863,
22295, 33443, 50165, 75248, 37624, 18812, 9406, 4703, 7055, 10583,
15875, 23813, 35720, 17860, 8930, 4465, 6698, 3349, 5024, 2512,
1256, 628, 314, 157, 236, 118, 59, 89, 134, 67,
101, 152, 76, 38, 19, 29, 44, 22, 11, 17,
26, 13, 20, 10, 5, 8, 4, 2, 1
For initial numbers less than or equal to 113 382, the largest value in any run is already attained using initial number 77 671, which gives length 149, but maximum value 785 412 368. Since this was obtained after dividing by 2, internally the largest value appearing is 1 570 824 736. Calculations involving 113 383 result in integer overflow for 32-bit integers, that is, larger than 231 - 1 = 2 147 483 647. In C, C++, or Java, one can use double type for calculations. This effectively gives exact integer arithmetic up to size 52 bits. In Java one can also use the long type, giving 64-bit integers. Finally, Java has the BigInteger class that provides integers of arbitrary size. You could try one of these methods to discover the mysterious overflow number in the run starting with 113 383.