Commit Graph

2640 Commits

Author SHA1 Message Date
eb621240d7 ocaml: phase 5.1 simpson_int.ml baseline (Simpson 1/3 rule, integral_0^1 x^2 -> 10000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Composite Simpson's 1/3 rule with 100 panels:

  let simpson f a b n =
    let h = (b -. a) /. float_of_int n in
    let sum = ref (f a +. f b) in
    for i = 1 to n - 1 do
      let x = a +. float_of_int i *. h in
      let coef = if i mod 2 = 0 then 2.0 else 4.0 in
      sum := !sum +. coef *. f x
    done;
    h *. !sum /. 3.0

The 1-4-2-4-...-4-1 coefficient pattern is implemented via even/odd
index dispatch. Endpoints get coefficient 1.

For x^2 over [0, 1], exact value is 1/3 ~= 0.33333. Scaled by 30000
gives 9999.99..., int_of_float -> 10000.

Tests higher-order function (passing the integrand 'fun x -> x *. x'),
float arithmetic in for-loop, and float_of_int for index->x conversion.

89 baseline programs total.
2026-05-09 18:09:33 +00:00
e8a0c86de0 ocaml: phase 5.1 int_sqrt.ml baseline (Newton integer sqrt, 12+14+1000+1 = 1027)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Newton's method on integers, converging when y >= x:

  let isqrt n =
    if n < 2 then n
    else
      let x = ref n in
      let y = ref ((!x + 1) / 2) in
      while !y < !x do
        x := !y;
        y := (!x + n / !x) / 2
      done;
      !x

Test cases:
  isqrt 144      = 12     (perfect square)
  isqrt 200      = 14     (floor of sqrt(200) ~= 14.14)
  isqrt 1000000  = 1000
  isqrt 2        = 1
  sum            = 1027

Companion to newton_sqrt.ml (iter 124, float Newton). Tests integer
division semantics from iter 94 and a while-until-convergence loop.

88 baseline programs total.
2026-05-09 17:55:07 +00:00
4eeb7e59b4 ocaml: phase 5.1 grid_paths.ml baseline (count paths in 4x6 grid = 210)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
DP filling a flattened 2D array:

  dp.(0, 0)         = 1
  dp.(i, j)         = dp.(i-1, j) + dp.(i, j-1)
  index             = i * (n+1) + j

For a 4x6 grid (5x7 dp matrix), the count is C(10, 4) = 210.

Tests Array as 2D via row-major flatten + nested for + multi-step
conditional access (above/left guarded by 'if i > 0' / 'if j > 0').

87 baseline programs total.
2026-05-09 17:45:52 +00:00
f1df5b1b72 ocaml: phase 5.1 fib_doubling.ml baseline (Fibonacci by doubling, fib(40) = 102334155)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Uses the identities:
  F(2k)   = F(k) * (2 * F(k+1) - F(k))
  F(2k+1) = F(k)^2 + F(k+1)^2

to compute Fibonacci in O(log n) recursive depth instead of O(n).

  let rec fib_pair n =
    if n = 0 then (0, 1)
    else
      let (a, b) = fib_pair (n / 2) in
      let c = a * (2 * b - a) in
      let d = a * a + b * b in
      if n mod 2 = 0 then (c, d)
      else (d, c + d)

Each call returns the pair (F(n), F(n+1)). fib(40) = 102334155 fits
in JS safe-int (< 2^53). Tests tuple returns with let-tuple
destructuring + recursion on n / 2.

86 baseline programs total.
2026-05-09 17:36:24 +00:00
254ef0daff ocaml: phase 5.1 merge_two.ml baseline (merge two sorted lists, length*sum = 441)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Standard two-finger merge with nested match-in-match:

  let rec merge xs ys =
    match xs with
    | [] -> ys
    | x :: xs' ->
      match ys with
      | [] -> xs
      | y :: ys' ->
        if x <= y then x :: merge xs' (y :: ys')
        else y :: merge (x :: xs') ys'

Used as a building block in merge_sort.ml (iter 104) but called out
as its own baseline here.

  merge [1;4;7;10] [2;3;5;8;9]   = [1;2;3;4;5;7;8;9;10]
  length 9, sum 49, product 441.

85 baseline programs total.
2026-05-09 17:24:53 +00:00
b6e723fc3e ocaml: phase 5.1 pow_mod.ml baseline (modular exponentiation, sum = 738639)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Fast exponentiation by squaring with modular reduction:

  let rec pow_mod base exp m =
    if exp = 0 then 1
    else if exp mod 2 = 0 then
      let half = pow_mod base (exp / 2) m in
      (half * half) mod m
    else
      (base * pow_mod base (exp - 1) m) mod m

Even exponent halves and squares (O(log n)); odd decrements and
multiplies. mod-reduction at each step keeps intermediates bounded.

  pow_mod 2 30 1000003 + pow_mod 3 20 13 + pow_mod 5 17 100 = 738639

84 baseline programs total.
2026-05-09 17:15:47 +00:00
2e84492d96 ocaml: phase 5.1 tree_depth.ml baseline (binary tree depth, longest path = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Same 'tree = Leaf | Node of int * tree * tree' ADT as iter-159
max_path_tree.ml, but the recursion ignores the value:

  let rec depth t = match t with
    | Leaf -> 0
    | Node (_, l, r) ->
      let dl = depth l in
      let dr = depth r in
      1 + (if dl > dr then dl else dr)

For the test tree:
        1
       /       2   3
     /     4   5
       /
      8

longest path is 1 -> 2 -> 5 -> 8, depth = 4.

Tests wildcard pattern in constructor 'Node (_, l, r)', two nested
let-bindings in match arm, inline if-as-expression for max.

83 baseline programs total.
2026-05-09 17:06:10 +00:00
1bde4e834f ocaml: phase 5.1 stable_unique.ml baseline (Hashtbl dedupe preserving order, 8+38 = 46)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Walk input with Hashtbl.mem + Hashtbl.add seen x () (unit-payload
turns the table into a set); on first occurrence cons to the result
list; reverse at the end:

  let stable_unique xs =
    let seen = Hashtbl.create 8 in
    let result = ref [] in
    List.iter (fun x ->
      if not (Hashtbl.mem seen x) then begin
        Hashtbl.add seen x ();
        result := x :: !result
      end
    ) xs;
    List.rev !result

For [3;1;4;1;5;9;2;6;5;3;5;8;9]:
  result = [3;1;4;5;9;2;6;8]   (input order, dupes dropped)
  length = 8, sum = 38         total = 46

Tests Hashtbl as a set abstraction (unit-payload), the rev-build
idiom, and 'not (Hashtbl.mem seen x)' membership negation.

82 baseline programs total.
2026-05-09 16:56:46 +00:00
554ef48c63 ocaml: phase 5.1 run_decode.ml baseline (RLE decode, expansion sum = 21)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Inverse of run_length.ml from iteration 130. Takes a list of
(value, count) tuples and expands:

  let rec rle_decode pairs =
    match pairs with
    | [] -> []
    | (x, n) :: rest ->
      let rec rep k = if k = 0 then [] else x :: rep (k - 1) in
      rep n @ rle_decode rest

  rle_decode [(1,3); (2,2); (3,4); (1,2)]
  = [1;1;1; 2;2; 3;3;3;3; 1;1]
  sum = 3 + 4 + 12 + 2 = 21.

Tests tuple-cons pattern, inner-let recursion, list concat (@), and
the 'List.fold_left (+) 0' invariant on encoding round-trips.

81 baseline programs total.
2026-05-09 16:47:56 +00:00
b7b841821c ocaml: phase 5.1 peano.ml baseline (Peano arithmetic, 5*6 = 30)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Defines unary Peano numerals with two recursive functions for
arithmetic:

  type peano = Zero | Succ of peano

  let rec plus a b = match a with
    | Zero -> b
    | Succ a' -> Succ (plus a' b)

  let rec mul a b = match a with
    | Zero -> Zero
    | Succ a' -> plus b (mul a' b)

