Problem. Arrange in an grid so that every row, column and both main diagonals have the same sum, the magic constant (). Possible for every .

MethodIdea
oddSiamese (de la Loubère)walk up-right, wrap around; step down when the cell is taken
(doubly even)complement the diagonals of every blockwrite in order, replace by on the marked cells
(singly even, )LUX (Conway)take an odd square of order , blow each cell up into a block filled in an L, U or X pattern
impossible forces
OrderNumber of magic squares (rotations and reflections counted)
38 (all are the Lo Shu square up to symmetry)
47040 (880 up to symmetry; Frénicle, 1693)
5 (Schroeppel, 1973, computer count)
vector<vector<int>> magicSquareAny(int n) {           // any n != 2; values 1..n^2
    vector<vector<int>> a(n, vector<int>(n));
    if (n % 2 == 1) {                                  // Siamese
        int r = 0, c = n / 2;
        for (int v = 1; v <= n * n; v++) {
            a[r][c] = v;
            int nr = (r - 1 + n) % n, nc = (c + 1) % n;
            if (a[nr][nc]) nr = (r + 1) % n, nc = c;
            r = nr, c = nc;
        }
    } else if (n % 4 == 0) {                           // doubly even
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
            int v = i * n + j + 1;
            a[i][j] = (i % 4 == j % 4 || i % 4 + j % 4 == 3) ? n * n + 1 - v : v;
        }
    } else {                                           // LUX, n = 2k, k = 2m + 1 odd
        int k = n / 2, m = (k - 1) / 2;
        auto A = magicSquareAny(k);
        static const int pat[3][2][2] = {{{4, 1}, {2, 3}}, {{1, 4}, {2, 3}}, {{1, 4}, {3, 2}}};   // L, U, X
        for (int i = 0; i < k; i++) for (int j = 0; j < k; j++) {
            int t = i <= m ? 0 : i == m + 1 ? 1 : 2;   // rows 0..m: L, row m+1: U, the rest: X
            if (j == m && i == m) t = 1;               // swap the middle L with the U below it
            if (j == m && i == m + 1) t = 0;
            for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) a[2 * i + x][2 * j + y] = 4 * (A[i][j] - 1) + pat[t][x][y];
        }
    }
    return a;
}