diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index c2530159..7da9235c 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -85,6 +85,7 @@ "fizzbuzz.ml": 57, "flatten_tree.ml": 28, "flood_fill.ml": 7, + "floyd_cycle.ml": 8, "floyd_warshall.ml": 9, "lis.ml": 6, "list_ops.ml": 30, diff --git a/lib/ocaml/baseline/floyd_cycle.ml b/lib/ocaml/baseline/floyd_cycle.ml new file mode 100644 index 00000000..60ecec9f --- /dev/null +++ b/lib/ocaml/baseline/floyd_cycle.ml @@ -0,0 +1,29 @@ +let f x = (x * 2 + 5) mod 17 + +let floyd_cycle x0 = + let slow = ref x0 in + let fast = ref x0 in + let meet = ref false in + while not !meet do + slow := f !slow; + fast := f (f !fast); + if !slow = !fast then meet := true + done; + slow := x0; + let mu = ref 0 in + while !slow <> !fast do + slow := f !slow; + fast := f !fast; + mu := !mu + 1 + done; + let lam = ref 1 in + fast := f !slow; + while !slow <> !fast do + fast := f !fast; + lam := !lam + 1 + done; + !mu * 100 + !lam + +;; + +floyd_cycle 1 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index fe1ccc6b..3548ce41 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,17 @@ _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-11 Phase 5.1 — floyd_cycle.ml baseline (Floyd's tortoise + -and-hare cycle detection on f(x) = (2x+5) mod 17; μ=0 λ=8 → + encoded 8). Three phases: (1) advance slow/fast until they + collide inside the cycle, (2) restart slow from x0 and advance + both by 1 until they meet — the count is μ (tail length), + (3) advance fast around the cycle once until it meets slow — the + count is λ (cycle length). For x0=1, f(1)=7, f(7)=2, … cycles + through all 8 distinct values 1,7,2,9,6,0,5,15 before returning + to 1, so μ=0 λ=8 → 0*100+8=8. Tests three sequential while + loops sharing ref state, double-step `fast := f (f !fast)`, + meeting-condition flag pattern. 185 baseline programs total. - 2026-05-11 Phase 5.1 — kth_two.ml baseline (8th smallest in merged [1;3;5;7;9;11;13] ∪ [2;4;6;8;10;12] = 8). Two-pointer merge that advances the smaller-head pointer k times. Pick rule: