From ed8aaf8af7f61255550ef061770803ec4a8dce45 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 00:26:46 +0000 Subject: [PATCH] ocaml: phase 5.1 euler36.ml baseline (sum of double-base palindromes <= 1000 = 1772) Numbers that read the same in base 10 and base 2: 1, 3, 5, 7, 9, 33, 99, 313, 585, 717 sum = 1772 Implementation: pal_dec n check decimal palindrome via index walk to_binary n build binary string via mod 2 / div 2 stack pal_bin n check binary palindrome euler36 limit scan 1..limit-1, sum where both palindromes Real PE36 uses 10^6 (answer 872187). 1000 takes ~9 minutes on contended host but stays within reasonable budget for the spec-level evaluator. 123 baseline programs total. --- lib/ocaml/baseline/euler36.ml | 41 ++++++++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 6 +++++ 3 files changed, 48 insertions(+) create mode 100644 lib/ocaml/baseline/euler36.ml diff --git a/lib/ocaml/baseline/euler36.ml b/lib/ocaml/baseline/euler36.ml new file mode 100644 index 00000000..e1c81c19 --- /dev/null +++ b/lib/ocaml/baseline/euler36.ml @@ -0,0 +1,41 @@ +let pal_dec 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 to_binary n = + if n = 0 then "0" + else + let buf = Buffer.create 32 in + let m = ref n in + let stack = ref [] in + while !m > 0 do + stack := (!m mod 2) :: !stack; + m := !m / 2 + done; + List.iter (fun d -> Buffer.add_string buf (string_of_int d)) !stack; + Buffer.contents buf + +let pal_bin n = + let s = to_binary 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 euler36 limit = + let sum = ref 0 in + for n = 1 to limit - 1 do + if pal_dec n && pal_bin n then sum := !sum + n + done; + !sum + +;; + +euler36 1000 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 2b64e4bb..d995d935 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -36,6 +36,7 @@ "euler29_small.ml": 15, "euler30_cube.ml": 1301, "euler34_small.ml": 145, + "euler36.ml": 1772, "euler40_small.ml": 15, "euler3.ml": 29, "euler4_small.ml": 9009, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 248b321f..f779c07c 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-10 Phase 5.1 — euler36.ml baseline (sum of double-base + palindromes ≤ 1000 = 1772). Numbers that read the same in base 10 + and base 2: 1, 3, 5, 7, 9, 33, 99, 313, 585, 717. Sum = 1772. + Real PE36 uses 10^6 (answer 872187); 1000 takes ~9 minutes on + contended host but fits within 480s timeout * inner-iteration + cost ratio. 123 baseline programs total. - 2026-05-10 Phase 5.1 — euler40_small.ml baseline (Champernowne digit-product at 1, 10, 100, 1000 = 1*1*5*3 = 15). Builds the Champernowne string until ≥1500 chars; tracks length separately