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)


function spiral(integer dimension)
    integer side, curr, curr2
    sequence s
    s = repeat(repeat(0,dimension),dimension)
    side = dimension
    curr = 0
    for i = 0 to floor(dimension/2) do
        for j = 1 to side-1 do
            s[i+1][i+j] = curr -- top
            curr2 = curr + side-1
            s[i+j][i+side] = curr2 -- right
            curr2 += side-1
            s[i+side][i+side-j+1] = curr2 -- bottom
            curr2 += side-1
            s[i+side-j+1][i+1] = curr2 -- left
            curr += 1
        end for
        curr = curr2 + 1
        side -= 2
    end for
 
    if remainder(dimension,2) then
        s[floor(dimension/2)+1][floor(dimension/2)+1] = curr
    end if
 
    return s
end function
 
? spiral(5)