From e8a0c86de0522df73d2a6848ff41419be822b2dc Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 17:55:07 +0000 Subject: [PATCH] ocaml: phase 5.1 int_sqrt.ml baseline (Newton integer sqrt, 12+14+1000+1 = 1027) 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. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/int_sqrt.ml | 14 ++++++++++++++ plans/ocaml-on-sx.md | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 lib/ocaml/baseline/int_sqrt.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 4b779132..1c8b6f80 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -36,6 +36,7 @@ "hailstone.ml": 111, "hanoi.ml": 1023, "hist.ml": 75, + "int_sqrt.ml": 1027, "fizzbuzz.ml": 57, "flatten_tree.ml": 28, "list_ops.ml": 30, diff --git a/lib/ocaml/baseline/int_sqrt.ml b/lib/ocaml/baseline/int_sqrt.ml new file mode 100644 index 00000000..b09a6290 --- /dev/null +++ b/lib/ocaml/baseline/int_sqrt.ml @@ -0,0 +1,14 @@ +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 + +;; + +isqrt 144 + isqrt 200 + isqrt 1000000 + isqrt 2 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index d70be985..6cd77ac3 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,16 @@ _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 — int_sqrt.ml baseline (integer Newton sqrt, + 12+14+1000+1 = 1027). Newton's method on integers using `(x + + n/x) / 2` until convergence (`y >= x`). Tests: + isqrt 144 = 12 + 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 + while convergence loop. 88 + baseline programs total. - 2026-05-09 Phase 5.1 — grid_paths.ml baseline (count distinct paths in (4+1)x(6+1) grid = C(10,4) = 210). DP fills a flattened 2D array: `dp.(0,0) = 1`, others `dp.(i,j) = dp.(i-1,j) + dp.(i,