mul is defined inductively: mul Zero _ = Zero; mul (Succ a) b =
b + (a * b).

  to_int (mul (from_int 5) (from_int 6)) = 30

The result is a Peano value with 30 nested Succ wrappers; to_int
unrolls them to a host int. Tests recursive ADT with a single-arg
constructor + four mutually-defined recursive functions (no rec/and
needed since each is defined separately).

80 baseline programs total — milestone.
2026-05-09 16:38:09 +00:00
2129e04bfd ocaml: phase 5.1 count_change.ml baseline (ways to make 50c from [1;2;5;10;25] = 406)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Companion to coin_change.ml (min coins). Counts distinct multisets
via the unbounded-knapsack DP:

  let count_ways coins target =
    let dp = Array.make (target + 1) 0 in
    dp.(0) <- 1;
    List.iter (fun c ->
      for i = c to target do
        dp.(i) <- dp.(i) + dp.(i - c)
      done
    ) coins;
    dp.(target)

Outer loop over coins, inner DP relaxes dp.(i) += dp.(i - c). The
order matters — coin in outer, amount in inner — to count multisets
rather than ordered sequences.

count_ways [1; 2; 5; 10; 25] 50 = 406.

79 baseline programs total.
2026-05-09 16:28:15 +00:00
89726ed6c2 ocaml: phase 5.1 paren_depth.ml baseline (max nesting depth, 3+3+1 = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
One-pass walk tracking current depth and a high-water mark:

  let max_depth s =
    let d = ref 0 in
    let m = ref 0 in
    for i = 0 to String.length s - 1 do
      if s.[i] = '(' then begin
        d := !d + 1;
        if !d > !m then m := !d
      end
      else if s.[i] = ')' then d := !d - 1
    done;
    !m

Three inputs:
  '((1+2)*(3-(4+5)))'   3   (innermost (4+5) at depth 3)
  '(((deep)))'           3
  '()()()'               1   (no nesting)
  sum                    7

Tests for-loop char comparison s.[i] = '(' and the high-water-mark
idiom with two refs.

78 baseline programs total.
2026-05-09 16:13:05 +00:00
5d71be364e ocaml: phase 5.1 pancake_sort.ml baseline (in-place pancake sort, 9 flips -> 910)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Each pass:
  1. find_max in [0..size-1]
  2. if max not at the right end, flip max to position 0 (if needed)
  3. flip the size-prefix to push max to the end

Inner 'flip k' reverses prefix [0..k] using two pointer refs lo/hi.
Inner 'find_max k' walks 1..k tracking the max-position.

  pancake_sort [3;1;4;1;5;9;2;6]
  = 9 flips * 100 + a.(0) + a.(n-1)
  = 9 * 100 + 1 + 9
  = 910

The output combines flip count and sorted endpoints, so the test
verifies both that the sort terminates and that it sorts correctly.

Tests two inner functions closing over the same Array, ref-based
two-pointer flip, and downto loop with conditional flip dispatch.

77 baseline programs total.
2026-05-09 16:03:22 +00:00
ce013fa138 ocaml: phase 5.1 fib_mod.ml baseline (Fibonacci mod prime, fib(100) mod 1000003 = 391360)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Iterative two-ref Fibonacci with modular reduction every step:

  let fib_mod n m =
    let a = ref 0 in
    let b = ref 1 in
    for _ = 1 to n do
      let c = (!a + !b) mod m in
      a := !b;
      b := c
    done;
    !a

The 100th Fibonacci is 354_224_848_179_261_915_075, well past JS
safe-int (2^53). Modular reduction every step keeps intermediate
values within int53 precision so the answer is exact in our
runtime. fib(100) mod 1000003 = 391360.

76 baseline programs total.
2026-05-09 15:53:47 +00:00
07de86365e ocaml: phase 5.1 luhn.ml baseline (Luhn check-digit, 2/4 inputs valid)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Walks digits right-to-left, doubles every other starting from the
second-from-right; if a doubled value > 9, subtract 9. Sum must be
divisible by 10:

  let luhn s =
    let n = String.length s in
    let total = ref 0 in
    for i = 0 to n - 1 do
      let d = Char.code s.[n - 1 - i] - Char.code '0' in
      let v = if i mod 2 = 1 then
        let dd = d * 2 in
        if dd > 9 then dd - 9 else dd
      else d
      in
      total := !total + v
    done;
    !total mod 10 = 0

Test cases:
  '79927398713'        valid
  '79927398710'        invalid
  '4532015112830366'   valid (real Visa test)
  '1234567890123456'   invalid
  sum = 2

Tests right-to-left index walk via 'n - 1 - i', Char.code '0'
arithmetic for digit conversion, and nested if-then-else.

75 baseline programs total.
2026-05-09 15:42:01 +00:00
5b38f4d499 ocaml: phase 5.1 triangle.ml baseline (Pascal-shape min path sum, 2+3+5+1 = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Bottom-up DP minimum-path through a triangle:

       2
      3 4
     6 5 7
    4 1 8 3

  let min_path_triangle rows =
    initialise dp from last row;
    for r = n - 2 downto 0 do
      for c = 0 to row_len - 1 do
        dp.(c) <- row.(c) + min(dp.(c), dp.(c+1))
      done
    done;
    dp.(0)

The optimal path 2 -> 3 -> 5 -> 1 sums to 11.

Tests downto loop, Array.of_list inside loop body, nested arr.(i)
reads + writes, and inline if-then-else for min.

74 baseline programs total.
2026-05-09 15:32:11 +00:00
a3a93c20b8 ocaml: phase 5.1 max_path_tree.ml baseline (max root-to-leaf sum, 1+3+7 = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Recursive ADT for binary trees:

  type tree = Leaf | Node of int * tree * tree

  let rec max_path t =
    match t with
    | Leaf -> 0
    | Node (v, l, r) ->
      let lp = max_path l in
      let rp = max_path r in
      v + (if lp > rp then lp else rp)

For the test tree:
       1
      /      2   3
    / \   \
   4   5   7

paths sum:    1+2+4=7, 1+2+5=8, 1+3+7=11.  max = 11.

Tests 3-arg Node constructor with positional arg destructuring, two
nested let-bindings, and if-then-else as an inline expression.

73 baseline programs total.
2026-05-09 15:22:28 +00:00
30b237a891 ocaml: phase 5.1 mod_inverse.ml baseline (extended Euclidean, inverse sum = 27)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Extended Euclidean returns a triple (gcd, x, y) such that
a*x + b*y = gcd:

  let rec ext_gcd a b =
    if b = 0 then (a, 1, 0)
    else
      let (g, x1, y1) = ext_gcd b (a mod b) in
      (g, y1, x1 - (a / b) * y1)

  let mod_inverse a m =
    let (_, x, _) = ext_gcd a m in
    ((x mod m) + m) mod m

Three invariants checked:

  inv(3, 11)  = 4      (3*4  = 12  = 1 mod 11)
  inv(5, 26)  = 21     (5*21 = 105 = 1 mod 26)
  inv(7, 13)  = 2      (7*2  = 14  = 1 mod 13)
  sum         = 27

Tests recursive triple-tuple return, tuple-pattern destructuring on
let-binding (with wildcard for unused fields), and nested
let-binding inside the recursive call site.

72 baseline programs total.
2026-05-09 15:11:46 +00:00
667dfcfd7c ocaml: phase 5.1 hist.ml baseline (Hashtbl int histogram, total * max = 75)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Three small functions:

  hist xs       build a Hashtbl of count-by-value
  max_value h   Hashtbl.fold to find the max bin
  total h       Hashtbl.fold to sum all bins

For the 15-element list [1;2;3;1;4;5;1;2;6;7;1;8;9;1;0]:
  total      = 15
  max_value  =  5 (the number 1 appears 5 times)
  product    = 75

Companion to bag.ml (string keys) and frequency.ml (char keys) —
same Hashtbl.fold + Hashtbl.find_opt pattern, exercised on int
keys this time.

71 baseline programs total.
2026-05-09 15:02:13 +00:00
7f8bf5f455 ocaml: phase 5.1 mortgage.ml baseline (monthly payment, 200k @ 5% / 30y = $1073)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Standard amortising-mortgage formula:

  payment = P * r * (1 + r)^n / ((1 + r)^n - 1)

where r = annual_rate / 12, n = years * 12.

  let payment principal annual_rate years =
    let r = annual_rate /. 12.0 in
    let n = years * 12 in
    let pow_r = ref 1.0 in
    for _ = 1 to n do pow_r := !pow_r *. (1.0 +. r) done;
    principal *. r *. !pow_r /. (!pow_r -. 1.0)

For 200,000 at 5% over 30 years: monthly payment ~= $1073.64,
int_of_float -> 1073.

Manual (1+r)^n via for-loop instead of Float.pow keeps the program
portable to any environment where pow is restricted.

Tests float arithmetic precedence, for-loop accumulation in a float
ref, int_of_float on the result.

70 baseline programs total — milestone.
2026-05-09 14:52:13 +00:00
a98d683e60 ocaml: phase 5.1 group_consec.ml baseline (group consecutive equals, 5*10+3 = 53)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Inner 'collect cur acc tail' walks the tail while head matches
'cur', accumulating into 'acc'. Returns (rev acc, remaining) on
first mismatch. Outer 'group' recurses on the remaining list.

  group [1;1;2;2;2;3;1;1;4]
   = [[1;1]; [2;2;2]; [3]; [1;1]; [4]]

  List.length groups        = 5
  List.length (gs.(1))      = 3
  5 * 10 + 3                = 53

Tests nested recursion (inner aux + outer recursion), tuple
destructuring 'let (g, tail) = ...' inside the outer match arm,
and List.nth.

69 baseline programs total.
2026-05-09 14:40:22 +00:00
a2f3c533b8 ocaml: phase 5.1 zigzag.ml baseline (interleave two lists, sum 1..10 = 55)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
One-liner that swaps the lists on every recursive call:

  let rec zigzag xs ys =
    match xs with
    | [] -> ys
    | x :: xs' -> x :: zigzag ys xs'

This works because each call emits the head of xs and recurses with
ys as the new xs and the rest of xs as the new ys.

  zigzag [1;3;5;7;9] [2;4;6;8;10] = [1;2;3;4;5;6;7;8;9;10]   sum = 55

Tests recursive list cons + arg-swap idiom that is concise but
non-obvious to readers expecting symmetric-handling.

68 baseline programs total.
2026-05-09 14:30:55 +00:00
0f2eb45f5c ocaml: phase 5.1 prefix_sum.ml baseline (precomputed sums + range queries, 14+25+27 = 66)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Two utility functions:

  prefix_sums xs   builds Array of len n+1 such that
                   arr.(i) = sum of xs[0..i-1]
  range_sum p lo hi = p.(hi+1) - p.(lo)

For [3;1;4;1;5;9;2;6;5;3]:
  range_sum 0 4   = 14   (3+1+4+1+5)
  range_sum 5 9   = 25   (9+2+6+5+3)
  range_sum 2 7   = 27   (4+1+5+9+2+6)
  sum             = 66

Tests List.iter mutating Array indexed by a ref counter, plus the
classic prefix-sum technique for O(1) range queries.

67 baseline programs total.
2026-05-09 14:21:24 +00:00
1c40fec8fa ocaml: phase 5.1 tic_tac_toe.ml baseline (3x3 winner check, X wins top row = 1)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Board as 9-element flat int array, 0=empty, 1=X, 2=O. Three
predicate functions:

  check_row b r       check_col b c       check_diag b

each return the winning player's mark or 0. Main 'winner' loops
i = 0..2 calling row(i)/col(i) then check_diag, threading via a
result ref.

Test board:
  X X X
  . O .
  . . O

X wins on row 0 -> winner returns 1.

Tests Array.of_list with row-major 'b.(r * 3 + c)' indexing,
multi-fn collaboration, and structural equality on int values.

66 baseline programs total.
2026-05-09 14:00:49 +00:00
b94a47a9a9 ocaml: phase 5.1 subset_sum.ml baseline (count subsets of [1..8] summing to 10 = 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 45s
Pure recursion — at each element, take it or don't:

  let rec count_subsets xs target =
    match xs with
    | [] -> if target = 0 then 1 else 0
    | x :: rest ->
      count_subsets rest target
      + count_subsets rest (target - x)

For [1;2;3;4;5;6;7;8] target 10, the recursion tree has 2^8 = 256
leaves. Returns 8 — number of subsets summing to 10:

  1+2+3+4, 1+2+7, 1+3+6, 1+4+5, 2+3+5, 2+8, 3+7, 4+6 = 8

Tests doubly-recursive list traversal pattern with single-arg list
+ accumulator-via-target.

65 baseline programs total.
2026-05-09 13:46:40 +00:00
7de014cd75 ocaml: phase 5.1 hailstone.ml baseline (Collatz length from 27 = 111 steps)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Iterative Collatz / hailstone sequence:

  let collatz_length n =
    let m = ref n in
    let count = ref 0 in
    while !m > 1 do
      if !m mod 2 = 0 then m := !m / 2
      else m := 3 * !m + 1;
      count := !count + 1
    done;
    !count

27 is the famous 'long-running' Collatz starter. Reaches a peak of
9232 mid-sequence and takes 111 steps to bottom out at 1.

64 baseline programs total.
2026-05-09 13:30:46 +00:00
0eef5bc8e6 ocaml: phase 5.1 twosum.ml baseline (LeetCode #1 one-pass hashmap, index sum = 5)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Walks list with List.iteri, checking if target - x is already in
the hashtable; if yes, the earlier index plus current is the
answer; otherwise record the current pair.

  twosum [2;7;11;15] 9   = (0, 1)   2+7
  twosum [3;2;4]     6   = (1, 2)   2+4
  twosum [3;3]       6   = (0, 1)   3+3

Sum of i+j over each pair: 1 + 3 + 1 = 5.

Tests Hashtbl.find_opt + add (the iter-99 cleanup), List.iteri, and
tuple destructuring on let-binding (iter 98 'let (i, j) = twosum
... in').

63 baseline programs total.
2026-05-09 13:15:05 +00:00
50981a2a9b ocaml: phase 5.1 bisect.ml baseline (root-finding, sqrt(2)*100 = 141)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Bisection method searching for f(x) = 0 in [lo, hi] over 50
iterations:

  let bisect f lo hi =
    let lo = ref lo and hi = ref hi in
    for _ = 1 to 50 do
      let mid = (!lo +. !hi) /. 2.0 in
      if f mid = 0.0 || f !lo *. f mid < 0.0 then hi := mid
      else lo := mid
    done;
    !lo

Solving x^2 - 2 = 0 in [1, 2] via 'bisect (fun x -> x *. x -. 2.0)
1.0 2.0' converges to ~1.41421356... -> int_of_float (r *. 100) =
141.

Tests:
  - higher-order function passing
  - multi-let 'let lo = ref ... and hi = ref ...'
  - float arithmetic
  - int_of_float truncate-toward-zero (iter 117)

62 baseline programs total.
2026-05-09 13:02:17 +00:00
05487b497d ocaml: phase 5.1 base_n.ml baseline (int to base-N string, length sum = 17)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
36-character digit alphabet '0..9A..Z' supports any base 2..36. Loop
divides the magnitude by base and prepends the digit:

  while !m > 0 do
    acc := String.make 1 digits.[!m mod base] ^ !acc;
    m := !m / base
  done

Special-cases n = 0 -> '0' and prepends '-' for negatives.

Test cases (length, since the strings differ in alphabet):
  255 hex      'FF'              2
  1024 binary  '10000000000'    11
  100 dec      '100'             3
  0 any base   '0'               1
  sum                            17

Combines digits.[i] (string indexing) + String.make 1 ch + String
concatenation in a loop.

61 baseline programs total.
2026-05-09 12:52:55 +00:00
af38d98583 ocaml: phase 5.1 prime_factors.ml baseline (trial-division, 360 factor sum = 17)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Three refs threading through a while loop:
  m       remaining quotient
  d       current divisor
  result  accumulator (built in reverse, List.rev at end)

  while !m > 1 do
    if !m mod !d = 0 then begin
      result := !d :: !result;
      m := !m / !d
    end else
      d := !d + 1
  done

360 = 2^3 * 3^2 * 5 factors to [2;2;2;3;3;5], sum 17.

60 baseline programs total — milestone.
2026-05-09 12:44:02 +00:00
f5122a9a5d ocaml: phase 5.1 atm.ml baseline (mutable record + exception + try/with, balance = 120)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Models a bank account using a mutable record + a user exception:

  type account = { mutable balance : int }
  exception Insufficient

  let withdraw acct amt =
    if amt > acct.balance then raise Insufficient
    else acct.balance <- acct.balance - amt

Sequence:
  start             100
  deposit 50        150
  withdraw 30       120
  withdraw 200      raises Insufficient
  handler returns   acct.balance        (= 120, transaction rolled back)

Combines mutable record fields, user exception declaration,
try-with-bare-pattern, and verifies that a raise in the middle of a
sequence doesn't leave a partial mutation.

59 baseline programs total.
2026-05-09 12:34:36 +00:00
097c7f4590 ocaml: phase 5.1 bf_full.ml baseline (full Brainfuck with [] loops, +++[.-] = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Extends the iter-92 brainfuck.ml subset interpreter with bracket
matching:

  '['  if mem[ptr] = 0, jump past matching ']'
       (forward depth-counting scan: '[' increments depth, ']' decrements)
  ']'  if mem[ptr] <> 0, jump back to matching '['
       (backward depth-counting scan)

Test program '+++[.-]':
  +++   set cell 0 = 3
  [     enter loop (cell != 0)
    .   acc += cell
    -   cell -= 1
  ]     loop while cell != 0
  result: acc = 3 + 2 + 1 = 6

Tests deeply nested while loops, mutable pc / ptr / acc, multi-arm
if/else if dispatch on chars + nested begin/end blocks for loop
body conditionals.

58 baseline programs total.
2026-05-09 12:24:48 +00:00
5c587c0f61 ocaml: phase 5.1 anagram_check.ml baseline (char-frequency array, 2/4 anagrams)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
to_counts builds a 256-slot int array of character frequencies:

  let to_counts s =
    let counts = Array.make 256 0 in
    for i = 0 to String.length s - 1 do
      let c = Char.code s.[i] in
      counts.(c) <- counts.(c) + 1
    done;
    counts

same_counts compares two arrays element-by-element via for loop +
bool ref. is_anagram composes them.

Four pairs:
  listen ~ silent       true
  hello !~ world        false
  anagram ~ nagaram     true
  abc !~ abcd           false (length differs)
  sum                   2

Exercises Array.make + arr.(i) + arr.(i) <- v + nested for loops +
Char.code + s.[i].

57 baseline programs total.
2026-05-09 12:14:32 +00:00
acc8b01ddb ocaml: phase 5.1 exception_user.ml baseline (user exception with payload, 4+5+7+10 = 26)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Defines a user exception with int payload:

  exception Negative of int

  let safe_sqrt n =
    if n < 0 then raise (Negative n)
    else <integer sqrt via while loop>

  let try_sqrt n =
    try safe_sqrt n with
    | Negative x -> -x

  try_sqrt  16  -> 4
  try_sqrt  25  -> 5
  try_sqrt -7   -> 7  (handler returns -(-7) = 7)
  try_sqrt 100  -> 10
  sum           -> 26

Tests exception declaration with int payload, raise with carry, and
try-with arm pattern-matching the constructor with payload binding.

56 baseline programs total.
2026-05-09 12:04:42 +00:00
027678f31e ocaml: phase 5.1 flatten_tree.ml baseline (parametric ADT flatten, sum 1..7 = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Defines a parametric tree:

  type 'a tree = Leaf of 'a | Node of 'a tree list

  let rec flatten t =
    match t with
    | Leaf x -> [x]
    | Node ts -> List.concat (List.map flatten ts)

Test tree has 3 levels of nesting:

  Node [Leaf 1; Node [Leaf 2; Leaf 3];
        Node [Node [Leaf 4]; Leaf 5; Leaf 6];
        Leaf 7]

flattens to [1;2;3;4;5;6;7] -> sum = 28.

Tests parametric ADT, mutual recursion via map+self, List.concat.

55 baseline programs total.
2026-05-09 11:52:19 +00:00
cca3a28206 ocaml: phase 5.1 gcd_lcm.ml baseline (Euclidean gcd + lcm, 12+12+36 = 60)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 19s
Two-line baseline:

  let rec gcd a b = if b = 0 then a else gcd b (a mod b)
  let lcm a b = a * b / gcd a b

  gcd 36 48  = 12
  lcm 4 6    = 12
  lcm 12 18  = 36
  sum        = 60

Tests mod arithmetic and the integer-division fix from iteration 94
(without truncate-toward-zero, 'lcm 4 6 = 4 * 6 / 2 = 12.0' rather
than the expected 12).

54 baseline programs total.
2026-05-09 11:42:52 +00:00
b8dfc080dd ocaml: phase 5.1 zip_unzip.ml baseline (zip/unzip round-trip, sum-product = 1000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
zip walks both lists in lockstep, truncating at the shorter. unzip
uses tuple-pattern destructuring on the recursive result.

  let pairs = zip [1;2;3;4] [10;20;30;40] in
  let (xs, ys) = unzip pairs in
  List.fold_left (+) 0 xs * List.fold_left (+) 0 ys
  = 10 * 100
  = 1000

Exercises:
  - tuple-cons patterns in match scrutinee: 'match (xs, ys) with'
  - tuple constructor in return value: '(a :: la, b :: lb)'
  - the iter-98 let-tuple destructuring: 'let (la, lb) = unzip rest'

53 baseline programs total.
2026-05-09 11:33:30 +00:00
ac19b7aced ocaml: phase 5.1 bigint_add.ml baseline (digit-list bignum add, 1+18+9 = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Recursive 4-arm match on (a, b) tuples threading a carry:

  match (a, b) with
  | ([], []) -> if carry = 0 then [] else [carry]
  | (x :: xs, []) -> (s mod 10) :: aux xs [] (s / 10)   where s = x + carry
  | ([], y :: ys) -> ...
  | (x :: xs, y :: ys) -> ...                           where s = x + y + carry

Little-endian digit lists. Three tests:

  [9;9;9] + [1]                 = [0;0;0;1]      (=1000, digit sum 1)
  [5;6;7] + [8;9;1]             = [3;6;9]        (=963, digit sum 18)
  [9;9;9;9;9;9;9;9] + [1]       length 9         (carry propagates 8x)

Sum = 1 + 18 + 9 = 28.

Exercises tuple-pattern match on nested list-cons with the integer
arithmetic and carry-threading idiom typical of multi-precision
implementations.

52 baseline programs total.
2026-05-09 11:19:23 +00:00
aa0a7fa1a2 ocaml: phase 5.1 expr_simp.ml baseline (symbolic simplifier, eval(simp e) = 22)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Recursive ADT with three constructors (Num/Add/Mul). simp does
bottom-up rewrite using algebraic identities:

  x + 0  -> x
  0 + x  -> x
  x * 0  -> 0
  0 * x  -> 0
  x * 1  -> x
  1 * x  -> x
  constant folding for Num + Num and Num * Num

Uses tuple pattern in nested match: 'match (simp a, simp b) with'.

  Add (Mul (Num 3, Num 5), Add (Num 0, Mul (Num 1, Num 7)))
   -> simp ->  Add (Num 15, Num 7)
   -> eval ->  22

51 baseline programs total.
2026-05-09 11:09:23 +00:00
bafa2410e4 ocaml: phase 5.1 mat_mul.ml baseline (3x3 row-major matrix multiply, sum = 621)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Triple-nested for loop with row-major indexing:

  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      for k = 0 to n - 1 do
        c.(i * n + j) <- c.(i * n + j) + a.(i * n + k) * b.(k * n + j)
      done
    done
  done

For 3x3 matrices A=[[1..9]] and B=[[9..1]], the resulting C has sum
621. Tests deeply nested for loops on Array, Array.make + arr.(i) +
arr.(i) <- v + Array.fold_left.

50 baseline programs total — milestone.
2026-05-09 11:00:00 +00:00
a91ff62730 ocaml: phase 5.1 bsearch.ml baseline (binary search, position sum = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Iterative binary search on a sorted int array:

  let bsearch arr target =
    let n = Array.length arr in
    let lo = ref 0 and hi = ref (n - 1) in
    let found = ref (-1) in
    while !lo <= !hi && !found = -1 do
      let mid = (!lo + !hi) / 2 in
      if arr.(mid) = target then found := mid
      else if arr.(mid) < target then lo := mid + 1
      else hi := mid - 1
    done;
    !found

For [1;3;5;7;9;11;13;15;17;19;21]:
  bsearch a 13   = 6
  bsearch a 5    = 2
  bsearch a 100  = -1
  sum            = 7

Exercises Array.of_list + arr.(i) + multi-let 'let lo = ... and
hi = ...' + while + multi-arm if/else if/else.

49 baseline programs total.
2026-05-09 10:40:49 +00:00
073ea44fdb ocaml: phase 5.1 palindrome.ml baseline (two-pointer check, 4/6 inputs match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Two-pointer palindrome check:

  let is_palindrome s =
    let n = String.length s in
    let rec check i j =
      if i >= j then true
      else if s.[i] <> s.[j] then false
      else check (i + 1) (j - 1)
    in
    check 0 (n - 1)

Tests on six strings:
  racecar = true
  hello   = false
  abba    = true
  ''      = true (vacuously, i >= j on entry)
  'a'     = true
  'ab'    = false

Sum = 4.

Uses s.[i] <> s.[j] (string-get + structural inequality), recursive
2-arg pointer advancement, and a multi-clause if/else if/else for
the three cases.

48 baseline programs total.
2026-05-09 10:31:22 +00:00
aee7226b9c ocaml: phase 5.1 coin_change.ml baseline (DP, 67c with [1;5;10;25] = 6 coins)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Bottom-up dynamic programming. dp[i] = minimum coins to make
amount i.

  let dp = Array.make (target + 1) (target + 1) in   (* sentinel *)
  dp.(0) <- 0;
  for i = 1 to target do
    List.iter (fun c ->
      if c <= i && dp.(i - c) + 1 < dp.(i) then
        dp.(i) <- dp.(i - c) + 1
    ) coins
  done

Sentinel 'target + 1' means impossible — any real solution uses at
most 'target' coins.

  coin_change [1; 5; 10; 25] 67   = 6   (= 25+25+10+5+1+1)

Exercises Array.make + arr.(i) + arr.(i) <- v + nested
for/List.iter + guard 'c <= i'.

47 baseline programs total.
2026-05-09 10:21:11 +00:00
b3d5da5361 ocaml: phase 5.1 kadane.ml baseline (max subarray sum = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Kadane's algorithm in O(n):

  let max_subarray xs =
    let max_so_far = ref min_int in
    let cur = ref 0 in
    List.iter (fun x ->
      cur := max x (!cur + x);
      max_so_far := max !max_so_far !cur
    ) xs;
    !max_so_far

For [-2;1;-3;4;-1;2;1;-5;4] the optimal subarray is [4;-1;2;1] = 6.

Exercises min_int (iter 94), max as global, ref / ! / :=, and
List.iter with two side-effecting steps in one closure body.

46 baseline programs total.
2026-05-09 10:07:12 +00:00
da6d8e39c9 ocaml: phase 5.1 pascal.ml baseline (Pascal triangle row 10 middle = C(10,5) = 252)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
next_row prepends 1, walks adjacent pairs (x, y) emitting x+y,
appends a final 1:

  let rec next_row prev =
    let rec aux a =
      match a with
      | [_] -> [1]
      | x :: y :: rest -> (x + y) :: aux (y :: rest)
      | [] -> []
    in
    1 :: aux prev

row n iterates next_row n times starting from [1] using a ref +
'for _ = 1 to n do r := next_row !r done'.

  row 10 = [1;10;45;120;210;252;210;120;45;10;1]
  List.nth (row 10) 5 = 252 = C(10, 5)

Exercises three-arm match including [_] singleton wildcard, x :: y
:: rest binding, and the for-loop with wildcard counter. 45 baseline
programs total.
2026-05-09 09:57:18 +00:00
32aba1823d ocaml: phase 5.1 run_length.ml baseline (RLE, sum-of-counts = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Run-length encoding via tail-recursive 4-arg accumulator:

  let rle xs =
    let rec aux xs cur n acc =
      match xs with
      | [] -> List.rev ((cur, n) :: acc)
      | h :: t ->
        if h = cur then aux t cur (n + 1) acc
        else aux t h 1 ((cur, n) :: acc)
    in
    match xs with
    | [] -> []
    | h :: t -> aux t h 1 []

  rle [1;1;1;2;2;3;3;3;3;1;1] = [(1,3);(2,2);(3,4);(1,2)]
  sum of counts                = 11 (matches input length)

The sum-of-counts test verifies that the encoding preserves total
length — drops or duplicates would diverge.

44 baseline programs total.
2026-05-09 09:47:25 +00:00
3be2dc6e78 ocaml: phase 5.1 grep_count.ml baseline (substring-aware line filter, 3 matches)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Defines a recursive str_contains that walks the haystack with
String.sub to find a needle substring. Real OCaml's String.contains
only accepts a single char, so this baseline implements its own
substring search to stay portable.

  let rec str_contains s sub i =
    if i + sl > nl then false
    else if String.sub s i sl = sub then true
    else str_contains s sub (i + 1)

count_matching splits text on newlines, folds with the predicate.

  'the quick brown fox\nfox runs fast\nthe dog\nfoxes are clever'
   needle = 'fox'
   matches = 3 (lines 1, 2, 4 — 'foxes' contains 'fox')

43 baseline programs total.
2026-05-09 09:34:40 +00:00
b0cbdaf713 ocaml: phase 5.1 pretty_table.ml baseline (Buffer + Printf widths, len = 64)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Builds a 4-row scoreboard via Buffer + Printf.sprintf:

  Buffer.add_string buf (Printf.sprintf '%-10s %4d\n' name score)

Each row is exactly 16 chars regardless of actual name/score length:
  10 name padding + 1 space + 4 score padding + 1 newline.
4 rows -> 64 chars total.

Combines:
  - Buffer.add_string + Printf.sprintf
  - %-Ns left-justified string and %Nd right-justified int width
  - List.iter on tuple-pattern args (iter 101 fun (a, b))

42 baseline programs total.
2026-05-09 09:24:41 +00:00
aaaf054441 ocaml: phase 4 bitwise ops land/lor/lxor/lsl/lsr/asr + bits.ml baseline (+5 tests, 607 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
The binop precedence table already had land/lor/lxor/lsl/lsr/asr
(iter 0 setup) but eval-op fell through to 'unknown operator' for
all of them. SX doesn't expose host bitwise primitives, so each is
implemented in eval.sx via arithmetic on the host:

  land/lor/lxor:  mask & shift loop, accumulating 1<<k digits
  lsl k:          repeated * 2 k times
  lsr k:          repeated floor (/ 2) k times
  asr:            aliased to lsr (no sign extension at our bit width)

bits.ml baseline: popcount via 'while m > 0 do if m land 1 = 1 then
... ; m := m lsr 1 done'. Sum of popcount(1023, 5, 1024, 0xff) = 10
+ 2 + 1 + 8 = 21.

  5 land 3 = 1
  5 lor 3 = 7
  5 lxor 3 = 6
  1 lsl 8 = 256
  256 lsr 4 = 16

41 baseline programs total.
2026-05-09 09:15:00 +00:00
70b9b4f6cf ocaml: phase 5.1 ackermann.ml baseline (ack(3, 4) = 125)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Classic Ackermann function:

  let rec ack m n =
    if m = 0 then n + 1
    else if n = 0 then ack (m - 1) 1
    else ack (m - 1) (ack m (n - 1))

ack(3, 4) = 125, expanding to ~6700 evaluator frames — a useful
stress test of the call stack and control transfer. Real OCaml
evaluates this in milliseconds; ours takes ~2 minutes on a
contended host but completes correctly.

40 baseline programs total.
2026-05-09 08:50:12 +00:00