Results 1 to 3 of 3
 02212013, 06:47 AM #1Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Need help with program for Hailstone Sequence
The Question:
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:
Num 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.
 02212013, 06:51 AM #2Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Re: Need help with program for Hailstone Sequence
I manged to do the first half which is:
//prompt user to enter range
//check if both no.s ve+ indivly
//check if lo > or = to hi
if wrong prompt user to input ve+ num in right order
but now I am trying to fig out an algorithm for finding the hailstone sequence would you mind helping please.
Here is the code ive written its in complete..only the first half i mentioned above works...but i have started a skeleton for the second half...
Java Code:import java.util.*; public class Conjecture { public static void main(String[] args) { //create scanner object and prompt for input of values Scanner input = new Scanner(System.in); System.out.print("Enter starting number of the range: "); int lo = input.nextInt(); System.out.print("Enter ending number of the range: "); int hi = input.nextInt(); //check if both no.s ve+ indivly and if the first value is > second one do { if(lo < 0  hi < 0) System.out.println("Invalid! Remember numbers must be be possitive and in the right order."); while (lo < 0) { System.out.println("Try again! Improper range value."); System.out.print("Enter starting number of the range: "); lo = input.nextInt(); } while (hi < 0) { System.out.println("Try again! Improper range value."); System.out.print("Enter ending number of the range: "); hi = input.nextInt(); } } while (lo < 0  hi < 0); if(lo > hi) { System.out.println("Try again! First value must be lower than second value."); System.out.print("Enter starting number of the range: "); lo = input.nextInt(); System.out.print("Enter ending number of the range: "); hi = input.nextInt(); } for(int num = lo; num <=hi; num++) { while ( num % 2 == 0) { int half = num / 2 num = half } while ( num % 2 == 1) { } } } }
 02212013, 07:08 AM #3Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Re: Need help with program for Hailstone Sequence
UPDATE TO SECOND HALF CODE:
Java Code:int cycleLength = 0; int numOfCycles = 0; for(int num = lo; num <= hi; num++) { int cycleLength_orgi = cycleLength; if( num % 2 == 0) { do { num = num / 2; if (num % 2 == 0) { num = num / 2; } esle { num = 3 * num + 1; } ++numOfCycles; } while (num > 1); } else if( num % 2 == 1) { do { num = 3 * num + 1; if (num % 2 == 0) { num = num / 2; } esle { num = 3 * num + 1; } ++numOfCycles; } while (num > 1); } }
Last edited by abi; 02212013 at 07:39 AM.
Similar Threads

Fibonacci sequence program that only prints the n'th digit in the sequence
By erichfx in forum New To JavaReplies: 1Last Post: 01232013, 06:06 PM 
Sum sequence
By Aero in forum New To JavaReplies: 2Last Post: 09262011, 06:30 PM 
Record call trace of a java program and generate UML Sequence Diagrams
By ali_naqvi in forum Java SoftwareReplies: 0Last Post: 04192010, 06:40 PM 
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12312009, 01:30 AM
Bookmarks