33 lines
814 B
Plaintext
33 lines
814 B
Plaintext
fun hailstone_step(x: int): int =
|
|
if (x % 2) == 0
|
|
then x/2
|
|
else (3*x) + 1
|
|
|
|
fun hailstone_seq(x: int): []int =
|
|
let capacity = 100
|
|
let i = 1
|
|
let steps = replicate capacity (-1)
|
|
let steps[0] = x
|
|
loop ((capacity,i,steps,x)) = while x != 1 do
|
|
let (steps, capacity) =
|
|
if i == capacity then
|
|
(concat steps (replicate capacity (-1)),
|
|
capacity * 2)
|
|
else (steps, capacity)
|
|
let x = hailstone_step x
|
|
let steps[i] = x
|
|
in (capacity, i+1, steps, x)
|
|
in #1 (split i steps)
|
|
|
|
fun hailstone_len(x: int): int =
|
|
let i = 1
|
|
loop ((i,x)) = while x != 1 do
|
|
(i+1, hailstone_step x)
|
|
in i
|
|
|
|
fun max (x: int) (y: int): int = if x < y then y else x
|
|
|
|
fun main (x: int) (n: int): ([]int, int) =
|
|
(hailstone_seq x,
|
|
reduce max 0 (map hailstone_len (map (1+) (iota (n-1)))))
|