Page not loading? Try clicking here.
Placeholder

#2074

Odd magic square 1s 64MB

Problems

Write a program that, given the size of an odd square, fills it as a magic square.

A magic square is an N \times N square filled with the numbers from 1 to N \times N, each used exactly once, so that the sums of each row, each column, and both main diagonals are all equal.

Conditions

Fill the numbers in the square in the following order:

  1. Place the first number, 1, in the middle of the first row.

  2. 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.

  3. If moving up goes above the first row, wrap around to the last row.

  4. 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 N \times N magic square.

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


Source

JUNGOL

You must sign in to write code.