From fb0e83d3a1ad45b9a55d1872d8f0f4472ad2866e Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 01:29:52 +0000 Subject: [PATCH] ocaml: phase 5.1 palindrome_sum.ml baseline (sum of 3-digit palindromes = 49500) let palindrome_sum lo hi = let total = ref 0 in for n = lo to hi do if is_pal n then total := !total + n done; !total palindrome_sum 100 999 = 49500 There are 90 three-digit palindromes (form aba; 9 choices for a, 10 for b). Average value 550, sum 49500. Companion to palindrome.ml (predicate-only) and paren_depth.ml. 127 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/palindrome_sum.ml | 19 +++++++++++++++++++ plans/ocaml-on-sx.md | 4 ++++ 3 files changed, 24 insertions(+) create mode 100644 lib/ocaml/baseline/palindrome_sum.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index f2b22765..bb8e8c68 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -89,6 +89,7 @@ "mutable_record.ml": 10, "option_match.ml": 5, "palindrome.ml": 4, + "palindrome_sum.ml": 49500, "paren_depth.ml": 7, "partition.ml": 3025, "pancake_sort.ml": 910, diff --git a/lib/ocaml/baseline/palindrome_sum.ml b/lib/ocaml/baseline/palindrome_sum.ml new file mode 100644 index 00000000..dc3fd3e4 --- /dev/null +++ b/lib/ocaml/baseline/palindrome_sum.ml @@ -0,0 +1,19 @@ +let is_pal n = + let s = string_of_int n in + let len = String.length s in + let p = ref true in + for i = 0 to len / 2 - 1 do + if s.[i] <> s.[len - 1 - i] then p := false + done; + !p + +let palindrome_sum lo hi = + let total = ref 0 in + for n = lo to hi do + if is_pal n then total := !total + n + done; + !total + +;; + +palindrome_sum 100 999 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 312d14ae..64e0a2e2 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,10 @@ _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 — palindrome_sum.ml baseline (sum of 3-digit + palindromes = 49500). 90 palindromes between 100 and 999 (form + aba; 9 choices for a, 10 for b). Sum = 49500 = 90 * 550 (mean + is 550). 127 baseline programs total. - 2026-05-10 Phase 5.1 — triangle_div.ml baseline (first triangle number with > 10 divisors = 120). PE12 with target 10. T(15) = 120 has 16 divisors {1,2,3,4,5,6,8,10,12,15,20,24,30,40,60,120} —