# HAILSTONE SEQUENCING

• November 3rd, 2013, 08:02 PM
leeminho
HAILSTONE SEQUENCING
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?

• November 4th, 2013, 05:43 AM
Norm
Re: HAILSTONE SEQUENCING
Quote:

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.
• November 4th, 2013, 06:55 AM
leeminho
Re: HAILSTONE SEQUENCING
Quote:

Originally Posted by Norm
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;
• November 4th, 2013, 07:17 AM
Norm
Re: HAILSTONE SEQUENCING
Quote:

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

[code=java]
[/code]
to get highlighting and preserve formatting.
• November 4th, 2013, 07:33 AM
leeminho
Re: HAILSTONE SEQUENCING
public static void question3()
{
Code java:

``` 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;
Code java:

` start at one`
int Sn = 0;
int numList = 0;
Code java:

` numbers`
while(Sn != 472151)
Code java:

` keep going until Sn(sum of all the hailstone sequences) = 472151`
{
n = n + 1;
Code java:

` 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;
Code java:

` every time you take hailstone sequence store it into Sn and keep adding the new n`
}

}
if(Sn == 472151)
Code java:

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

}
• November 4th, 2013, 07:43 AM
Norm
Re: HAILSTONE SEQUENCING
Please put the whole program within one pair of code tags. Not just bits and pieces of it.
• November 4th, 2013, 08:55 AM
leeminho
Re: HAILSTONE SEQUENCING
Code java:

```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); }   }```
• November 4th, 2013, 08:58 AM
Norm
Re: HAILSTONE SEQUENCING
Can you explain what problems you are having writing the code for the algorithm?
• November 4th, 2013, 09:05 AM
leeminho
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 )
• November 4th, 2013, 09:22 AM
Norm
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.
• November 4th, 2013, 09:30 AM
leeminho
Re: HAILSTONE SEQUENCING
I tried that but it didn't work......

--- Update ---

*it didn't help
• November 4th, 2013, 09:31 AM
Norm
Re: HAILSTONE SEQUENCING
Quote:

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?