From 5c1b4349aabd918249bb703a7fd067d0fe39cbca Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 02:46:20 +0000 Subject: [PATCH] ocaml: phase 5.1 harshad.ml baseline (count Niven/Harshad numbers <= 100 = 33) A Harshad (or Niven) number is divisible by its digit sum: let count_harshad limit = let c = ref 0 in for n = 1 to limit do if n mod (digit_sum n) = 0 then c := !c + 1 done; !c count_harshad 100 = 33 All single-digit numbers (1..9) qualify trivially. Plus 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100 (24 more) = 33 total under 100. 133 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/harshad.ml | 19 +++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 26 insertions(+) create mode 100644 lib/ocaml/baseline/harshad.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 209ee3f4..03ab6707 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -59,6 +59,7 @@ "grid_paths.ml": 210, "group_consec.ml": 53, "hailstone.ml": 111, + "harshad.ml": 33, "hamming.ml": 4, "hanoi.ml": 1023, "hist.ml": 75, diff --git a/lib/ocaml/baseline/harshad.ml b/lib/ocaml/baseline/harshad.ml new file mode 100644 index 00000000..cff87551 --- /dev/null +++ b/lib/ocaml/baseline/harshad.ml @@ -0,0 +1,19 @@ +let digit_sum n = + let m = ref n in + let s = ref 0 in + while !m > 0 do + s := !s + !m mod 10; + m := !m / 10 + done; + !s + +let count_harshad limit = + let c = ref 0 in + for n = 1 to limit do + if n mod (digit_sum n) = 0 then c := !c + 1 + done; + !c + +;; + +count_harshad 100 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 993a5dab..e121528e 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-10 Phase 5.1 — harshad.ml baseline (count Niven/Harshad + numbers ≤ 100 = 33). A Harshad number is divisible by its digit + sum. All single-digit numbers qualify trivially (9). Plus 10, 12, + 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, + 72, 80, 81, 84, 90, 100 (24 more) = 33 under 100. 133 baseline + programs total. - 2026-05-10 Phase 5.1 — reverse_int.ml baseline (digit-reverse, reverse(12345) + reverse(100) + reverse(7) = 54321 + 1 + 7 = 54329). Walks digits via mod 10 / div 10, accumulating the