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


function zigzag( [int] $n ) {
    $zigzag=New-Object 'Object[,]' $n,$n
    $nodd = $n -band 1
    $nm1 = $n - 1
    $i=0;
    $j=0;
    foreach( $k in 0..( $n * $n - 1 ) ) {
        $zigzag[$i,$j] = $k
        $iodd = $i -band 1
        $jodd = $j -band 1
        if( ( $j -eq $nm1 ) -and ( $iodd -ne $nodd ) ) {
            $i++
        } elseif( ( $i -eq $nm1 ) -and ( $jodd -eq $nodd ) ) {
            $j++
        } elseif( ( $i -eq 0 ) -and ( -not $jodd ) ) {
            $j++
        } elseif( ( $j -eq 0 ) -and $iodd ) {
            $i++
        } elseif( $iodd -eq $jodd ) {
            $i--
            $j++
        } else {
            $i++
            $j--
        }
    }
    ,$zigzag
}
 
function displayZigZag( [int] $n ) {
    $a = zigzag $n
    0..$n | ForEach-Object {
        $b=$_
        $pad=($n*$n-1).ToString().Length
        "$(0..$n | ForEach-Object {
            "{0,$pad}" -f $a[$b,$_]
        } )"
    }
}