ocaml: phase 5.1 triangle_div.ml baseline (first triangle with >10 divisors = 120)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
PE12 with target = 10:
let count_divisors n =
let c = ref 0 in
let i = ref 1 in
while !i * !i <= n do
if n mod !i = 0 then begin
c := !c + 1;
if !i * !i <> n then c := !c + 1
end;
i := !i + 1
done;
!c
let first_triangle_with_divs target =
walk triangles T(n) = T(n-1) + n until count_divisors T > target
T(15) = 120 has 16 divisors — first to exceed 10. Real PE12 uses
target 500 (answer 76576500); 10 stays well under budget.
126 baseline programs total.
This commit is contained in:
@@ -122,6 +122,7 @@
|
||||
"sum_squares.ml": 385,
|
||||
"tree_depth.ml": 4,
|
||||
"triangle.ml": 11,
|
||||
"triangle_div.ml": 120,
|
||||
"twosum.ml": 5,
|
||||
"unique_set.ml": 9,
|
||||
"validate.ml": 417,
|
||||
|
||||
26
lib/ocaml/baseline/triangle_div.ml
Normal file
26
lib/ocaml/baseline/triangle_div.ml
Normal file
@@ -0,0 +1,26 @@
|
||||
let count_divisors n =
|
||||
let c = ref 0 in
|
||||
let i = ref 1 in
|
||||
while !i * !i <= n do
|
||||
if n mod !i = 0 then begin
|
||||
c := !c + 1;
|
||||
if !i * !i <> n then c := !c + 1
|
||||
end;
|
||||
i := !i + 1
|
||||
done;
|
||||
!c
|
||||
|
||||
let first_triangle_with_divs target =
|
||||
let t = ref 0 in
|
||||
let n = ref 0 in
|
||||
let found = ref false in
|
||||
while not !found do
|
||||
n := !n + 1;
|
||||
t := !t + !n;
|
||||
if count_divisors !t > target then found := true
|
||||
done;
|
||||
!t
|
||||
|
||||
;;
|
||||
|
||||
first_triangle_with_divs 10
|
||||
Reference in New Issue
Block a user