HCI '16 - Ulam Spiral

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 0.6s
Memory limit: 64M

Authors:
Problem type

Ulam Spiral

"Ulam's spiral is a plot in which positive integers are arranged in a spiral. It was discovered by the mathematician Stanislaw Ulam in 1963, while he was doodling during the presentation of a "long and very boring paper" at a scientific meeting. ...Ulam constructed the spiral by writing down a regular rectangular grid of numbers, starting with 1 at the center, and spiraling out." [adapted from Wikipedia]

You are a student who, like Ulam, also doodles during the presentation of a "long and very boring" lesson. However, there is a finite amount of space on your doodling surface (paper), and you have gotten bored of drawing the spiral. The solution to this would be to write a program to draw the spiral for you.

Given an integer N, output the Ulam spiral for an N \times N square. Please refer to the sample output for more details.

Input Specification

The input will contain exactly one line with one integer, N.

Output Specification

The output should contain the integer matrix representing the spiral. Each number in the spiral should be padded with spaces until it is three characters long. For example, 16 should be printed as X16 and 1 should be printed as XX1, where spaces are represented by X. On top of this, there should be 1 space between each number. As can be seen in sample output 1, there are 3 spaces between 5 and 4 (1 from the spacing, 2 from the padding). There should not be any trailing spaces at the end of each row.

Constraints

Subtask 1 [0%]

Sample test cases.

Subtask 2 [37%]

1 \le N \le 9

Subtask 3 [63%]

1 \le N \le 31

Sample Input 1

4

Sample Output 1

 16  15  14  13
  5   4   3  12
  6   1   2  11
  7   8   9  10

Sample Input 2

5

Sample Output 2

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

Comments