ocaml: phase 5.1 euler21_small.ml baseline (sum of amicable numbers <= 300 = 504)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
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.
This commit is contained in:
25
lib/ocaml/baseline/euler21_small.ml
Normal file
25
lib/ocaml/baseline/euler21_small.ml
Normal file
@@ -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
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user