ocaml: phase 5.1 perfect.ml baseline (count perfect numbers <= 500 = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s

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.
This commit is contained in:
2026-05-10 01:02:18 +00:00
parent da96a79104
commit 58ea001f12
3 changed files with 30 additions and 0 deletions

View File

@@ -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,

View File

@@ -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