Walk Like an Egyptian

Time limit1sMemory limit128 MB

Summary
Stones fill an N by N grid along a spiraling quarter-circle path; find the number placed in the top-right cell. Multiple N values per input.
Level

Medium5 of 10

Topics
Simulation, Implementation, Math, Array
Solved
No attempts yet

Problem

Walk Like an Egyptian is an old multiplayer board game once played by children of the Sahara nomad tribes. Children would collect stones and number each one. A game with NN players needs N2N^2 stones, and each player picks NN of them. The stones are laid out on an N×NN \times N grid in a peculiar order, as shown in Figure (a) (for N=4N = 4). The player whose stone lands in the top-right corner loses the round. A new round is then played with N−1N - 1 players, and N−1N - 1 rounds in total decide the winner.

The layout follows an old rule that the Pharaohs' workers used to cross a dark room inside a pyramid without losing anyone (see Figure (b)):

  1. The first worker stands in the lower-left corner of the room.
  2. The next three workers stand around the first one, forming a quarter circle in the anti-clockwise direction.
  3. The next five workers stand around those three, this time forming a quarter circle in the clockwise direction.
  4. They keep repeating the previous two steps until the room is full. Whenever they reach the left wall or the bottom wall, they begin a larger quarter circle and alternate the direction between clockwise and anti-clockwise.

The stones are numbered 1,2,3,…,N21, 2, 3, \ldots, N^2 in the exact order in which the workers take their places. Given NN, determine the number written on the stone that ends up in the top-right corner of the grid.

Input

The input contains one or more test cases. Each test case is given on its own line as a single integer NN (0<N<10000 < N < 1000), the number of players.

The input ends with a line containing a single zero, which is not part of the test data.

Output

For each test case, print one line in the form N => result, where NN is the number of players for that test case and result is the number on the stone at the top-right corner of the grid.

Examples1

  1. Example 1

    Input
    4
    2
    0
    
    Expected output
    4 => 13
    2 => 3