Results 1 to 3 of 3
Thread: Helppp!!!!
 02212013, 11:52 PM #1Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Helppp!!!!
I can seem to figure out what is wrong with my code for the hailstone sequence. I dont get an output i.e. it seems like it is running continuously for a long time because all I get is the cursor.
I will post the question as a comment below:
Java Code:public class HailStone_SecondHalf { public static void main(String[] args) { int num; for(num = 1; num <= 5; num++) { int num_withHiCycles = 0; int cycleLength = 0; int numOfCycles = 0; int even = 0; int odd = 1; int num_EO_check = num % 2; if( num_EO_check == even ) { do { num = num / 2; if (num % 2 == 0) { num = num / 2; ++numOfCycles; } else if ( num % 2 == 1) { num = 3 * num + 1; ++numOfCycles; } ++numOfCycles; if(numOfCycles > cycleLength) { cycleLength = numOfCycles; num_withHiCycles = num; } } while (num > 1); } else if( num_EO_check == odd) { do { num = 3 * num + 1; if (num % 2 == 0) { num = num / 2; ++numOfCycles; } else if ( num % 2 == 1) { num = 3 * num + 1; ++numOfCycles; } ++numOfCycles; if(numOfCycles > cycleLength) { cycleLength = numOfCycles; num_withHiCycles = num; } } while (num > 1); } System.out.print(num + numOfCycles ); System.out.print("The number "+ num_withHiCycles + " has the longest cycle length of " + cycleLength + "."); } }
Last edited by abi; 02222013 at 01:58 AM.
 02222013, 02:00 AM #2Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Re: Helppp!!!!
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.
 02222013, 04:51 AM #3
Re: Helppp!!!!
Please go through the Forum Rules, particularly the third paragraph.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads

Helppp ASAP please MAGIC SQUARE
By nfurn in forum New To JavaReplies: 2Last Post: 11202012, 06:25 PM 
urgent helppp !!!!!
By rishu2003 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04122011, 04:13 PM 
ComplexNumber.java I NEED HELPPP
By db7_turbo in forum Advanced JavaReplies: 4Last Post: 04142010, 03:16 PM 
helppp
By dadaman in forum New To JavaReplies: 2Last Post: 04302008, 01:01 PM
Bookmarks