ocaml: phase 5.1 count_bits.ml baseline (sum popcount 0..100 = 319)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
DP recurrence for popcount that avoids host bitwise operations: result[i] = result[i / 2] + (i mod 2) Drops the low bit (i / 2 stands in for i lsr 1) and adds it back if it was 1 (i mod 2 stands in for i land 1). sum over 0..100 of popcount(i) = 319 Tests pure-arithmetic popcount, accumulating ref + DP array, classic look-back to half-index pattern. 197 baseline programs total.
This commit is contained in:
14
lib/ocaml/baseline/count_bits.ml
Normal file
14
lib/ocaml/baseline/count_bits.ml
Normal file
@@ -0,0 +1,14 @@
|
||||
let count_bits n =
|
||||
let result = Array.make (n + 1) 0 in
|
||||
for i = 1 to n do
|
||||
result.(i) <- result.(i / 2) + (i mod 2)
|
||||
done;
|
||||
let sum = ref 0 in
|
||||
for i = 0 to n do
|
||||
sum := !sum + result.(i)
|
||||
done;
|
||||
!sum
|
||||
|
||||
;;
|
||||
|
||||
count_bits 100
|
||||
@@ -32,6 +32,7 @@
|
||||
"convex_hull.ml": 5,
|
||||
"coin_change.ml": 6,
|
||||
"coin_min.ml": 6,
|
||||
"count_bits.ml": 319,
|
||||
"count_change.ml": 406,
|
||||
"count_paths_dag.ml": 3,
|
||||
"count_inversions.ml": 12,
|
||||
|
||||
Reference in New Issue
Block a user