From 87f9a84365b2824ce481978edf55b30a59915d49 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 23:27:15 +0000 Subject: [PATCH] ocaml: phase 5.1 euler21_small.ml baseline (sum of amicable numbers <= 300 = 504) div_sum computes proper divisor sum via trial division up to sqrt(n): let div_sum n = let s = ref 1 in let i = ref 2 in while !i * !i <= n do if n mod !i = 0 then begin s := !s + !i; let q = n / !i in if q <> !i then s := !s + q end; i := !i + 1 done; if n = 1 then 0 else !s Outer loop finds amicable pairs (a, b) with d(a) = b, d(b) = a, a != b. Only pair under 300 is (220, 284); 220 + 284 = 504. Real PE21 uses 10000 (answer 31626). 300 keeps the run under budget while exercising the same divisor-sum trick. 119 baseline programs total. --- lib/ocaml/baseline/euler21_small.ml | 25 +++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 5 +++++ 3 files changed, 31 insertions(+) create mode 100644 lib/ocaml/baseline/euler21_small.ml diff --git a/lib/ocaml/baseline/euler21_small.ml b/lib/ocaml/baseline/euler21_small.ml new file mode 100644 index 00000000..45e64e98 --- /dev/null +++ b/lib/ocaml/baseline/euler21_small.ml @@ -0,0 +1,25 @@ +let div_sum n = + let s = ref 1 in + let i = ref 2 in + while !i * !i <= n do + if n mod !i = 0 then begin + s := !s + !i; + let q = n / !i in + if q <> !i then s := !s + q + end; + i := !i + 1 + done; + if n = 1 then 0 else !s + +let euler21 limit = + let total = ref 0 in + for a = 2 to limit do + let b = div_sum a in + if b <> a && b > a && b <= limit && div_sum b = a then + total := !total + a + b + done; + !total + +;; + +euler21 300 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 5f65b1ab..55e60ad7 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -30,6 +30,7 @@ "euler10.ml": 1060, "euler14.ml": 97, "euler2.ml": 4613732, + "euler21_small.ml": 504, "euler25.ml": 55, "euler28.ml": 261, "euler30_cube.ml": 1301, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 9aed8e0b..73af64c8 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 — 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) + with d(a) = b, d(b) = a, a ≠ b. Only pair under 300 is (220, 284). + Real PE21 uses 10000 (answer 31626). 119 baseline programs total. - 2026-05-09 Phase 5.1 — euler30_cube.ml baseline (sum of numbers equal to sum of cubes of their digits, ≤999 = 1301). The full numbers are 153 + 370 + 371 + 407 = 1301. PE30 proper uses 5th