84. Hailstone Sequence, Redux

This program is very similar to the preceding one. The main difference is that this program builds a list of sequence lengths and the preceding solution builds a list containing a single sequence.

You could build the list of lengths by simply using the FindHailstoneSequence method from the preceding solution to create each sequence in turn. The sequences aren't too long, at least for relatively small starting values, so that would work. However, there's a shortcut that you can use to make generating the lengths even faster.

The following method demonstrates that shortcut to find the lengths of hailstone sequences starting with numbers between 1 and max:

// Return the lengths of hailstone sequences for starting numbers 1 
// through max.
private List<int> FindHailstoneLengths(int max)
{
// Create an array to hold the lengths.
int[] lengths = new int[max + 1];

// Fill the lengths.
for (int i = 1; i <= max; i++)
{
int length = 1;
int number = i;
while (number != 1)
{
// See if we know the length for the current number.
if ((number <= max) && (lengths[number] > 0))
{
// We know lengths[number].
length += lengths[number] - 1;
break;
}

// Go to the next number.
length++;
if (number % 2 == 0)
number = number / 2;
else
number = 3 * number + 1;
}
lengths[i] = length;
}

// Convert the array to a list and remove the entry 0.
List<int> lengthList = new List<int>(lengths);
lengthList.RemoveAt(0);

return lengthList;
}

The method creates an array to hold the sequence lengths and then enters a loop to calculate those lengths. For each value within the loop, the code initializes a length counter. It then uses a while loop to generate values in the sequence until it reaches the value 1.

After generating the next number in a sequence, the method checks the lengths array to see if it already contains a length for the sequence starting at that number.

For example, consider the sequence starting at the value 8. The next value in the sequence is 4, but 4 is smaller than 8. If we're currently calculating the length of the 8 sequence, then we have already calculated the length of the 4 sequence. It took one step to get to this point, so the length of the 8 sequence is 1 greater than the length of the 4 sequence.

The length counter starts at 1 and the while loop adds steps to reach a previously calculated value, so we need to subtract 1 when we add the previously calculated value's length. (If you walk through the code for the value 8, you'll see how that works.) After adding the new value to the length counter, the code breaks out of its while loop.

After the while loop ends, the method saves the calculated sequence length in the lengths array and moves on to the next sequence.

After the method finishes finding all of the sequence lengths, it converts the lengths array into a list, removes the first entry (because no hailstone sequence is defined starting with the value 0), and returns the list.

The rest of the solution is very similar to the preceding one. Download the HailstoneSequenceRedux example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset