Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: HAILSTONE SEQUENCING

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HAILSTONE SEQUENCING

    This is the task:
    This puzzle involves sequences of integers known as Hailstone sequences. A Hailstone sequence begins with any positive integer and continues until it reaches 1. When a positive integer n (other than 1) appears in a Hailstone sequence, the integer that follows it is determined by the following rule:
    If n is even, the next integer is n/2.
    If n is odd, the next integer is 3n+1.
    Here are a couple of examples. The Hailstone sequence beginning with 6 is
    6, 3, 10, 5, 16, 8, 4, 2, 1
    The Hailstone sequence beginning with 7 is
    7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
    Hailstone sequences are intriguing to mathematicians. No one has ever found a Hailstone sequence that goes on forever and never bottoms out at 1. On the other hand, no one has ever proven that such a sequence cannot exist.
    Hailstone sequences get their name from the way that the numbers rise and fall before they finally make it all the way down to 1. And they can get really high before they crash back down. (So high, in fact, that 32-bit integers are too small for some of the questions that are coming up!)

    Your job is to answer the following questions about Hailstone sequences. You will almost certainly need to write some simple computer programs. If you aren't a programmer, please feel free to enlist the help of someone who is. When you post your log, please let us know what programming language you used.

    1. L(n) stands for the length of the Hailstone sequence beginning with n. For example, L(6) = 9 and L(7) = 17. What is L(7450)?

    2. If you make a list of all the integers n for which L(n) = 140 and sort it into ascending order, the first number in the list will be 731 and the second will be 737. What is the 47th number in the list?

    3. S(n) stands for the sum of all the numbers in the Hailstone sequence beginning with n. For example, S(6) = 55 and S(7) = 288. For what integer n does S(n) = 472151?

    4. H(n) stands for the number of times the Hailstone sequence beginning with n reaches its record high—a number higher than any it has reached before. (The starting point does not count as a record high.) For example, H(6) = 2 and H(7) = 3. What is H(140394587)?

    5. If you make a list of all the integers n for which H(n) = 20 and sort it into ascending order, 6887 will occupy position 1 on the list, 8161 will occupy position 2, and 39935 will occupy position 10. What position will 979547 occupy?

    I did the first two but i don't know how to do #3,4,5. PLEASE HELP ME!YOUR HELP WILL BE GREATLY APPRECIATED!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HAILSTONE SEQUENCING

    For what integer n does S(n) = 472151?
    Do you have an algorithm for solving that? If you do, post it and explain what problems you are having coding it in java.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HAILSTONE SEQUENCING

    Quote Originally Posted by Norm View Post
    Do you have an algorithm for solving that? If you do, post it and explain what problems you are having coding it in java.
    If n is even, the next integer is n/2.
    If n is odd, the next integer is 3n+1.

    --- Update ---

    this is my method so far...
    public static void question3()
    {
    /*
    1. S(n) stands for the sum of all the numbers in the Hailstone sequence beginning with n. For example, S(6) = 55 and
    S(7) = 288. For what integer n does S(n) = 472151?
    */
    System.out.println("Question 3:");
    int Sn = 0;
    int numList = 0;//numbers
    while(length != 472151)
    {
    while(Sn != 1)
    {
    Sn = Sn + 1;
    if(Sn% 2 == 0)
    {
    Sn = (Sn/2);
    }
    else
    {
    Sn = ((3*Sn)+1);
    }
    length = length+Sn;
    }

    }
    if(length == 472151)
    {
    System.out.println(numList);
    }

    }
    but whenever i run it it doesn't print out anything but question 3

    --- Update ---

    sorry but right underneath Sn = Sn +1; it should say numList = numList +1;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HAILSTONE SEQUENCING

    it should say
    Please post the complete new version of the code. Be sure to wrap the code in code tags:

    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HAILSTONE SEQUENCING

    public static void question3()
    {
       1.	S(n) stands for the sum of all the numbers in the Hailstone sequence beginning with n. For example, S(6) = 55 and 
       S(7) = 288. For what integer n does S(n) = 472151?

    System.out.println("Question 3:");

    int n = 1;
     start at one
    int Sn = 0;
    int numList = 0;
     numbers
    while(Sn != 472151)
     keep going until Sn(sum of all the hailstone sequences) = 472151
    {
    n = n + 1;
     start from 1 and keep going until you find a number whose Sn will equal 472151
    numList = n;[code = java] store n into numList [/code]


    while(n != 1)
    {
    if(n% 2 == 0)
    {
    n = (n/2);
    }
    else
    {
    n = ((3*n)+1);
    }
    Sn = Sn + n;
     every time you take hailstone sequence store it into Sn and keep adding the new n
    }

    }
    if(Sn == 472151)
    when sn=472151 print the orginal number
    {
    System.out.println(numList);
    }

    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HAILSTONE SEQUENCING

    Please put the whole program within one pair of code tags. Not just bits and pieces of it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HAILSTONE SEQUENCING

    public static void question3()
       {
       /*
       1.	S(n) stands for the sum of all the numbers in the Hailstone sequence beginning with n. For example, S(6) = 55 and 
       S(7) = 288. For what integer n does S(n) = 472151? 
       */
          System.out.println("Question 3:");
          int n = 1;// start at one
          int Sn = 0;
          int numList = 0;//numbers
          while(Sn != 472151)// keep going until Sn(sum of all the hailstone sequences) = 472151
          {
             n = n + 1;// start from 1 and keep going until you find a number whose Sn will equal 472151
             numList = n;
     
     
             while(n != 1)
             {
                if(n% 2 == 0)
                {
                   n = (n/2);
                }    
                else
                {
                   n = ((3*n)+1);
                }
                Sn = Sn + n;// every time you take hailstone sequence store it into Sn and keep adding the new n
             }           
     
          }
          if(Sn == 472151)//when sn=472151 print the orginal number
          {
             System.out.println(numList);
          }
     
       }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HAILSTONE SEQUENCING

    Can you explain what problems you are having writing the code for the algorithm?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HAILSTONE SEQUENCING

    Well my problem is that when i run my program, it keeps printing out 2 as the answer but the answer should be 8111. So i probably messed up with while(Sn != 472151 )

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HAILSTONE SEQUENCING

    For testing use the small values: 55 and 268.
    When the code works for those numbers then try the larger number.

    Try debugging the code by adding println() statements inside the loops that print out the values of all the variables the code uses.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HAILSTONE SEQUENCING

    I tried that but it didn't work......

    --- Update ---

    *it didn't help

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HAILSTONE SEQUENCING

    it didn't work
    What does "didn't work" mean? If there are errors, copy the full text of the error message and paste it here.

    What did the print out show you about how the code is computing values?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM

Tags for this Thread