ocaml: phase 5.1 is_prime.ml baseline (count primes <= 100 = 25)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Trial division up to sqrt(n) with early-exit via bool ref:
let is_prime n =
if n < 2 then false
else
let p = ref true in
let i = ref 2 in
while !i * !i <= n && !p do
if n mod !i = 0 then p := false;
i := !i + 1
done;
!p
Outer count_primes loops 2..n calling is_prime, accumulating count.
Returns 25 — the canonical prime-counting function pi(100).
107 baseline programs total.
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
"hanoi.ml": 1023,
|
||||
"hist.ml": 75,
|
||||
"int_sqrt.ml": 1027,
|
||||
"is_prime.ml": 25,
|
||||
"fizz_classifier.ml": 540,
|
||||
"fizzbuzz.ml": 57,
|
||||
"flatten_tree.ml": 28,
|
||||
|
||||
21
lib/ocaml/baseline/is_prime.ml
Normal file
21
lib/ocaml/baseline/is_prime.ml
Normal file
@@ -0,0 +1,21 @@
|
||||
let is_prime n =
|
||||
if n < 2 then false
|
||||
else
|
||||
let p = ref true in
|
||||
let i = ref 2 in
|
||||
while !i * !i <= n && !p do
|
||||
if n mod !i = 0 then p := false;
|
||||
i := !i + 1
|
||||
done;
|
||||
!p
|
||||
|
||||
let count_primes n =
|
||||
let c = ref 0 in
|
||||
for i = 2 to n do
|
||||
if is_prime i then c := !c + 1
|
||||
done;
|
||||
!c
|
||||
|
||||
;;
|
||||
|
||||
count_primes 100
|
||||
@@ -407,6 +407,12 @@ _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 — is_prime.ml baseline (count primes ≤ 100 =
|
||||
25). Trial division up to √n with early-exit via bool ref. Loop
|
||||
2..n calling is_prime, accumulate count. Returns 25 (the canonical
|
||||
prime-counting function π(100)). Tests two cooperating functions
|
||||
+ while-with-bool-short-circuit + nested for. 107 baseline
|
||||
programs total.
|
||||
- 2026-05-09 Phase 5.1 — catalan.ml baseline (Catalan number C(5)
|
||||
via DP recurrence = 42). DP recurrence `C(n) = sum_{j=0}^{n-1}
|
||||
C(j) * C(n-1-j)`. C(5) = 42 — also the count of distinct binary
|
||||
|
||||
Reference in New Issue
Block a user