Task

Produce a spiral array.



A   spiral array   is a square arrangement of the first    N2   natural numbers,   where the

numbers increase sequentially as you go around the edges of the array spiraling inwards.



For example, given   5,   produce this array:

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



Related tasks

   Zig-zag matrix 
   Identity_matrix
   Ulam_spiral_(for_primes)


  [ stack ]                  is stepcount (     --> s )
  [ stack ]                  is position  (     --> s )
  [ stack ]                  is heading   (     --> s )
 
  [ heading take 
    behead join 
    heading put ]            is right     (     -->   )
 
  [ heading share 0 peek
    unrot times
      [ position share
        stepcount share 
        unrot poke
        over position tally
        1 stepcount tally ]
     nip ]                   is walk      ( [ n --> [ )
 
  [ dip [ temp put [] ]
    temp share times
       [ temp share split
         dip 
           [ nested join ] ]
    drop temp release ]      is matrixify ( n [ --> [ )
 
  [ 0 stepcount put      ( set up... )
    0 position put
    ' [ 1 ] over join
    -1 join over negate join
    heading put
    0 over dup * of
 
    over 1 - walk right  ( turtle draws spiral )
    over 1 - times
      [ i 1+ walk right
        i 1+ walk right ]
    1 walk
 
    matrixify            ( ...tidy up )
    heading release
    position release
    stepcount release ]      is spiral    (   n --> [ )
 
  9 spiral
  witheach
    [ witheach
        [ dup 10 < if sp echo sp ]
      cr ]