36 lines
876 B
Plaintext
36 lines
876 B
Plaintext
fun hail (x = 1) = [1]
|
|
| (x rem 2 = 0) = x :: hail (x div 2)
|
|
| x = x :: hail (x * 3 + 1)
|
|
|
|
fun hailstorm
|
|
([], i, largest, largest_at) = (largest_at, largest)
|
|
| (x :: xs, i, largest, largest_at) =
|
|
let
|
|
val k = len (hail x)
|
|
in
|
|
if k > largest then
|
|
hailstorm (xs, i + 1, k, i)
|
|
else
|
|
hailstorm (xs, i + 1, largest, largest_at)
|
|
end
|
|
| (x :: xs) = hailstorm (x :: xs, 1, 0, 0)
|
|
|
|
;
|
|
|
|
val h27 = hail 27;
|
|
print "hailstone sequence for the number 27 has ";
|
|
print ` len (h27);
|
|
print " elements starting with ";
|
|
print ` sub (h27, 0, 4);
|
|
print " and ending with ";
|
|
print ` sub (h27, len(h27)-4, len h27);
|
|
println ".";
|
|
|
|
val biggest = hailstorm ` iota (100000 - 1);
|
|
|
|
print "The number less than 100,000 which has the longest ";
|
|
print "hailstone sequence is at element ";
|
|
print ` ref (biggest, 0);
|
|
print " and is of length ";
|
|
println ` ref (biggest, 1);
|