ocaml: phase 5.1 gray_code.ml baseline (4-bit reflected Gray code, sum+len = 136)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Single-formula generation: gray[i] = i lxor (i lsr 1) For n = 4, generates 16 values, each differing from its neighbour by one bit. Output is a permutation of 0..15, so its sum equals the natural-sequence sum 120; +16 entries -> 136. Tests lsl / lxor / lsr together (the iter-127 bitwise ops) plus Array.make / Array.fold_left. 95 baseline programs total.
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
"fraction.ml": 7,
|
||||
"frequency.ml": 5,
|
||||
"gcd_lcm.ml": 60,
|
||||
"gray_code.ml": 136,
|
||||
"grep_count.ml": 3,
|
||||
"grid_paths.ml": 210,
|
||||
"group_consec.ml": 53,
|
||||
|
||||
12
lib/ocaml/baseline/gray_code.ml
Normal file
12
lib/ocaml/baseline/gray_code.ml
Normal file
@@ -0,0 +1,12 @@
|
||||
let gray n =
|
||||
let m = 1 lsl n in
|
||||
let result = Array.make m 0 in
|
||||
for i = 0 to m - 1 do
|
||||
result.(i) <- i lxor (i lsr 1)
|
||||
done;
|
||||
result
|
||||
|
||||
;;
|
||||
|
||||
let g = gray 4 in
|
||||
Array.fold_left (+) 0 g + Array.length g
|
||||
Reference in New Issue
Block a user