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
