|
|
Need help to write Java code based on the following question :
If possible anyone canhelp with the code.
----------------------------------------------------------------------------
Consider the following problem, defined for positive integers:
Choose a number.
Repeat the following:
• If the number is 1, end.
• If the number is even, divide it in half.
• If the number is odd (but not1), multiply it by 3 and add 1.
Example: If you start with the number 17, you get the following sequence :
17- this is odd, so multiply by 3 and add 1, getting 52
52- this is even, so divide by 2, getting 26
26- this is even, so divide by 2
13- this is odd, so multiply by 3 and add 1
40- this is even
20-this is even
10- this is even
5- this is odd
16- this is even
8- this is even
4- this is even
2- this is even
1- stop
The question of whether this sequence ends with 1 for every integer is an unsolved problem:
No one has yet proved that the process always results in 1, and no one has yet found a counterexample.
a. Write a class Sequence that has the method
Public int[] getSequence(int seed);
That, given any integer input, will returns an array of integers containing the sequence for that number. For example, for the integer 17, the method returns the array
{52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}
b. Add another method to class Sequence:
Public void displaySequence(int seed);
That, given any integer input, print to the console a line showing the sequence as follows:
first the seed number, then a colon and space, then the sequence of numbers, separated with single spaces. For example, for the integer 17 , you would print the single line :
17: 52 26 13 40 20 10 5 16 8 4 2 1
c. Create a main class, with a main routine that displays all sequences for seed
numbers between 1 and 20.