diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index bb4040eb..c6511a5d 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -81,6 +81,7 @@ "lis.ml": 6, "list_ops.ml": 30, "luhn.ml": 2, + "magic_square.ml": 65, "mat_mul.ml": 621, "matrix_power.ml": 832040, "max_path_tree.ml": 11, diff --git a/lib/ocaml/baseline/magic_square.ml b/lib/ocaml/baseline/magic_square.ml new file mode 100644 index 00000000..f2b6ff5f --- /dev/null +++ b/lib/ocaml/baseline/magic_square.ml @@ -0,0 +1,27 @@ +let n = 5 + +let make_magic () = + let m = Array.init n (fun _ -> Array.make n 0) in + let row = ref 0 in + let col = ref (n / 2) in + for k = 1 to n * n do + m.(!row).(!col) <- k; + let nr = (!row - 1 + n) mod n in + let nc = (!col + 1) mod n in + if m.(nr).(nc) <> 0 then begin + row := (!row + 1) mod n + end else begin + row := nr; + col := nc + end + done; + m + +;; + +let m = make_magic () in +let sum_diag = ref 0 in +for i = 0 to n - 1 do + sum_diag := !sum_diag + m.(i).(i) +done; +!sum_diag diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 1f01de16..5b8a4a48 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _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-10 Phase 5.1 — magic_square.ml baseline (5×5 Siamese + construction → main-diagonal sum 65, the magic constant for n=5). + Place k=1 at (0, n/2); for each next k, move up-right with wrap- + around; if that cell is taken, move down one row instead. + Magic constant for an n×n square is n(n²+1)/2 = 5·26/2 = 65. + Tests 2D array via Array.init + Array.make, mod arithmetic with + the `(x - 1 + n) mod n` idiom for negative-safe wrap, nested + begin/end branches inside for-loop body. 166 baseline programs + total. - 2026-05-10 Phase 5.1 — matrix_power.ml baseline (Fibonacci via 2×2 matrix fast exponentiation, F(30) = 832040). [[1,1],[1,0]]^n has Fibonacci numbers in the top row; recursive O(log n) power