From 2a01758f28c37a3c74b796b0189ff6398c48587d Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 22:11:11 +0000 Subject: [PATCH] ocaml: phase 5.1 euler7.ml baseline (100th prime = 541) Scaled-down PE7 (real version asks for the 10001st prime = 104743). Trial-division within an outer while loop searching forward from 2, short-circuited via bool ref: let nth_prime n = let count = ref 0 in let i = ref 1 in let result = ref 0 in while !count < n do i := !i + 1; let p = ref true in let j = ref 2 in while !j * !j <= !i && !p do if !i mod !j = 0 then p := false; j := !j + 1 done; if !p then begin count := !count + 1; if !count = n then result := !i end done; !result nth_prime 100 = 541 100 keeps the run under our 3-minute budget while exercising the same algorithm. 112 baseline programs total. --- lib/ocaml/baseline/euler7.ml | 22 ++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 5 +++++ 3 files changed, 28 insertions(+) create mode 100644 lib/ocaml/baseline/euler7.ml diff --git a/lib/ocaml/baseline/euler7.ml b/lib/ocaml/baseline/euler7.ml new file mode 100644 index 00000000..e4ad4b1e --- /dev/null +++ b/lib/ocaml/baseline/euler7.ml @@ -0,0 +1,22 @@ +let nth_prime n = + let count = ref 0 in + let i = ref 1 in + let result = ref 0 in + while !count < n do + i := !i + 1; + let p = ref true in + let j = ref 2 in + while !j * !j <= !i && !p do + if !i mod !j = 0 then p := false; + j := !j + 1 + done; + if !p then begin + count := !count + 1; + if !count = n then result := !i + end + done; + !result + +;; + +nth_prime 100 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index e9824aa4..1db73d94 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -31,6 +31,7 @@ "euler4_small.ml": 9009, "euler5.ml": 232792560, "euler6.ml": 25164150, + "euler7.ml": 541, "euler9.ml": 31875000, "expr_eval.ml": 16, "expr_simp.ml": 22, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 5c1d1f87..1f39a248 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -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 — euler7.ml baseline (100th prime = 541; + scaled-down PE7 which asks for the 10001st = 104743). Trial- + division within an outer while loop searching forward from 2, + short-circuited via bool ref. 100 keeps under 3-min budget while + exercising the same algorithm. 112 baseline programs total. - 2026-05-09 Phase 5.1 — euler4_small.ml baseline (largest 2-digit palindrome product = 9009 = 91 * 99). Scaled-down Project Euler #4 (real version uses 3-digit numbers, 906609; that's 810k inner