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.
24 lines
390 B
OCaml
24 lines
390 B
OCaml
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
|