Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Hailstone Sequence Program

dpatel23
dpatel23 


Write a Java program (Hailstone.java) that contains the main method and another method named minHailstoneValue. The minHailstoneValue method takes integers n and m as parameters and returns the minimum value in a hailstone sequence of length m that begins with n. The values of n and m are given by the user, and you may assume that the user will enter positive integers. In a hailstone sequence, each value x is followed either by: (3x + 1) if x is odd (x/2) if x is even For example, if we start with 7 and we construct a sequence of length 10, we get: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20 In this case, the minimum value in the sequence is 7 (the number we started with). Therefore, the following call: minHailstoneValue(7, 10) should return 7. If we instead construct a sequence of length 20, we get: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1 In this case, the minimum value in the sequence is 1. Therefore, the call: minHailstoneValue(7, 20) should return 1. Example (user input underlined): Hailstone sequence Please enter a positive integer: 7 Please enter the length of the Hailstone sequence: 20 The min value in the sequence is 1 Attachments
Tags Clarifications

Answers

JAVA Solution

Seeker
Seeker 



Source code is attached with preview Attachments
hailstone.jpg (142 K) Download Preview

Hailstone.java (1 K) Download Preview
if(currentValue % 2 == 0){ currentValue = currentValue / 2; }else{ currentValue = 3 * currentValue + 1; } } System.out.println("Hailstone sequence: " + sequence); System.out.println("The minimum value in the sequence is " + minValue); } public static

Screenshots
Screenshot
Purchase Answer