From 21dbd195d5f2020507f6faad0c06ec9b9392458a Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 23:37:36 +0000 Subject: [PATCH] ocaml: phase 5.1 euler29_small.ml baseline (distinct a^b for 2<=a,b<=5 = 15) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compute every power a^b for a, b in [2..N] and count distinct values. Hashtbl as a set with unit-payload (iter-168 idiom): let euler29 n = let h = Hashtbl.create 64 in for a = 2 to n do for b = 2 to n do let p = ref 1 in for _ = 1 to b do p := !p * a done; Hashtbl.replace h !p () done done; Hashtbl.length h For N=5: 16 powers minus one duplicate (4^2 = 2^4 = 16) -> 15. Real PE29 uses N=100 (answer 9183). 120 baseline programs total — milestone. --- lib/ocaml/baseline/euler29_small.ml | 14 ++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 lib/ocaml/baseline/euler29_small.ml diff --git a/lib/ocaml/baseline/euler29_small.ml b/lib/ocaml/baseline/euler29_small.ml new file mode 100644 index 00000000..602cf7cb --- /dev/null +++ b/lib/ocaml/baseline/euler29_small.ml @@ -0,0 +1,14 @@ +let euler29 n = + let h = Hashtbl.create 64 in + for a = 2 to n do + for b = 2 to n do + let p = ref 1 in + for _ = 1 to b do p := !p * a done; + Hashtbl.replace h !p () + done + done; + Hashtbl.length h + +;; + +euler29 5 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 55e60ad7..5255b53d 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -33,6 +33,7 @@ "euler21_small.ml": 504, "euler25.ml": 55, "euler28.ml": 261, + "euler29_small.ml": 15, "euler30_cube.ml": 1301, "euler3.ml": 29, "euler4_small.ml": 9009, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 73af64c8..afec331a 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,11 @@ _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 — euler29_small.ml baseline (distinct a^b for + 2≤a,b≤5 = 15). 16 powers minus the one duplicate (4^2 = 2^4 = 16) + → 15 distinct values. Hashtbl as a set with unit-payload (the + iter-168 idiom). Real PE29 uses N=100 (answer 9183). 120 baseline + programs total — milestone. - 2026-05-09 Phase 5.1 — euler21_small.ml baseline (sum of amicable numbers ≤ 300 = 504). div_sum computes proper divisor sum via trial division up to √n; outer loop finds amicable pairs (a, b)