Spirals

Interview

Time limit1sMemory limit128 MB

Summary
Place the numbers x through y into a square-ish grid spiraling counter-clockwise outward from x at the center, then print the grid with numbers right-aligned to the width of y.
Level

Medium4 of 10

Topics
Matrix, Simulation, Implementation
Solved
No attempts yet

Problem

You are given two positive integers xx and yy, both less than 100100. Print the integers from xx to yy inclusive, laid out in a spiral. You may assume that the end value yy is greater than or equal to the start value xx.

The spiral begins with the first number xx in the centre. The next number is placed immediately below the first number. From there the numbers keep increasing by 11, winding outward in a counter-clockwise direction, until the last number yy has been placed.

Input

The first line contains the start value xx; the second line contains the end value yy. Both are positive integers less than 100100, and x≤yx \le y.

Output

Print the grid of numbers from xx to yy arranged in the spiral. Within each row the numbers are separated by a single space, and every number is right-aligned to the width of the largest number (that is, yy, by its digit count).

Examples2

  1. Example 1

    Input
    10
    27
    
    Expected output
          27 26
    16 15 14 25
    17 10 13 24
    18 11 12 23
    19 20 21 22
    
  2. Example 2

    Input
    7
    12
    
    Expected output
    12 11
     7 10
     8  9