let n = 5

let make_magic () =
  let m = Array.init n (fun _ -> Array.make n 0) in
  let row = ref 0 in
  let col = ref (n / 2) in
  for k = 1 to n * n do
    m.(!row).(!col) <- k;
    let nr = (!row - 1 + n) mod n in
    let nc = (!col + 1) mod n in
    if m.(nr).(nc) <> 0 then begin
      row := (!row + 1) mod n
    end else begin
      row := nr;
      col := nc
    end
  done;
  m

;;

let m = make_magic () in
let sum_diag = ref 0 in
for i = 0 to n - 1 do
  sum_diag := !sum_diag + m.(i).(i)
done;
!sum_diag
