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


 
local zigzag = {}
 
function zigzag.new(n)
    local a = {}
    local i -- cols
    local j -- rows
 
    a.n = n
    a.val = {}
 
    for j = 1, n do
        a.val[j] = {}
        for i = 1, n do
            a.val[j][i] = 0
        end
    end
 
    i = 1
    j = 1
 
    local di
    local dj
    local k = 0
 
    while k < n * n do
        a.val[j][i] = k
        k = k + 1
        if i == n then
            j = j + 1
            a.val[j][i] = k
            k = k + 1
            di = -1
            dj = 1
        end
        if j == 1 then
            i = i + 1
            a.val[j][i] = k
            k = k + 1
            di = -1
            dj = 1
        end
        if j == n then
            i = i + 1
            a.val[j][i] = k
            k = k + 1
            di = 1
            dj = -1
        end
        if i == 1 then
            j = j + 1
            a.val[j][i] = k
            k = k + 1
            di = 1
            dj = -1
        end
        i = i + di
        j = j + dj
    end
 
    setmetatable(a, {__index = zigzag, __tostring = zigzag.__tostring})
    return a
end
 
function zigzag:__tostring()
    local s = {} 
    for j = 1, self.n do
        local row = {}
        for i = 1, self.n do
            row[i] = string.format('%d', self.val[j][i])
        end
        s[j] = table.concat(row, ' ')
    end
    return table.concat(s, '\n')
end
 
print(zigzag.new(5))
 