function M = magic_even_singly ( n )

%*****************************************************************************80
%
%% magic_even_singly returns a MAGIC matrix of order divisible by 2, but not 4.
%
%  Modified:
%
%    16 December 2019
%
%  Author:
%
%    The Mathworks, Inc.
%
%  Input:
%
%    integer N, the order of A.
%    N must be divisible by 2, but not 4.
%
%  Output:
%
%    real A(N,N), the matrix.
%
   p = n / 2;
   M = oddOrderMagicSquare(p);
   M = [M       M+2*p^2; ...
        M+3*p^2 M+p^2];

   if n == 2
      return
   end

   i = (1:p)';
   k = (n-2)/4;
   j = [1:k (n-k+2):n];
   M([i; i+p],j) = M([i+p; i],j);
   i = k+1;
   j = [1 i];
   M([i; i+p],j) = M([i+p; i],j);
  return
end
function M = oddOrderMagicSquare(n)
  p = 1:n;
  M = n*mod(p'+p-(n+3)/2,n) + mod(p'+2*p-2,n) + 1;
  return
end

