Hi, I need this answered by 11:OO P.M Central, today. The answer needs to be eas
ID: 3528779 • Letter: H
Question
Hi, I need this answered by 11:OO P.M Central, today. The answer needs to be easy to copy and paste, and ready to compile into a class file. We have only covered basic, basic java programming so far, and have learned if/else statements, and for, while, and do/while loops. Here is the assignment. Hailstone Sequence (Due 22 February 2013) Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply by 3 and add 1 to obtain 3 n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will eventually reach 1. This conjecture has never been proved. But it has been verified on the computer for all starting values up to 20 * 258. To disprove this conjecture one has to find a single starting number that goes into a cycle that does not contain 1. The function can be written as follows: f ( n ) = ( n / 2 ) if n is even f ( n ) = ( 3 * n + 1 ) if n is odd For a given starting number, this function generates a series of numbers ending at 1 that is called the hailstone sequence. The hailstone sequences for starting numbers 7, 8, and 9 are: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 8 4 2 1 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 All sequences tested so far end in an endless cycle of 4, 2, and 1. Hence the sequences are halted when they reach 1. We will define the cycle length of a sequence to be the number of steps it takes from the starting number to reach 1. Here are the cycle lengths of a few starting numbers: Number Cycle Length 1 0 2 1 3 7 4 2 5 5 In your program you will verify this conjecture in a user defined range. You will prompt the user to enter the first number of the range and the last number. You will check if both numbers are positive (> 0) individually and then you will check that the first number in the user defined range is less than or equal to the last number in that range. Use nested loops to accomplish this. Keep prompting the user to input positive numbers in the right order. Once the beginning and ending of the range have been verified, your program will compute the cycle length for each of the numbers in that range inclusive of the end points. Your program will print out the number that has the largest cycle length and what that cycle length is. Your sample session will look like this: Enter starting number of the range: 1 Enter ending number of the range: 5 The number 3 has the longest cycle length of 7. In your program all your computations will be in the method main(). You must use nested loops to obtain your results. The class that you will be writing will be called Hailstone. We will be looking at good documentation, design, and adherence to the coding convention mentioned below. You must use Scanner for your input.Explanation / Answer
import java.util.Scanner; public class HailStone { static Scanner MyScanner = new Scanner(System.in); public static void main(String[] args) { System.out.println("This program will generate the HailStone sequence. "); System.out.println("Enter a number: "); int num = MyScanner.nextInt(); //Taking input from user while(num>1) { if (num%2 == 0) { num /= 2; //Dividing num by 2 if it is even System.out.print(num+" "); } else { num = (num*3)+ 1; // Adding num*3 + 1 to num if the num is odd System.out.print(num+" "); } } } }run this in ellipse
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.