Java Reference
In-Depth Information
The pseudocode for computing the minimum is a slight variation of this code:
initialize min either to highest possible value or to first value.
for (all numbers to examine) {
obtain "next".
if (next < min) {
min = next.
}
}
To help you understand this better, let's put the pseudocode into action with a
real problem. In mathematics, there is an open problem that involves what are
known as hailstone sequences. These sequences of numbers often rise and fall in
unpredictable patterns, which is somewhat analogous to the process that forms
hailstones.
A hailstone sequence is a sequence of numbers in which each value x is fol-
lowed by:
(3 x
1), if x is odd
x
2 b
a
,
if x is even
For example, if you start with 7 and construct a sequence of length 10, you get the
sequence:
7, 22, 11, 34, 17, 52, 26, 13, 40, 20
In this sequence, the maximum and minimum values are 52 and 7, respectively. If
you extend this computation to a sequence of length 20, you get the sequence:
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1
In this case, the maximum and minimum values are 52 and 1, respectively.
You will notice that once 1, 2, or 4 appears in the sequence, the sequence repeats
itself. It is conjectured that all integers eventually reach 1, like hailstones that fall to
the ground. This is an unsolved problem in mathematics. Nobody has been able to
disprove it, but nobody has proven it either.
Let's write a method that takes a starting value and a sequence length and prints
the maximum and minimum values obtained in a hailstone sequence formed with that
starting value and of that length. Our method will look like this:
public static void printHailstoneMaxMin(int value, int length) {
...
}
We can use the starting value to initialize max and min :
int min = value;
int max = value;
 
Search WWH ::




Custom Search