Results 1 to 3 of 3
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default 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.

  2. #2
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default 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)
          {
             
          }
    
       }
    
     } 
    }

  3. #3
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default 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);
          }     
               
    
       }
    what i am focusing on now is trying to see how i can count the number of cycles for each no. within the range and then display the number with the highest no. of cycles along with the number itself...help me out please....TY
    Last edited by abi; 02-21-2013 at 08:39 AM.

Similar Threads

  1. Replies: 1
    Last Post: 01-23-2013, 07:06 PM
  2. Sum sequence
    By Aero in forum New To Java
    Replies: 2
    Last Post: 09-26-2011, 07:30 PM
  3. Replies: 0
    Last Post: 04-19-2010, 07:40 PM
  4. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •