Task

Produce a zig-zag array.



A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the

numbers increase sequentially as you zig-zag along the array's   anti-diagonals.

For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images).



For example, given   5,   produce this array:

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



Related tasks

   Spiral matrix
   Identity matrix
   Ulam spiral (for primes)


See also

   Wiktionary entry:   anti-diagonals


/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
zigzag(5)
 
return
 
method zigzag(msize) public static
 
  row = 1
  col = 1
 
  ziggy = Rexx(0)
  loop j_ = 0 for msize * msize
    ziggy[row, col] = j_
    if (row + col) // 2 == 0 then do
      if col < msize then -
        col = col + 1
      else row = row + 2
      if row \== 1 then -
        row = row - 1
      end
    else do
      if row < msize then -
        row = row + 1
      else col = col + 2
      if col \== 1 then -
        col = col - 1
      end
    end j_
 
  L = (msize * msize - 1).length             /*for a constant element width.  */
  loop row = 1 for msize                     /*show all the matrix's rows.    */
    rowOut = ''
    loop col = 1 for msize
      rowOut = rowOut ziggy[row, col].right(L)
      end col
    say rowOut
    end row
 
  return
 