ocaml: phase 5.1 euler14.ml baseline (longest Collatz under 100, starting n = 97)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s

collatz_len walks n through n/2 if even, 3n+1 if odd, counting
steps. Outer loop scans 2..N tracking the best length and arg-best:

  let euler14 limit =
    let best = ref 0 in
    let best_n = ref 0 in
    for n = 2 to limit do
      let l = collatz_len n in
      if l > !best then begin
        best := l;
        best_n := n
      end
    done;
    !best_n

  euler14 100 = 97   (97 generates a 118-step chain)

Real PE14 uses limit = 1_000_000 (answer 837799); 100 exercises the
same algorithm in <2 minutes on our contended host.

116 baseline programs total.
This commit is contained in:
2026-05-09 22:53:27 +00:00
parent 391a2d0c4f
commit 89a807a1ed
3 changed files with 33 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
let collatz_len n =
let m = ref n in
let c = ref 0 in
while !m > 1 do
if !m mod 2 = 0 then m := !m / 2
else m := 3 * !m + 1;
c := !c + 1
done;
!c
let euler14 limit =
let best = ref 0 in
let best_n = ref 0 in
for n = 2 to limit do
let l = collatz_len n in
if l > !best then begin
best := l;
best_n := n
end
done;
!best_n
;;
euler14 100

View File

@@ -28,6 +28,7 @@
"euler1.ml": 233168,
"euler16.ml": 26,
"euler10.ml": 1060,
"euler14.ml": 97,
"euler2.ml": 4613732,
"euler25.ml": 55,
"euler3.ml": 29,

View File

@@ -407,6 +407,13 @@ _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 — euler14.ml baseline (longest Collatz chain
starting under N=100 → 97). collatz_len walks n through the
iteration n/2 if even, 3n+1 if odd, counting steps. Outer loop
scans 2..N tracking best length and arg-best. n=97 generates the
118-step chain. Real PE14 uses N=1,000,000 (answer 837799); N=100
exercises the same algorithm in <2 minutes. 116 baseline programs
total.
- 2026-05-09 Phase 5.1 — euler16.ml baseline (digit sum of 2^15 =
26). Computes 2^n via for-loop accumulation, then walks the digits
via mod 10 / div 10 to sum them. Real PE16 asks for 2^1000 which