A Magic Square contains the integers 1 to n^(2), arranged in an n by n array such that the columns, rows and both main diagonals add up to the same number.For doubly even positive integers (multiples of 4) the following method can be used to create a magic square.
Fill an array with the numbers 1 to n^(2) in succession. Then, for each 4 by 4 subarray, replace the entries on the blue and red diagonals by n^(2)+1-aij.
So, in the following example, a11 (row 1, column 1) was initially 1 and is replaced by 8^(2)+1-1 = 64


    tab1 { padding-left: 2em; }
    fc1  { font color="blue"}


n=8
642 3 61606 7 57 
9 55541213515016
1747462021434224
4026273736303133
3234352928383925
4123224445191848
4915145253111056
8 58595 4 62631 

The function even_magic() should return a 2D array as follows:-
Example:
n=4: Output: [[16,2,3,13],[5,11,10,8],[9,7,6,12],[4,14,15,1]]

162 3 13
5 11108 
9 7 6 12
4 14151 


Only doubly even numbers will be passed to the function in the tests.

See mathworld.wolfram.com for further details.

Difficulty: introductory

Starter code:

def even_magic(n):
	

Solutions:
def even_magic(n):
    return [ [ n*n-(y*n+x) if x%4==y%4 or (x%4+y%4)%4==3 else y*n+x+1 for x in range(n)] for y in range(n) ]

def even_magic(n):
    l = list(range(1,n*n+1))
    for i in range(n*n//2):
        y,x = (i//n)%4,(i%n)%4
        if y==x or x+y==3: l[i],l[n*n-1-i] = l[n*n-1-i], l[i]
    return [l[i*n:i*n+n] for i in range(n)]

import numpy as np

def even_magic(n):
    mx    = n*n
    draft = np.arange(1, mx +1).reshape((n, n))
    art   = np.arange(mx, 0,-1).reshape((n, n))
    
    for i in range(n):
        for y in range(n):
            if any(( all((i%4 in [0,3], y%4 in [0,3])), all((i%4 in [1,2], y%4 in [1,2])) )):
                draft[i][y] = art[i][y]
                
    return draft.tolist()

def even_magic(n):
    n2_1 = n ** 2 + 1
    return [
        [n2_1 - (i*n+j+1) if i % 4 == j % 4 or (i + j) % 4 == 3 else i*n+j+1 for j in range(n)]
        for i in range(n)
    ]

def even_magic(n):
    sq = [[y*n+x+1 for x in range(n)] for y in range(n)]
    
    for x in range(n):
        for y in range(n):
            if y % 4 == x % 4 or y % 4 == 3-x % 4:
                sq[x][y] = n**2 + 1 - sq[x][y]
    return sq


def even_magic(n):
    numbers, p = iter(list(range(1, n * n + 1))), 0
    main_grid = [[next(numbers) for _ in range(n)] for _ in range(n)]
    A = lambda s,se,see,e,ee,eee:list(zip(range(s,se,see),range(e,ee,eee)))
    for i in range(n//4):
        o = 0
        for j in range(n//4):
            for k, l in A(p, p + 4, 1, o, o + 4, 1) + A(p, p + 4, 1, o + 3, o - 1, -1):
                main_grid[k][l] = ((n ** 2) + 1) - main_grid[k][l]
            o += 4
        p += 4
    return main_grid

def even_magic(n):
    square = []
    for j in range(0, n):
        row = []
        for i in range(0, n):
            v = i + j * n + 1
            if i % 4 == j % 4 or -(i + 1) % 4 == j % 4:
                row.append(n**2 + 1 - v)
            else:
                row.append(v)
        square.append(row)   
    return square

def even_magic(n):
    arr = [];
    for i in range(0, n):
        arr.append([])
        for j in range(0, n):
            if i%4==j%4 or i%4+j%4 == 3:
                arr[i].append(n*n-i*n-j)
            else:
                arr[i].append(i*n+j+1)
    return arr

even_magic=lambda n:[[y%4in(x%4,3-x%4)and n*n-n*y-x or n*y+x+1for x in range(n)]for y in range(n)]

def even_magic(n):
    result, n2 = [[n*i+j+1 for j in range(n)] for i in range(n)], n**2
    for x in range(n):
        result[x][x] = n2 + 1 - result[x][x]
        result[x][-x-1] = n2 + 1 - result[x][-x-1]
    for i in range(4, n, 4):
        for x in range(n-i):
            result[i+x][x] = n2 + 1 - result[i+x][x]
            result[x][i+x] = n2 + 1 - result[x][i+x]
            result[x][-x-1-i] = n2 + 1 - result[x][-x-1-i]
            result[i+x][-x-1] = n2 + 1 - result[i+x][-x-1]
    return result