diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 201762dd..3cce9ca5 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -25,6 +25,7 @@ "expr_eval.ml": 16, "expr_simp.ml": 22, "factorial.ml": 3628800, + "fib_doubling.ml": 102334155, "fib_mod.ml": 391360, "fraction.ml": 7, "frequency.ml": 5, diff --git a/lib/ocaml/baseline/fib_doubling.ml b/lib/ocaml/baseline/fib_doubling.ml new file mode 100644 index 00000000..3843f459 --- /dev/null +++ b/lib/ocaml/baseline/fib_doubling.ml @@ -0,0 +1,14 @@ +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) + +let fib n = let (f, _) = fib_pair n in f + +;; + +fib 40 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index f3ade729..f80483a2 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -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 — fib_doubling.ml baseline (Fibonacci by + doubling, fib(40) = 102334155). Uses the identity F(2k) = F(k) * + (2*F(k+1) - F(k)) and F(2k+1) = F(k)^2 + F(k+1)^2 to compute fib + in O(log n) recursive depth. Returns a tuple (F(n), F(n+1)) at + each step. fib(40) = 102334155 fits in JS safe-int (< 2^53). 86 + baseline programs total. - 2026-05-09 Phase 5.1 — merge_two.ml baseline (merge two sorted lists, length*sum = 9*49 = 441). Standard two-finger merge with nested match-in-match. Used as a building block in merge_sort.ml