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


5 var Size
0 Size repeat Size repeat
 
1 var i 1 var j
 
Size 2 power for
    swap i get rot j set i set 
    i j + 1 bitand 0 == IF
        j Size < IF j 1 + var j ELSE i 2 + var i ENDIF
        i 1 > IF i 1 - var i ENDIF
    ELSE
        i Size < IF i 1 + var i ELSE j 2 + var j ENDIF
        j 1 > IF j 1 - var j ENDIF
    ENDIF
endfor
 
Size FOR
    var row
    Size FOR
        var col
        row get col get tostr 32 32 chain chain 1 3 slice print drop drop
    ENDFOR
    nl
ENDFOR