Magic Square

An n × n square grid contains numbers from 1 to n × n, with each number appearing exactly once. A magic square is one in which the sum of each column, each row, and each diagonal is equal.

For creating an odd-sized magic square, the following method can be used:
1. Place 1 in the square just below the center.
2. Place the next number in the square diagonally down to the right. If the square is out of bounds or already filled, follow specific rules to find the next square.
3. Repeat steps 1 and 2 until all squares are filled.

Create a program that takes the size of the square, n, as input and outputs the magic square of that size. The input n is an odd number between 3 and 15. Each number in the square should be output with 4 digits, right-aligned.

Input
Multiple inputs are given. Each input provides a positive integer n on a single line. The input ends with 0. There will be no more than 10 inputs.

Output
For each input, output the n × n magic square.

Sample Input
3
5
0

Output for the Sample Input
   4   9   2
   3   5   7
   8   1   6
  11  24   7  20   3
   4  12  25   8  16
  17   5  13  21   9
  10  18   1  14  22
  23   6  19   2  15

# Buggy version:
from sys import stdin
def sqmatrix(n):
  return [[0] * n for i in range(n)]

def magicsq(m):
    n = len(m)
    j = int(n/2)
    i = j + 1
    for num in range(1, n**2 + 1):
        m[i][j] = num
        i += 1
        j += 1
        i = i%n
        j = j % n
        if m[i][j] != 0:
            i += 1
            j -= 1
        if j < 0:
            j = n -1
        i = i % n
    return m

for line in stdin:
    n = int(line)
    if n == 0:
        break
    sm = sqmatrix(n)
    for row in magicsq(sm): print (' '.join(map(str,row)))

# Fixed version:
from sys import stdin
def sqmatrix(n):
  return [[0] * n for i in range(n)]

def magicsq(m):
    n = len(m)
    j = int(n/2)
    i = j + 1
    for num in range(1, n**2 + 1):
        m[i][j] = num
        i += 1
        j += 1
        i = i%n
        j = j % n
        if m[i][j] != 0:
            i += 1
            j -= 1
        if j < 0:
            j = n -1
        i = i % n
    return m

for line in stdin:
    n = int(line)
    if n == 0:
        break
    sm = sqmatrix(n)
    for row in magicsq(sm): print (''.join(map(lambda x: str(x).rjust(4, ' '),row)))

# Buggy version:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0078
"""
import sys



class MagicSquare():
    def __init__(self, size):
        self.size = size
        self.data = [['@']*self.size for _ in range(self.size)]
        self.fill_data()

    def fill_data(self):
        num = 1
        x = self.size // 2
        y = x + 1
        self.data[y][x] = num

        while num < self.size**2:
            x += 1
            if x >= self.size:
                x = 0
            y += 1
            if y >= self.size:
                y = 0
            if self.data[y][x] == '@':
                num += 1
                self.data[y][x] = num
            else:
                while self.data[y][x] != '@':
                    x -= 1
                    if x < 0:
                        x = self.size - 1
                    y += 1
                    if y >= self.size:
                        y = 0
                num += 1
                self.data[y][x] = num

    def print_data(self):
        digits = len(str(self.size**2))
        for row in self.data:
            print(*row)


def main(args):
    while True:
        size = int(input())
        if size < 3 or size%2 == 0:
            break
        ms = MagicSquare(size)
        ms.print_data()


if __name__ == '__main__':
    main(sys.argv[1:])

# Fixed version:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0078
"""
import sys



class MagicSquare():
    def __init__(self, size):
        self.size = size
        self.data = [['@']*self.size for _ in range(self.size)]
        self.fill_data()

    def fill_data(self):
        num = 1
        x = self.size // 2
        y = x + 1
        self.data[y][x] = num

        while num < self.size**2:
            x += 1
            if x >= self.size:
                x = 0
            y += 1
            if y >= self.size:
                y = 0
            if self.data[y][x] == '@':
                num += 1
                self.data[y][x] = num
            else:
                while self.data[y][x] != '@':
                    x -= 1
                    if x < 0:
                        x = self.size - 1
                    y += 1
                    if y >= self.size:
                        y = 0
                num += 1
                self.data[y][x] = num

    def print_data(self):
        for row in self.data:
            temp = ""
            for d in row:
                temp += str(d).rjust(4)
            print(temp)
            temp = ""



def main(args):
    while True:
        size = int(input())
        if size < 3 or size%2 == 0:
            break
        ms = MagicSquare(size)
        ms.print_data()


if __name__ == '__main__':
    main(sys.argv[1:])