ocaml: phase 5.1 palindrome_sum.ml baseline (sum of 3-digit palindromes = 49500)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

let palindrome_sum lo hi =
    let total = ref 0 in
    for n = lo to hi do
      if is_pal n then total := !total + n
    done;
    !total

  palindrome_sum 100 999 = 49500

There are 90 three-digit palindromes (form aba; 9 choices for a, 10
for b). Average value 550, sum 49500.

Companion to palindrome.ml (predicate-only) and paren_depth.ml.

127 baseline programs total.
This commit is contained in:
2026-05-10 01:29:52 +00:00
parent 0b79d4d4b4
commit fb0e83d3a1
3 changed files with 24 additions and 0 deletions

View File

@@ -89,6 +89,7 @@
"mutable_record.ml": 10,
"option_match.ml": 5,
"palindrome.ml": 4,
"palindrome_sum.ml": 49500,
"paren_depth.ml": 7,
"partition.ml": 3025,
"pancake_sort.ml": 910,

View File

@@ -0,0 +1,19 @@
let is_pal n =
let s = string_of_int n in
let len = String.length s in
let p = ref true in
for i = 0 to len / 2 - 1 do
if s.[i] <> s.[len - 1 - i] then p := false
done;
!p
let palindrome_sum lo hi =
let total = ref 0 in
for n = lo to hi do
if is_pal n then total := !total + n
done;
!total
;;
palindrome_sum 100 999