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,
|
||||
|
||||
Reference in New Issue
Block a user