Problems
Write a program that, given the size of an odd square, fills it as a magic square.
A magic square is an
Conditions
Fill the numbers in the square in the following order:
Place the first number, 1, in the middle of the first row.
For each subsequent number:
If the previous number is a multiple of
N , move directly below to place the next number.Otherwise, move one row up and one column to the left to place the next number.
If moving up goes above the first row, wrap around to the last row.
If moving left goes beyond the first column, wrap around to the last column.
Example for N=3
Place 1 in the middle of the first row (row 1, column 2).
Move diagonally up-left to (row 0, column 1). Row 0 is above the first row, so wrap to the last row (row 3, column 2) and place 2.
Move diagonally up-left to (row 2, column 0). Column 0 is before the first column, so wrap to the last column (row 2, column 3) and place 3.
Since 3 is a multiple of
N , move directly below to (row 3, column 3) and place 4.Move diagonally up-left to (row 2, column 2) and place 5.
Move diagonally up-left to (row 1, column 1) and place 6.
Since 6 is a multiple of
N , move directly below to (row 2, column 1) and place 7.Move diagonally up-left to (row 1, column 0). Column 0 is before the first column, so wrap to the last column (row 1, column 3) and place 8.
Continue in the same manner until all numbers are placed.
Input
A single odd integer π (1 β€ π β€ 99) representing the size of the square.
Output
Print the completed
Numbers in each row should be separated by a space.
Example #1
3
6 1 8
7 5 3
2 9 4
Example #2
5
15 8 1 24 17
16 14 7 5 23
22 20 13 6 4
3 21 19 12 10
9 2 25 18 11