with(ArrayTools): spiralArray := proc(size::integer) local M, sideLength, count, i, j: M := Matrix(size): count := 0: sideLength := size: for i from 1 to ceil(sideLength / 2) do for j from 1 to sideLength do M[i,i + j - 1] := count++: end: for j from 1 to sideLength - 1 do M[i + j, sideLength + i - 1] := count++: end: for j from 1 to sideLength - 1 do M[i + sideLength - 1, sideLength - j + i - 1] := count++: end: for j from 1 to sideLength - 2 do M[sideLength + i - j - 1, i] := count++ end: sideLength -= 2: end: return M; end proc: spiralArray(5);