From 7de014cd7565c4152e7c7d61d66ea5868f84dcad Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 13:30:46 +0000 Subject: [PATCH] ocaml: phase 5.1 hailstone.ml baseline (Collatz length from 27 = 111 steps) Iterative Collatz / hailstone sequence: let collatz_length n = let m = ref n in let count = ref 0 in while !m > 1 do if !m mod 2 = 0 then m := !m / 2 else m := 3 * !m + 1; count := !count + 1 done; !count 27 is the famous 'long-running' Collatz starter. Reaches a peak of 9232 mid-sequence and takes 111 steps to bottom out at 1. 64 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/hailstone.ml | 13 +++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 20 insertions(+) create mode 100644 lib/ocaml/baseline/hailstone.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index c6706fe0..65062630 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -28,6 +28,7 @@ "frequency.ml": 5, "gcd_lcm.ml": 60, "grep_count.ml": 3, + "hailstone.ml": 111, "hanoi.ml": 1023, "fizzbuzz.ml": 57, "flatten_tree.ml": 28, diff --git a/lib/ocaml/baseline/hailstone.ml b/lib/ocaml/baseline/hailstone.ml new file mode 100644 index 00000000..4f9e3f9b --- /dev/null +++ b/lib/ocaml/baseline/hailstone.ml @@ -0,0 +1,13 @@ +let collatz_length n = + let m = ref n in + let count = ref 0 in + while !m > 1 do + if !m mod 2 = 0 then m := !m / 2 + else m := 3 * !m + 1; + count := !count + 1 + done; + !count + +;; + +collatz_length 27 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 9eee200c..face6a09 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — hailstone.ml baseline (Collatz length, + starting from 27 → 111 steps to reach 1). Iterative while-loop + applies `n / 2` if even, `3n + 1` if odd, counting steps. 27 is + the famous "long-running" Collatz starter that produces 111 + iterations and a peak value of 9232 mid-sequence. 64 baseline + programs total. - 2026-05-09 Phase 5.1 — twosum.ml baseline (LeetCode #1, hashtable one-pass, index-sum 1+3+1 = 5). Walks list with List.iteri, checking if `target - x` is already in the hashtable; if yes, the