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


 
call printArray zigzag(3)
say
call printArray zigzag(4)
say
call printArray zigzag(5)
 
::routine zigzag
  use strict arg size
 
  data = .array~new(size, size)
  row = 1
  col = 1
 
  loop element = 0 to (size * size) - 1
      data[row, col] = element
      -- even stripes
      if (row + col) // 2 = 0 then do
          if col < size then col += 1
          else row += 2
          if row > 1 then row -= 1
      end
      -- odd rows
      else do
          if row < size then row += 1
          else col += 2
          if col > 1 then col -= 1
      end
  end
 
  return data
 
::routine printArray
  use arg array
  dimension = array~dimension(1)
  loop i = 1 to dimension
      line = "|"
      loop j = 1 to dimension
          line = line array[i, j]~right(2)
      end
      line = line "|"
      say line
   end
 