From ce013fa138019943a4740c58b9217ed0b2657d9f Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 15:53:47 +0000 Subject: [PATCH] ocaml: phase 5.1 fib_mod.ml baseline (Fibonacci mod prime, fib(100) mod 1000003 = 391360) 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. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/fib_mod.ml | 13 +++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 20 insertions(+) create mode 100644 lib/ocaml/baseline/fib_mod.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index b7f41169..8c6e86e7 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -24,6 +24,7 @@ "expr_eval.ml": 16, "expr_simp.ml": 22, "factorial.ml": 3628800, + "fib_mod.ml": 391360, "fraction.ml": 7, "frequency.ml": 5, "gcd_lcm.ml": 60, diff --git a/lib/ocaml/baseline/fib_mod.ml b/lib/ocaml/baseline/fib_mod.ml new file mode 100644 index 00000000..9dccf4f7 --- /dev/null +++ b/lib/ocaml/baseline/fib_mod.ml @@ -0,0 +1,13 @@ +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 + +;; + +fib_mod 100 1000003 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index f29b437d..93229db5 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_mod.ml baseline (Fibonacci mod prime, + fib(100) mod 1000003 = 391360). Iterative two-ref Fibonacci with + modular reduction at every step to keep intermediate values + bounded. The 100th Fibonacci is 354224848179261915075, which + exceeds JS safe-int range; modular arithmetic on each step keeps + computations within int53 precision. 76 baseline programs total. - 2026-05-09 Phase 5.1 — luhn.ml baseline (Luhn check digit, 2/4 inputs valid). Walks digits right-to-left, doubles every other starting from the second-from-right; if doubled value > 9