ocaml: phase 5.1 euler28.ml baseline (sum of diagonals in 7x7 spiral = 261)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

For each layer 1..(n-1)/2, the four corners of an Ulam spiral are
spaced 2*layer apart. Step k four times per layer, accumulate:

  let euler28 n =
    let s = ref 1 in
    let k = ref 1 in
    for layer = 1 to (n - 1) / 2 do
      let step = 2 * layer in
      for _ = 1 to 4 do
        k := !k + step;
        s := !s + !k
      done
    done;
    !s

  euler28 7 = 1 + (3+5+7+9) + (13+17+21+25) + (31+37+43+49) = 261

Real PE28 uses 1001x1001 (answer 669171001); 7x7 is fast.

117 baseline programs total.
This commit is contained in:
2026-05-09 23:03:06 +00:00
parent 89a807a1ed
commit ea7120751d
3 changed files with 22 additions and 0 deletions

View File

@@ -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 — euler28.ml baseline (sum of diagonals in
7x7 number spiral = 261). For each layer (1..(n-1)/2) the four
corners are spaced 2*layer apart, so we step `k` four times per
layer and accumulate into s. Real PE28 uses 1001x1001 (answer
669171001); 7x7 is fast enough to land in seconds. 117 baseline
programs total.
- 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