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


100 PROGRAM "ZigZag.bas"
110 LET SIZE=5
120 NUMERIC A(1 TO SIZE,1 TO SIZE)
130 LET I,J=1
140 FOR E=0 TO SIZE^2-1
150   LET A(I,J)=E
160   IF ((I+J) BAND 1)=0 THEN
170     IF J<SIZE THEN
180       LET J=J+1
190     ELSE
200       LET I=I+2
210     END IF
220     IF I>1 THEN LET I=I-1
230   ELSE
240     IF I<SIZE THEN
250       LET I=I+1
260     ELSE
270       LET J=J+2
280     END IF
290     IF J>1 THEN LET J=J-1
300   END IF
310 NEXT
320 FOR ROW=1 TO SIZE
330   FOR COL=1 TO SIZE
340     PRINT USING " ##":A(ROW,COL);
350   NEXT
360   PRINT
370 NEXT