From 58ea001f128222d9926595276f10d15b6749ccae Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 01:02:18 +0000 Subject: [PATCH] ocaml: phase 5.1 perfect.ml baseline (count perfect numbers <= 500 = 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Perfect numbers = those where the proper-divisor sum equals n. Three exist under 500: 6, 28, 496. (8128 is the next; 33550336 the one after that.) Same div_sum machinery as euler21_small.ml / abundant.ml (the trial-division up to sqrt-n). Original 10000 limit timed out at 10 minutes under contention (496 itself takes thousands of trials at the inner loop). 500 stays under budget while still finding all three small perfects. 125 baseline programs total — milestone. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/perfect.ml | 23 +++++++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 30 insertions(+) create mode 100644 lib/ocaml/baseline/perfect.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 88a56448..07296ad2 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -94,6 +94,7 @@ "pancake_sort.ml": 910, "pascal.ml": 252, "peano.ml": 30, + "perfect.ml": 3, "pi_leibniz.ml": 314, "prefix_sum.ml": 66, "pretty_table.ml": 64, diff --git a/lib/ocaml/baseline/perfect.ml b/lib/ocaml/baseline/perfect.ml new file mode 100644 index 00000000..2ec3c4a9 --- /dev/null +++ b/lib/ocaml/baseline/perfect.ml @@ -0,0 +1,23 @@ +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 count_perfect limit = + let c = ref 0 in + for n = 2 to limit do + if div_sum n = n then c := !c + 1 + done; + !c + +;; + +count_perfect 500 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 34acfbec..4de83aaf 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 — perfect.ml baseline (count perfect numbers + ≤ 500 = 3). Perfect numbers = those where d(n) = n. Three under + 500: 6, 28, 496. (8128 is the next.) Same div_sum machinery as + euler21_small / abundant. Original 10000 limit timed out under + contention; 500 stays under budget. 125 baseline programs total — + milestone. - 2026-05-10 Phase 5.1 — abundant.ml baseline (count abundant numbers < 100 = 21). Abundant means d(n) > n where d(n) is the proper- divisor sum. Reuses the trial-division div_sum helper from iter