Hailstone

Aug 25, 2016 at 1:07pm
I'm looking for help in a section of my C++ program. I need to "write a function that takes an integer n and returns the start value k, from 1 to n, that has the longest length."

I have to use #include <cstdio>, and cannot use recursion.

Please help!!

Thanks!
Aug 25, 2016 at 1:10pm
What is that 'start value k'? There's some context missing here.

What have you tried so far?
Aug 25, 2016 at 1:26pm
I'm trying to get the bottom sentence with quotes around it:

What number shall I start with? 8
The hailstone sequence starting at 8 is:
8 4 2 1
The length of the sequence is 4.
The largest number in the sequence is 8.
The longest hailstone sequence starting with a number up to 8 has length 17
"The longest hailstone sequence starting with a number up to 8 begins with 7"
Aug 25, 2016 at 1:35pm
Ah, OK, I see.

The definition of the Hailstone sequence is:

If n is even, then divide n by 2 and n' = n / 2
If n is odd, then multiple by 3 and add 1, so n' = 3n + 1

Every sequence will eventually get to a repeating sequence of 4 2 1 4 1 ...

Have you written code to try to implement that sequence?
Aug 26, 2016 at 3:43am
I've gotten all of the above sentences coded except for the last one. Below is the code I have for the second to last sentence that determines the largest length. Also, I have no code yet for the last sentence.

<code>
int largestLength(int n)
{
if (n == 1)
{
return 1;
}
else
{
return max(length(n),largestLength(length(n)));
}
}
</code>
Aug 26, 2016 at 12:22pm
If you're able to find the length of the longest sequence then you must have found the starting point of that sequence. That's the value that you want to return.
Topic archived. No new replies allowed.