ocaml: phase 5.1 abundant.ml baseline (count abundant numbers < 100 = 21)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
A number n is abundant if its proper-divisor sum exceeds n. Reuses
the trial-division div_sum helper:
let count_abundant n =
let c = ref 0 in
for i = 12 to n - 1 do
if div_sum i > i then c := !c + 1
done;
!c
count_abundant 100 = 21
Abundant numbers under 100, starting at 12, 18, 20, 24, 30, 36, 40,
42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96 -> 21.
Companion to euler21_small.ml (amicable). The classification:
perfect: d(n) = n (e.g. 6, 28)
abundant: d(n) > n (e.g. 12, 18)
deficient:d(n) < n (everything else)
124 baseline programs total.
This commit is contained in:
23
lib/ocaml/baseline/abundant.ml
Normal file
23
lib/ocaml/baseline/abundant.ml
Normal 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_abundant n =
|
||||
let c = ref 0 in
|
||||
for i = 12 to n - 1 do
|
||||
if div_sum i > i then c := !c + 1
|
||||
done;
|
||||
!c
|
||||
|
||||
;;
|
||||
|
||||
count_abundant 100
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"abundant.ml": 21,
|
||||
"ackermann.ml": 125,
|
||||
"adler32.ml": 300286872,
|
||||
"anagram_check.ml": 2,
|
||||
|
||||
@@ -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-10 Phase 5.1 — abundant.ml baseline (count abundant numbers
|
||||
< 100 = 21). Abundant means d(n) > n where d(n) is the proper-
|
||||
divisor sum. Reuses the trial-division div_sum helper from iter
|
||||
205. 21 abundant numbers under 100, starting at 12, 18, 20, 24...
|
||||
124 baseline programs total.
|
||||
- 2026-05-10 Phase 5.1 — euler36.ml baseline (sum of double-base
|
||||
palindromes ≤ 1000 = 1772). Numbers that read the same in base 10
|
||||
and base 2: 1, 3, 5, 7, 9, 33, 99, 313, 585, 717. Sum = 1772.
|
||||
|
||||
Reference in New Issue
Block a user