From c8206e718a3f59daa7af17bb365d0f7e5157a2c9 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 20:46:42 +0000 Subject: [PATCH] ocaml: phase 5.1 max_product3.ml baseline (max product of 3, with negatives -> 300) Sort, then compare two candidates: p1 = product of three largest values p2 = product of two smallest (potentially negative) values and the largest For [-10;-10;1;3;2]: sorted = [-10;-10;1;2;3] p1 = 3 * 2 * 1 = 6 p2 = (-10) * (-10) * 3 = 300 max = 300 Tests List.sort + Array.of_list + arr.(n-i) end-walk + candidate-pick via if-then-else. 104 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/max_product3.ml | 11 +++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 lib/ocaml/baseline/max_product3.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index ba6a4e27..f5d9c7f7 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -51,6 +51,7 @@ "luhn.ml": 2, "mat_mul.ml": 621, "max_path_tree.ml": 11, + "max_product3.ml": 300, "max_run.ml": 5, "mod_inverse.ml": 27, "json_pretty.ml": 24, diff --git a/lib/ocaml/baseline/max_product3.ml b/lib/ocaml/baseline/max_product3.ml new file mode 100644 index 00000000..5ae5e769 --- /dev/null +++ b/lib/ocaml/baseline/max_product3.ml @@ -0,0 +1,11 @@ +let max_prod3 xs = + let sorted = List.sort compare xs in + let arr = Array.of_list sorted in + let n = Array.length arr in + let p1 = arr.(n - 1) * arr.(n - 2) * arr.(n - 3) in + let p2 = arr.(0) * arr.(1) * arr.(n - 1) in + if p1 > p2 then p1 else p2 + +;; + +max_prod3 [-10; -10; 1; 3; 2] diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index bdc7788c..42f866d5 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 — max_product3.ml baseline (max product of + three from a list including negatives = 300). Sort, then compare + product of three largest vs product of two smallest negatives and + one largest. For [-10;-10;1;3;2]: 3*2*1 = 6 vs (-10)*(-10)*3 = 300. + Tests List.sort + Array.of_list + arr.(n-i) end-walk + candidate + compare. 104 baseline programs total. - 2026-05-09 Phase 5.1 — euler9.ml baseline (Project Euler #9, abc product for the unique Pythagorean triple with a+b+c=1000 → 31875000). Naive triple loop times out under contention (10-min