Zig-zag matrix: Difference between revisions

m
(→‎{{header|Haskell}}: (change output to aligned string))
 
(104 intermediate revisions by 41 users not shown)
Line 5:
 
 
A &nbsp; ''zig-zag'' &nbsp; array is a square arrangement of the first &nbsp; <big>N<sup>2</sup></big> &nbsp; integersnatural numbers, &nbsp; where the
<br>numbers increase sequentially as you zig-zag along the array's &nbsp; [https://en.wiktionary.org/wiki/antidiagonal anti-diagonals].
 
Line 23:
;Related tasks:
* &nbsp; [[Spiral matrix]]
* &nbsp; [[Identity_matrixIdentity matrix]]
* &nbsp; [[Ulam_spiral_Ulam spiral (for_primesfor primes)]]
 
 
Line 30:
* &nbsp; Wiktionary entry: &nbsp; [https://en.wiktionary.org/wiki/antidiagonal anti-diagonals]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F zigzag(n)
F compare(xy)
V (x, y) = xy
R (x + y, I (x + y) % 2 {-y} E y)
V xs = 0 .< n
R Dict(enumerate(sorted((multiloop(xs, xs, (x, y) -> (x, y))), key' compare)), (n, index) -> (index, n))
 
F printzz(myarray)
V n = Int(myarray.len ^ 0.5 + 0.5)
V xs = 0 .< n
print((xs.map(y -> @xs.map(x -> ‘#3’.format(@@myarray[(x, @y)])).join(‘’))).join("\n"))
 
printzz(zigzag(6))</syntaxhighlight>
 
{{out}}
<pre>
0 2 3 9 10 20
1 4 8 11 19 21
5 7 12 18 22 29
6 13 17 23 28 30
14 16 24 27 31 34
15 25 26 32 33 35
</pre>
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Zig-zag matrix 15/08/2015
ZIGZAGMA CSECT
USING ZIGZAGMA,R12 set base register
Line 112 ⟶ 139:
T DS (N*N)H t(n,n) matrix
YREGS
END ZIGZAGMA</langsyntaxhighlight>
{{out}}
<pre>
Line 120 ⟶ 147:
9 11 17 20 22
10 18 19 23 24
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE MAX_SIZE="10"
DEFINE MAX_MATRIX_SIZE="100"
 
INT FUNC Index(BYTE size,x,y)
RETURN (x+y*size)
 
PROC PrintMatrix(BYTE ARRAY a BYTE size)
BYTE i,j,v
FOR j=0 TO size-1
DO
FOR i=0 TO size-1
DO
v=a(Index(size,i,j))
IF v<10 THEN
Print(" ")
ELSE
Print(" ")
FI
PrintB(v)
OD
PutE()
OD
RETURN
 
PROC FillMatrix(BYTE ARRAY a BYTE size)
BYTE start,end
INT dir,i,j
 
start=0 end=size*size-1
i=0 j=0 dir=1
 
DO
a(Index(size,i,j))=start
a(Index(size,size-1-i,size-1-j))=end
start==+1 end==-1
i==+dir j==-dir
IF i<0 THEN
i==+1 dir=-dir
ELSEIF j<0 THEN
j==+1 dir=-dir
FI
UNTIL start>=end
OD
 
IF start=end THEN
a(Index(size,i,j))=start
FI
RETURN
 
PROC Test(BYTE size)
BYTE ARRAY mat(MAX_MATRIX_SIZE)
PrintF("Matrix size: %B%E",size)
FillMatrix(mat,size)
PrintMatrix(mat,size)
PutE()
RETURN
 
PROC Main()
Test(5)
Test(6)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Zig-zag_matrix.png Screenshot from Atari 8-bit computer]
<pre>
Matrix size: 5
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
 
Matrix size: 6
0 1 5 6 14 15
2 4 7 13 16 25
3 8 12 17 24 26
9 11 18 23 27 32
10 19 22 28 31 33
20 21 29 30 34 35
</pre>
 
=={{header|ActionScript}}==
<syntaxhighlight lang="as">
<lang as>
package
{
Line 160 ⟶ 270:
}
}
}</syntaxhighlight>
}
</lang>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Test_Zig_Zag is
Line 213 ⟶ 322:
begin
Put (Zig_Zag (5));
end Test_Zig_Zag;</langsyntaxhighlight>
The function Zig_Zag generates a square matrix filled as requested by the task.
 
Line 227 ⟶ 336:
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<langsyntaxhighlight lang="agena"># zig-zag matrix
 
makeZigZag := proc( n :: number ) :: table is
Line 274 ⟶ 383:
print()
od
epocs</langsyntaxhighlight>
{{out}}
<pre>
Line 292 ⟶ 401:
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<syntaxhighlight lang ="algol68">PROC zig zag = (INT n)[,]INT: (
PROC zig zag = (INT n)[,]INT: (
PROC move = (REF INT i, j)VOID: (
IF j < n THEN
Line 326 ⟶ 436:
[,]INT result = zig zag(dim);
FOR i TO dim DO
print((result[IF i,], new= 1 THEN "((" ELSE " (" lineFI));
FOR j TO dim DO
print(( whole( result[i,j], -3 ), IF j /= dim THEN "," ELSE "" FI ))
OD;
print((IF i = dim THEN "))" ELSE ")," FI, new line))
OD
#FI#</lang>
</syntaxhighlight>
{{out}}
<pre>
{|border="1" style="border-collapse: collapse; border: 5px double grey;"
|align=center width=50%| With formatted transput possible, e.g. [[ALGOL 68G]]
|align=center| '''not''' formatted transput possible, e.g. [[ELLA ALGOL 68]]
|-
|align=center|<pre>
(( 0, 1, 5, 6, 14),
( 2, 4, 7, 13, 15),
Line 341 ⟶ 452:
( 10, 18, 19, 23, 24))
</pre>
||<pre>
+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
</pre>
|}
 
=={{header|ALGOL W}}==
Based on the Agena sample.
<langsyntaxhighlight lang="algolw">begin % zig-zag matrix %
% z is returned holding a zig-zag matrix of order n, z must be at least n x n %
procedure makeZigZag ( integer value n
Line 409 ⟶ 512:
end
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 423 ⟶ 526:
 
{{trans|J}}
<langsyntaxhighlight lang="apl"> zz ← {⍵⍴⎕IO-⍨⍋⊃,/{(2|⍴⍵):⌽⍵⋄⍵}¨(⊂w)/¨⍨w{↓⍵∘.=⍨∪⍵}+/[1]⍵⊤w←⎕IO-⍨⍳×/⍵} ⍝ General zigzag (any rectangle)
zzSq ← {zz,⍨⍵} ⍝ Square zigzag
zzSq 5
Line 430 ⟶ 533:
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24</langsyntaxhighlight>
 
=={{header|AppleScript}}==
Line 437 ⟶ 540:
 
Here's a vector & matrix boundary detection approach to the Zig-zap matrix:
<langsyntaxhighlight AppleScriptlang="applescript">set n to 5 -- Size of zig-zag matrix (n^2 cells).
 
-- Create an empty matrix.
Line 473 ⟶ 576:
set end of i to return
end repeat
return return & m as string</langsyntaxhighlight>
But this can be improved upon by building the matrix by populating empty AppleScript lists (it's about 50% faster when n=50):<langsyntaxhighlight AppleScriptlang="applescript">set n to 5
 
set m to {}
Line 507 ⟶ 610:
set i's contents to (i as string) & return
end repeat
return return & m as string</syntaxhighlight>
{{out}} for both scripts is:<pre>
</lang>
{{out}} for both scripts is:<pre>"
0 1 5 6 14
2 4 7 13 15
Line 515 ⟶ 617:
9 11 17 20 22
10 18 19 23 24
"</pre>
 
 
===Recursive===
By functional composition:
 
<syntaxhighlight lang="applescript">-- zigzagMatrix
By functional composition
 
<lang AppleScript>-- zigzagMatrix
on zigzagMatrix(n)
-- diagonals :: n -> [[n]]
script diagonals
on lambda|λ|(n)
script mf
on diags(xs, iCol, iRow)
Line 545 ⟶ 645:
end script
diags(rangeenumFromTo(0, n * n - 1), 1, 1) of mf
end lambda|λ|
end script
-- oddReversed :: [a] -> Int -> [a]
script oddReversed
on lambda|λ|(lst, i)
if i mod 2 = 0 then
lst
Line 557 ⟶ 657:
reverse of lst
end if
end lambda|λ|
end script
rowsFromDiagonals(n, map(oddReversed, lambda|λ|(n) of diagonals))
end zigzagMatrix
 
 
-- TEST
on run
zigzagMatrix(5)
end run
 
 
 
-- Rows of given length from list of diagonals
Line 581 ⟶ 671:
-- lengthOverOne :: [a] -> Bool
script lengthOverOne
on lambda|λ|(lst)
length of lst > 1
end lambda|λ|
end script
Line 593 ⟶ 683:
filter(lengthOverOne, edge)) & residue)
else
[]{}
end if
end rowsFromDiagonals
 
 
---- TEST -----------------------------------------------------------------------
on run
zigzagMatrix(5)
end run
 
 
-- GENERIC FUNCTIONS
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
 
-- filter :: (a -> Bool) -> [a] -> [a]
Line 609 ⟶ 719:
repeat with i from 1 to lng
set v to item i of xs
if lambda|λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
 
-- head :: [a] -> a
on head(xs)
if length of xs > 0 then
item 1 of xs
else
missing value
end if
end head
 
-- map :: (a -> b) -> [a] -> [b]
Line 621 ⟶ 740:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
-- splitAt:: n -> list -> {n items from start of list, rest of list}
Line 640 ⟶ 771:
end if
end splitAt
 
-- head :: [a] -> a
on head(xs)
if length of xs > 0 then
item 1 of xs
else
missing value
end if
end head
 
-- tail :: [a] -> [a]
Line 657 ⟶ 779:
{}
end if
end tail</syntaxhighlight>
 
-- range :: Int -> Int -> [Int]
on range(m, n)
set d to 1
if n < m then set d to -1
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn</lang>
 
{{Out}}
<pre>{{0, 1, 5, 6, 14},
Line 688 ⟶ 786:
{9, 11, 17, 20, 22},
{10, 18, 19, 23, 24}}</pre>
----
===Optimised iterative===
This is an optimised version of the second iterative script above, with the rendition to text kept separate and corrected. With n = 50, it's about 7.6 times as fast as the script on which it's based.
 
<syntaxhighlight lang="applescript">on zigzagMatrix(n)
script o
property matrix : {} -- Matrix list.
property row : missing value -- Row sublist.
end script
repeat n times
set end of o's matrix to {} -- Build a foundation for the matrix out of n empty lists.
end repeat
set {r, d} to {1, -1} -- Row index and direction to next insertion row (negative = row above).
repeat with v from 0 to (n ^ 2) - 1 -- Values to insert.
set o's row to o's matrix's item r
repeat while ((count o's row) = n)
set r to r + 1
set d to 1
set o's row to o's matrix's item r
end repeat
set end of o's row to v
set r to r + d
if (r < 1) then
set r to 1
set d to -d
else if (r > n) then
set r to n
set d to -d
else if ((r > 1) and ((count o's matrix's item (r - 1)) = 1)) then
set d to -d
end if
end repeat
return o's matrix
end zigzagMatrix
 
-- Demo:
on matrixToText(matrix, w)
script o
property matrix : missing value
property row : missing value
end script
set o's matrix to matrix
set padding to " "
repeat with r from 1 to (count o's matrix)
set o's row to o's matrix's item r
repeat with i from 1 to (count o's row)
set o's row's item i to text -w thru end of (padding & o's row's item i)
end repeat
set o's matrix's item r to join(o's row, "")
end repeat
return join(o's matrix, linefeed)
end matrixToText
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
set n to 5
set matrix to zigzagMatrix(n)
linefeed & matrixToText(matrix, (count (n ^ 2 - 1 as integer as text)) + 2) & linefeed</syntaxhighlight>
 
{{output}}
<pre>
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
</pre>
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">100 S = 5
110 S2 = S ^ 2 : REM SQUARED
120 H = S2 / 2 : REM HALFWAY
Line 714 ⟶ 890:
340 NEXT X
350 PRINT
360 NEXT Y</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">zigzag: function [n][
result: map 1..n 'x -> map 1..n => 0
 
x: 1, y: 1, v: 0, d: 1
 
while [v < n^2][
if? all? @[1 =< x x =< n 1 =< y y =< n][
set get result (y-1) (x-1) v
x: x + d, y: y - d, v: v + 1
]
else[if? x > n [x: n, y: y + 2, d: neg d]
else[if? y > n [x: x + 2, y: n, d: neg d]
else[if? x < 1 [x: 1, d: neg d]
else[if y < 1 [y: 1, d: neg d]
]
]
]
]
]
result
]
 
zz: zigzag 5
loop zz 'row -> print map row 'col [pad to :string col 3]</syntaxhighlight>
 
{{out}}
 
<pre> 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</pre>
 
=={{header|ATS}}==
<syntaxhighlight lang="ats">
(* ****** ****** *)
//
Line 780 ⟶ 991:
 
(* ****** ****** *)
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
{{trans|lisp}}
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276307.html#276307 forum].
<langsyntaxhighlight AutoHotkeylang="autohotkey">n = 5 ; size
v := x := y := 1 ; initial values
Loop % n*n { ; for every array element
Line 805 ⟶ 1,016:
t .= "`n" ; row is complete
}
MsgBox %t% ; show output</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<langsyntaxhighlight lang="autoit">
#include <Array.au3>
$Array = ZigZag(5)
Line 835 ⟶ 1,046:
Next
Return $av_array
EndFunc ;==>ZigZag</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ZIG-ZAG_MATRIX.AWK [-v offset={0|1}] [size]
BEGIN {
Line 867 ⟶ 1,077:
}
exit(0)
}</syntaxhighlight>
}
</lang>
{{out}}
<pre>
Line 879 ⟶ 1,088:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> Size% = 5
DIM array%(Size%-1,Size%-1)
Line 901 ⟶ 1,110:
NEXT
PRINT
NEXT row%</langsyntaxhighlight>
{{out}}
<pre>
Line 910 ⟶ 1,119:
10 18 19 23 24
</pre>
 
=={{header|Beads}}==
This is a translation of the [[Zig-zag_matrix#C++|C++]] example.
<syntaxhighlight lang="beads">beads 1 program 'Zig-zag Matrix'
 
calc main_init
var test : array^2 of num = create_array(5)
printMatrix(test)
calc create_array(
dimension:num
):array^2 of num
var
result : array^2 of num
lastValue = dimension^2 - 1
loopFrom
loopTo
row
col
currDiag = 0
currNum = 0
loop
if (currDiag < dimension) // if doing the upper-left triangular half
loopFrom = 1
loopTo = currDiag + 1
else // doing the bottom-right triangular half
loopFrom = currDiag - dimension + 2
loopTo = dimension
loop count:c from:loopFrom to:loopTo
var i = loopFrom + c - 1
if (rem(currDiag, 2) == 0) // want to fill upwards
row = loopTo - i + loopFrom
col = i
else // want to fill downwards
row = i
col = loopTo - i + loopFrom
result[row][col] = currNum
inc currNum
inc currDiag
if (currNum > lastValue)
exit
return result
calc printMatrix(
matrix:array^2 of num
)
var dimension = tree_count(matrix)
var maxDigits = 1 + lg((dimension^2-1), base:10)
loop across:matrix ptr:rowp index:row
var tempstr : str
loop across:rowp index:col
tempstr = tempstr & " " & to_str(matrix[row][col], min:maxDigits)
log(tempstr)</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Befunge}}==
The size, ''N'', is specified by the first value on the stack - 5 in the example below. The upper limit is constrained only by the range of the playfield cells used for variables, since we're using an algorithm that calculates the values on the fly rather than building them up in memory. On an 8 bit interpreter this means an upper limit of at least 127, but with an extended cell range the size of ''N'' can be almost unlimited.
 
<langsyntaxhighlight lang="befunge">>> 5 >>00p0010p:1:>20p030pv >0g-:0`*:*-:00g:*1-55+/>\55+/:v v:,*84<
v:++!\**2p01:+1g01:g02$$_>>#^4#00#+p#1:#+1#g0#0g#3<^/+ 55\_$:>55+/\|
>55+,20g!00g10g`>#^_$$$@^!`g03g00!g04++**2p03:+1g03!\*+1*2g01:g04.$<</langsyntaxhighlight>
 
{{out}}
Line 924 ⟶ 1,192:
9 11 17 20 22
10 18 19 23 24</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Flip ← {m←2|+⌜˜↕≠𝕩 ⋄ (⍉𝕩׬m)+𝕩×m}
Zz ← {Flip ⍋∘⍋⌾⥊+⌜˜↕𝕩}</syntaxhighlight>
 
Example:
 
<syntaxhighlight lang="bqn">Zz 5</syntaxhighlight>
 
<pre>
┌─
╵ 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
</pre>
 
([https://mlochbaum.github.io/BQN/try.html#code=RmxpcCDihpAge23ihpAyfCvijJzLnOKGleKJoPCdlakg4ouEICjijYnwnZWpw5fCrG0pK/CdlanDl219ClpaICAg4oaQIHtGbGlwIOKNi+KImOKNi+KMvuKliivijJzLnOKGlfCdlal9CgpaWiA1 online REPL])
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 948 ⟶ 1,236:
/* free(s) */
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>% ./a.out 7
Line 958 ⟶ 1,246:
20 22 32 35 41 44 46
21 33 34 42 43 47 48</pre>
 
=={{header|C sharp}}==
 
<syntaxhighlight lang="csharp">public static int[,] ZigZag(int n)
{
int[,] result = new int[n, n];
int i = 0, j = 0;
int d = -1; // -1 for top-right move, +1 for bottom-left move
int start = 0, end = n * n - 1;
do
{
result[i, j] = start++;
result[n - i - 1, n - j - 1] = end--;
 
i += d; j -= d;
if (i < 0)
{
i++; d = -d; // top reached, reverse
}
else if (j < 0)
{
j++; d = -d; // left reached, reverse
}
} while (start < end);
if (start == end)
result[i, j] = start;
return result;
}</syntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <memory> // for auto_ptr
#include <cmath> // for the log10 and floor functions
Line 1,040 ⟶ 1,356:
{
printZigZagArray( getZigZagArray( 5 ) );
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,050 ⟶ 1,366:
</pre>
 
=={{header|C sharpCeylon}}==
<syntaxhighlight lang="ceylon">class ZigZag(Integer size) {
value data = Array {
for (i in 0:size)
Array.ofSize(size, 0)
};
variable value i = 1;
variable value j = 1;
for (element in 0 : size^2) {
data[j - 1]?.set(i - 1, element);
if ((i + j).even) {
if (j < size) {
j++;
}
else {
i += 2;
}
if (i > 1) {
i--;
}
}
else {
if (i < size) {
i++;
}
else {
j += 2;
}
if (j > 1) {
j--;
}
}
}
shared void display() {
for (row in data) {
for (element in row) {
process.write(element.string.pad(3));
}
print(""); //newline
}
}
}
 
shared void run() {
<lang csharp>public static int[,] ZigZag(int n)
value zz = ZigZag(5);
{
zz.display();
int[,] result = new int[n, n];
}</syntaxhighlight>
int i = 0, j = 0;
int d = -1; // -1 for top-right move, +1 for bottom-left move
int start = 0, end = n * n - 1;
do
{
result[i, j] = start++;
result[n - i - 1, n - j - 1] = end--;
 
i += d; j -= d;
if (i < 0)
{
i++; d = -d; // top reached, reverse
}
else if (j < 0)
{
j++; d = -d; // left reached, reverse
}
} while (start < end);
if (start == end)
result[i, j] = start;
return result;
}</lang>
 
=={{header|Clojure}}==
Purely functional approach.
<langsyntaxhighlight Clojurelang="clojure">(defn partitions [sizes coll]
(lazy-seq
(when-let [n (first sizes)]
Line 1,112 ⟶ 1,452:
( 9 11 18 23 27 32)
(10 19 22 28 31 33)
(20 21 29 30 34 35))</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
# Calculate a zig-zag pattern of numbers like so:
# 0 1 5
Line 1,156 ⟶ 1,496:
console.log "---- n=#{n}"
console.log zig_zag_matrix(n)
console.log "\n"</syntaxhighlight>
</lang>
 
{{out}}
Line 1,185 ⟶ 1,524:
[ 20, 21, 29, 30, 34, 35 ] ]
</pre>
 
 
=={{header|Common Lisp}}==
==={{trans|Java}} (but with zero-based indexes and combining the even and odd cases)===
<langsyntaxhighlight lang="lisp">(defun zigzag (n)
(flet ((move (i j)
(if (< j (1- n))
Line 1,202 ⟶ 1,540:
(setf (values x y) (move x y))
(setf (values y x) (move y x)))
finally (return a))))</langsyntaxhighlight>
 
===An alternative approach===
<langsyntaxhighlight lang="lisp">
; ZigZag
;
Line 1,223 ⟶ 1,561:
(format t "~3d" (nth j st))
(setf (nth j st) (+ (nth j st) (nth j dx)))))))
</syntaxhighlight>
</lang>
(ZigZag 5) Produces:
<pre>
Line 1,254 ⟶ 1,592:
36 38 52 55 65 68 74 77 79
37 53 54 66 67 75 76 80 81
</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">def zigzag(n)
(seq=(0...n).to_a).product(seq)
.sort_by {|x,y| [x+y, (x+y).even? ? y : -y]}
.map_with_index{|v, i| {v, i}}.sort.map(&.last).each_slice(n).to_a
end
def print_matrix(m)
format = "%#{m.flatten.max.to_s.size}d " * m[0].size
m.each {|row| puts format % row}
end
print_matrix zigzag(5)</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|D}}==
{{trans|Common Lisp}}
<langsyntaxhighlight lang="d">int[][] zigZag(in int n) pure nothrow @safe {
static void move(in int n, ref int i, ref int j)
pure nothrow @safe @nogc {
Line 1,281 ⟶ 1,642:
 
writefln("%(%(%2d %)\n%)", 5.zigZag);
}</langsyntaxhighlight>
{{out}}
<pre> 0 1 5 6 14
Line 1,292 ⟶ 1,653:
{{trans|Scala}}
Same output.
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.array;
 
int[][] zigZag(in int n) pure nothrow {
Line 1,309 ⟶ 1,670:
void main() {
writefln("%(%(%2d %)\n%)", 5.zigZag);
}</langsyntaxhighlight>
 
=={{header|E}}==
Line 1,317 ⟶ 1,678:
 
Then the code. {{trans|D}}
<langsyntaxhighlight lang="e">def zigZag(n) {
def move(&i, &j) {
if (j < (n - 1)) {
Line 1,340 ⟶ 1,701:
}
return array
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
type TMatrix = array of array of double;
 
 
 
procedure DisplayMatrix(Memo: TMemo; Mat: TMatrix);
{Display specified matrix}
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=0 to High(Mat[0]) do
begin
S:=S+'[';
for X:=0 to High(Mat) do
S:=S+Format('%4.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
procedure ZigzagMatrix(Memo: TMemo);
var Mat: TMatrix;
var X,Y,Inx,Dir: integer;
const Size = 10;
 
procedure Toggle(var I: integer);
{Toggle Direction and increment I}
begin
Dir:=-Dir;
Inc(I);
end;
 
 
procedure Step(var X,Y: integer);
{Take one step "Dir" direction}
begin
X:=X+Dir;
Y:=Y-Dir;
end;
 
begin
SetLength(Mat,Size,Size);
Inx:=0; X:=0; Y:=0; Dir:=1;
repeat
begin
Mat[X,Y]:=Inx;
if (X+Dir)>=Size then Toggle(Y)
else if (Y-Dir)>=Size then Toggle(X)
else if (X+Dir)<0 then Toggle(Y)
else if (Y-Dir)<0 then Toggle(X)
else Step(X,Y);
Inc(Inx);
end
until Inx>=Size*Size;
DisplayMatrix(Memo,Mat);
end;
</syntaxhighlight>
{{out}}
<pre>
[ 0 1 5 6 14 15 27 28 44 45]
[ 2 4 7 13 16 26 29 43 46 63]
[ 3 8 12 17 25 30 42 47 62 64]
[ 9 11 18 24 31 41 48 61 65 78]
[ 10 19 23 32 40 49 60 66 77 79]
[ 20 22 33 39 50 59 67 76 80 89]
[ 21 34 38 51 58 68 75 81 88 90]
[ 35 37 52 57 69 74 82 87 91 96]
[ 36 53 56 70 73 83 86 92 95 97]
[ 54 55 71 72 84 85 93 94 98 99]
 
 
Elapsed Time: 1.576 ms.
 
</pre>
 
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 5.0:
<syntaxhighlight lang="elena">import extensions;
extension op : IntNumber
{
zigzagMatrix()
{
auto result := IntMatrix.allocate(self, self);
int i := 0;
int j := 0;
int d := -1;
int start := 0;
int end := self*self - 1;
while (start < end)
{
result.setAt(i, j, start); start += 1;
result.setAt(self - i - 1, self - j - 1, end); end -= 1;
i := i + d;
j := j - d;
if (i < 0)
{
i:=i+1; d := d.Negative
}
else if (j < 0)
{
j := j + 1; d := d.Negative
}
};
if (start == end)
{
result.setAt(i, j, start)
};
^ result
}
}
public program()
{
console.printLine(5.zigzagMatrix()).readChar()
}</syntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule RC do
require Integer
def zigzag(n) do
Line 1,357 ⟶ 1,850:
end
 
RC.zigzag(5)</langsyntaxhighlight>
 
{{out}}
Line 1,366 ⟶ 1,859:
9 11 17 20 22
10 18 19 23 24
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun zigzag = List by int n
List matrix = List[].with(n)
for int y = 0; y < n; y++ do matrix[y] = int[].with(n) end
int y, x = 1
for int value = 0; value < n * n; value++
matrix[y - 1][x - 1] = value
if (y + x) % 2 == 0
if x < n do x++
else do y += 2 end
if y > 1 do y-- end
else
if y < n do y++
else do x += 2 end
if x > 1 do x-- end
end
end
return matrix
end
fun dump = void by List matrix
int max = length(text!(matrix.length ** 2)) + 1
for each List row in matrix
for each int value in row
write(" " * (max - length(text!value)) + value)
end
writeLine()
end
end
dump(zigzag(5))
writeLine()
dump(zigzag(10))
</syntaxhighlight>
{{out}}
<pre>
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
 
0 1 5 6 14 15 27 28 44 45
2 4 7 13 16 26 29 43 46 63
3 8 12 17 25 30 42 47 62 64
9 11 18 24 31 41 48 61 65 78
10 19 23 32 40 49 60 66 77 79
20 22 33 39 50 59 67 76 80 89
21 34 38 51 58 68 75 81 88 90
35 37 52 57 69 74 82 87 91 96
36 53 56 70 73 83 86 92 95 97
54 55 71 72 84 85 93 94 98 99
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( zigzag ).
 
Line 1,397 ⟶ 1,943:
next_indexes( {0, Y}, _Max ) when Y rem 2 =:= 1 -> {0, Y + 1};
next_indexes( {X, Y}, _Max ) when (X + Y) rem 2 =:= 0 -> {X + 1, Y - 1};
next_indexes( {X, Y}, _Max ) when (X + Y) rem 2 =:= 1 -> {X - 1, Y + 1}.</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,408 ⟶ 1,953:
[10,18,19,23,24]]
</pre>
 
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM ZIG_ZAG
 
!$DYNAMIC
Line 1,439 ⟶ 1,983:
PRINT
END FOR
END PROGRAM</langsyntaxhighlight>
{{out}}
<pre> 0 1 5 6 14
Line 1,449 ⟶ 1,993:
=={{header|Euphoria}}==
{{trans|C#}}
<langsyntaxhighlight Euphorialang="euphoria">function zigzag(integer size)
sequence s
integer i, j, d, max
Line 1,468 ⟶ 2,012:
end function
 
? zigzag(5)</langsyntaxhighlight>
{{out}}
{
Line 1,479 ⟶ 2,023:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
//Produce a zig zag matrix - Nigel Galloway: April 7th., 2015
let zz l a =
Line 1,494 ⟶ 2,038:
| _ -> gng (n-1, i+1, g+1, 2)
gng (0, 0, 0, 2)
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang ="fsharp">zz 5 5</langsyntaxhighlight>
<pre>
[[0; 1; 5; 6; 14]
Line 1,504 ⟶ 2,048:
[10; 18; 19; 23; 24]]
</pre>
<syntaxhighlight lang ="fsharp">zz 8 8</langsyntaxhighlight>
<pre>
[[0; 1; 5; 6; 14; 15; 27; 28]
Line 1,516 ⟶ 2,060:
</pre>
Let's try something a little less square man
<syntaxhighlight lang ="fsharp">zz 5 8</langsyntaxhighlight>
<pre>
[[0; 1; 5; 6; 14; 15; 24; 25]
Line 1,523 ⟶ 2,067:
[9; 11; 18; 21; 28; 31; 35; 38]
[10; 19; 20; 29; 30; 36; 37; 39]]
</pre>
 
=={{header|Factor}}==
This version follows the algorithm laid out in the comments of the first JavaScript (ES5) functional example, though it is not exactly a straight translation.
{{works with|Factor|0.99 2019-03-17}}
<syntaxhighlight lang="factor">USING: columns fry kernel make math math.ranges prettyprint
sequences sequences.cords sequences.extras ;
IN: rosetta-code.zig-zag-matrix
 
: [1,b,1] ( n -- seq )
[1,b] dup but-last-slice <reversed> cord-append ;
 
: <reversed-evens> ( seq -- seq' )
[ even? [ <reversed> ] when ] map-index ;
 
: diagonals ( n -- seq )
[ sq <iota> ] [ [1,b,1] ] bi
[ [ cut [ , ] dip ] each ] { } make nip <reversed-evens> ;
 
: zig-zag-matrix ( n -- seq )
[ diagonals ] [ dup ] bi '[
[
dup 0 <column> _ head ,
[ _ < [ rest-slice ] when ] map-index harvest
] until-empty
] { } make ;
 
: zig-zag-demo ( -- ) 5 zig-zag-matrix simple-table. ;
 
MAIN: zig-zag-demo</syntaxhighlight>
{{out}}
<pre>
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
</pre>
The following example is an implementation of a J routine with an [http://rosettacode.org/wiki/Talk:Zig-zag_matrix#reading_the_J_examples excellent walkthrough on the talk page]. Luckily, we can mimic the "classification" step with the composition of 3 existing Factor words: <code>zip-index expand-keys-push-at values</code> and the <code>inverse-permutation</code> word is the same concept as J's grade, so this is fairly succinct.
{{works with|Factor|0.99 2020-01-23}}
<syntaxhighlight lang="factor">USING: assocs assocs.extras grouping io kernel math
math.combinatorics math.matrices prettyprint sequences ;
 
: <zig-zag-matrix> ( n -- matrix )
[
dup [ + ] <matrix-by-indices> concat zip-index
expand-keys-push-at values [ even? [ reverse ] when ]
map-index concat inverse-permutation
] [ group ] bi ;
 
5 <zig-zag-matrix> simple-table.</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Fan}}==
<langsyntaxhighlight Fanlang="fan">using gfx // for Point; convenient x/y wrapper
 
**
Line 1,616 ⟶ 2,218:
print(zag(8))
}
}</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">0 value diag
 
: south diag abs + cell+ ;
Line 1,668 ⟶ 2,270:
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24 ok</langsyntaxhighlight>
 
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM ZIGZAG
IMPLICIT NONE
Line 1,710 ⟶ 2,311:
END DO
END PROGRAM ZIGZAG</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim As Integer n
Line 1,786 ⟶ 2,387:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,803 ⟶ 2,404:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">ZigZag := function(n)
local a, i, j, k;
a := NullMat(n, n);
Line 1,838 ⟶ 2,439:
# [ 3, 8, 12, 16, 21 ],
# [ 9, 11, 17, 20, 22 ],
# [ 10, 18, 19, 23, 24 ] ]</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Groovy}} Edge direct algorithm
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,887 ⟶ 2,489:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,903 ⟶ 2,505:
and calculates the transform onto the grid.
 
<langsyntaxhighlight lang="groovy">def zz = { n ->
grid = new int[n][n]
i = 0
Line 1,913 ⟶ 2,515:
}
grid
}</langsyntaxhighlight>
 
{{out}}
Line 1,929 ⟶ 2,531:
Ported from the Java example
 
<langsyntaxhighlight lang="groovy">def zz = { n->
move = { i, j -> j < n - 1 ? [i <= 0 ? 0 : i-1, j+1] : [i+1, j] }
grid = new int[n][n]
Line 1,939 ⟶ 2,541:
}
grid
}</langsyntaxhighlight>
 
{{out}}
Line 1,954 ⟶ 2,556:
Ported from the Python example with some input from J
 
<langsyntaxhighlight lang="groovy">def zz = { n ->
(0..<n*n).collect { [x:it%n,y:(int)(it/n)] }.sort { c->
[c.x+c.y, (((c.x+c.y)%2) ? c.y : -c.y)]
}.with { l -> l.inject(new int[n][n]) { a, c -> a[c.y][c.x] = l.indexOf(c); a } }
}</langsyntaxhighlight>
 
{{out}}
Line 1,974 ⟶ 2,576:
Computing the array:
 
<langsyntaxhighlight lang="haskell">import Data.Array (Array, array, bounds, range, (!))
import DataText.MonoidPrintf (mappendprintf)
import Data.List (sortBy)
 
compZig :: (xInt,y Int) -> (x'Int,y' Int) =-> compare (x+y) (x'+y')Ordering
compZig (x, y) (x_, y_) = compare (x + y) (x_ + y_) <> go x y
`mappend` if even (x+y) then compare x x'
where
else compare y y'
go x y
| even (x + y) = compare x x_
| otherwise = compare y y_
 
zigZag upper:: =(Int, arrayInt) b-> $ zipArray (sortByInt, compZigInt) (range b))Int
zigZag upper = array b $ zip (sortBy compZig (range b)) [0 ..]
where b = ((0,0),upper)</lang>
b = ((0, 0), upper)</syntaxhighlight>
<tt>compZig</tt> compares coordinates using the order of a zigzag walk:
Line 1,995 ⟶ 2,601:
Displaying the array (not part of the task):
 
<syntaxhighlight lang="haskell">-- format a 2d array of integers neatly
<lang haskell>import Text.Printf (printf)
show2d a =
unlines
[ unwords
[ printf "%3d" (a ! (x, y) :: Integer)
| x <- axis fst ]
| y <- axis snd ]
where
(l, h) = bounds a
axis f = [f l .. f h]
 
main = mapM_ (putStr . ('\n' :) . show2d . zigZag) [(3, 3), (4, 4), (10, 2)]</syntaxhighlight>
-- format a 2d array of integers neatly
show2d a = unlines [unwords [printf "%3d" (a ! (x,y) :: Integer) | x <- axis fst] | y <- axis snd]
where (l, h) = bounds a
axis f = [f l .. f h]
 
main = mapM_ (putStr . show2d . zigZag) [(3,3), (4,4), (10,2)]</lang>
 
 
Or, building a list of lists with mapAccumL:
<langsyntaxhighlight lang="haskell">import Data.ListText (mapAccumLjustifyRight, pack, unpack)
import Data.TextList (justifyRight, pack, unpackmapAccumL)
import Data.Bool (bool)
 
zigZag :: Int -> [[Int]]
zigZag n = horizontalsgo n<*> (diagonals n)
where
go _ [] = []
go n xss = (head <$> edge) : go n (dropWhile null (tail <$> edge) <> rst)
diagonals :: Int -> [[Int]]
diagonals n =
snd $
mapAccumL
(\xs h ->
let (grp, rst) = splitAt h xs
in ( rst
, (if mod h 2 /= 0
then reverse
else id)
grp))
[0 .. (n * n) - 1]
(slope ++ [n] ++ reverse slope)
where
slope(edge, rst) = [1 ..splitAt n - 1]xss
 
horizontalsdiagonals :: Int -> [[Int]] -> [[Int]]
diagonals n =
horizontals n xss =
snd $ mapAccumL go [0 .. (n * n) - 1] (slope <> [n] <> reverse slope)
if not (null xss)
where
then let (edge, rst) = splitAt n xss
slope = [1 .. n - in (head <$> edge) :1]
go xs h = (rst, bool id horizontals nreverse (dropWhile0 null/= (tailmod <$>h edge2) ++ rstgrp)
else []where
(grp, rst) = splitAt h xs
 
main :: IO ()
Line 2,041 ⟶ 2,641:
putStrLn $
unlines $
(concatconcatMap . (unpack <$>)) . (fmap (justifyRight 3 ' ' . pack . show) <$>) zigZag 5<$/syntaxhighlight>
zigZag 5</lang>
 
{{Out}}
<pre> 0 1 5 6 14
Line 2,054 ⟶ 2,652:
This solution works for both Icon and Unicon.
 
<langsyntaxhighlight lang="icon">procedure main(args)
n := integer(!args) | 5
every !(A := list(n)) := list(n)
Line 2,078 ⟶ 2,676:
then if x[2] = n then [x[1]+1, x[2]] else [max(1, x[1]-1), x[2]+1]
else if x[1] = n then [x[1], x[2]+1] else [x[1]+1, max(1, x[2]-1)]
end</langsyntaxhighlight>
 
{{out}}
Line 2,089 ⟶ 2,687:
->
</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">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</syntaxhighlight>
 
=={{header|J}}==
 
A succinct way:
<langsyntaxhighlight lang="j"> ($ [: /:@; <@|.`</.@i.)@,~ 5
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</langsyntaxhighlight>
 
This version is longer, but more "mathematical" and less "procedural":
<langsyntaxhighlight lang="j"> ($ [: /:@; [: <@(A.~_2|#)/. i.)@,~ 5
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</langsyntaxhighlight>
 
Leveraging a [[Talk:Zig Zag#reading_the_J_examples|useful relationship among the indices]]:
<langsyntaxhighlight lang="j"> ($ ([: /:@;@(+/"1 <@|.`</. ]) (#: i.@(*/))))@,~ 5
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</langsyntaxhighlight>
 
(Also, of course, <code>($ [: /:@; <@|.`</.@i.)@,~0</code> creates a result with 0 rows and 0 columns. And, with an argument of 1, the result has one row and one column with the value <code>0</code>. And, the other expressions behave the same.)
 
By the way, all the edge cases are handled transparently,
without any special checks.
Furthermore, by simply ''removing'' the trailing <tt>@,~</tt>
from the solutions, they automatically generalize
to rectangular (non-square) matrices:
<langsyntaxhighlight lang="j"> ($ [: /:@; [: <@|.`</. i.) 5 3
0 1 5
2 4 6
3 7 11
8 10 12
9 13 14</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Ada}}
<langsyntaxhighlight lang="java">public static int[][] Zig_Zag(final int size)
{
int[][] data = new int[size][size];
Line 2,160 ⟶ 2,788:
}
return data;
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 2,171 ⟶ 2,799:
 
Subclasses the Matrix class defined at [[Matrix Transpose#JavaScript]]
<langsyntaxhighlight lang="javascript">function ZigZagMatrix(n) {
this.height = n;
this.width = n;
Line 2,202 ⟶ 2,830:
 
z = new ZigZagMatrix(4);
print(z);</langsyntaxhighlight>
{{out}}
<pre>0,1,5,6,14
Line 2,219 ⟶ 2,847:
====ES5====
 
<langsyntaxhighlight JavaScriptlang="javascript">(function (n) {
 
// Read range of values into a series of 'diagonal rows'
Line 2,315 ⟶ 2,943:
}));
 
})(5);</langsyntaxhighlight>
 
{{Out}}
 
<lang JavaScriptpre>[[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]]</langpre>
 
====ES6====
 
<langsyntaxhighlight JavaScriptlang="javascript">(n => {
 
// diagonals :: n -> [[n]]
Line 2,386 ⟶ 3,014:
);
 
})(5);</langsyntaxhighlight>
 
{{Out}}
<lang JavaScriptpre>[[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]]</langpre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">
<lang Joy>
(*
From the library.
Line 2,447 ⟶ 3,075:
concat [step '\n putch] cons step.
 
11 zigzag.</langsyntaxhighlight>
 
=={{header|jq}}==
Infrastructure:
<langsyntaxhighlight lang="jq"># Create an m x n matrix
def matrix(m; n; init):
if m == 0 then []
Line 2,469 ⟶ 3,097:
(""; . + reduce range(0;$length) as $j
(""; "\(.) \($in[$i][$j] | right )" ) + "\n" ) ;
</syntaxhighlight>
</lang>
Create a zigzag matrix by zigzagging:
<langsyntaxhighlight lang="jq">def zigzag(n):
 
# unless m == n*n, place m at (i,j), pointing
Line 2,489 ⟶ 3,117:
 
# Example
zigzag(5) | neatly(4)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,498 ⟶ 3,126:
9 11 17 20 22
10 18 19 23 24
</pre>
=== another solution ===
<syntaxhighlight lang="jq">#!/usr/bin/env jq -Mnrc -f
#
# solve zigzag matrix by constructing list of 2n+1 column "runs"
# and then shifting them into final form.
#
# e.g. for n=3 initial runs are [[0],[1,2],[3,4,5],[6,7],[8]]
# runs below are shown as columns:
#
# initial column runs 0 1 3 6 8
# 2 4 7
# 5
#
# reverse cols 0,2,4 0 1 5 6 8
# 2 4 7
# 3
#
# shift cols 3,4 down 0 1 5
# 2 4 6
# 3 7 8
#
# shift rows left 0 1 5
# to get final zigzag 2 4 6
# 3 7 8
 
def N: $n ; # size of matrix
def NR: 2*N - 1; # number of runs
def abs: if .<0 then -. else . end ; # absolute value
def runlen: N-(N-.|abs) ; # length of run
def makeruns: [
foreach range(1;NR+1) as $r ( # for each run
{c:0} # state counter
; .l = ($r|runlen) # length of this run
| .r = [range(.c;.c+.l)] # values in this run
| .c += .l # increment counter
; .r # produce run
) ] ; # collect into array
def even: .%2==0 ; # is input even?
def reverseruns: # reverse alternate runs
.[keys|map(select(even))[]] |= reverse ;
def zeros: [range(.|N-length)|0] ; # array of padding zeros
def shiftdown:
def pad($r): # pad run with zeros
if $r < N # determine where zeros go
then . = . + zeros # at back for left runs
else . = zeros + . # at front for right runs
end ;
reduce keys[] as $r (.;.[$r] |= pad($r)); # shift rows down with pad
def shiftleft: [
range(N) as $r
| [ range($r;$r+N) as $c
| .[$c][$r]
]
] ;
def width: [.[][]]|max|tostring|1+length; # width of largest value
def justify($w): (($w-length)*" ") + . ; # leading spaces
def format:
width as $w # compute width
| map(map(tostring | justify($w)))[] # justify values
| join(" ")
;
makeruns # create column runs
| reverseruns # reverse alternate runs
| shiftdown # shift right runs down
| shiftleft # shift rows left
| format # format final result</syntaxhighlight>
{{out}}
<pre>
$ ./zigzag.jq --argjson n 8
0 1 5 6 14 15 27 28
2 4 7 13 16 26 29 42
3 8 12 17 25 30 41 43
9 11 18 24 31 40 44 53
10 19 23 32 39 45 52 54
20 22 33 38 46 51 55 60
21 34 37 47 50 56 59 61
35 36 48 49 57 58 62 63
</pre>
 
=={{header|Julia}}==
=== simple solution ===
<langsyntaxhighlight Julialang="julia">function zigzag_matrix(n::Int)
matrix = zeros(Int, n, n)
x, y = 1, 1
Line 2,526 ⟶ 3,232:
end
return matrix
end</langsyntaxhighlight>
 
{{out}}
Line 2,542 ⟶ 3,248:
 
'''Zig-Zag Iterator'''
<syntaxhighlight lang="julia">
<lang Julia>
immutable ZigZag
m::Int
Line 2,602 ⟶ 3,308:
return (s, zzs)
end
</syntaxhighlight>
</lang>
 
'''Helper Functions'''
<syntaxhighlight lang="julia">
<lang Julia>
using Formatting
 
Line 2,630 ⟶ 3,336:
return s
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
n = 5
println("The n = ", n, " zig-zag matrix:")
Line 2,660 ⟶ 3,366:
end
println(pretty(a))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,685 ⟶ 3,391:
79 139 149 191 193 223 227
</pre>
 
=={{header|Klingphix}}==
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy
 
 
%Size 5 !Size
0 ( $Size dup ) dim
%i 1 !i %j 1 !j
 
$Size 2 power [
1 -
( $i $j ) set
$i $j + 1 band 0 == (
[$j $Size < ( [$j 1 + !j] [$i 2 + !i] ) if
$i 1 > [ $i 1 - !i] if ]
[$i $Size < ( [$i 1 + !i] [$j 2 + !j] ) if
$j 1 > [ $j 1 - !j] if ]
) if
] for
$Size [
%row !row
$Size [
%col !col
( $row $col ) get tostr 32 32 chain chain 1 3 slice print drop
] for
nl
] for
 
 
nl "End " input</syntaxhighlight>
{{out}}
<pre>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
 
End</pre>
 
=={{header|K}}==
 
'''Works with:''' [[ngnk|ngn/k]]
 
<syntaxhighlight>
f:{grid:+x#<<a,'(!#a)*- 2!a:+/!x,:x
padded:(-#$-1+*/x)$$grid
`0:" "/'padded}
f 5
</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.3
 
typealias Vector = IntArray
typealias Matrix = Array<Vector>
 
fun zigzagMatrix(n: Int): Matrix {
val result = Matrix(n) { Vector(n) }
var down = false
var count = 0
for (col in 0 until n) {
if (down)
for (row in 0..col) result[row][col - row] = count++
else
for (row in col downTo 0) result[row][col - row] = count++
down = !down
}
for (row in 1 until n) {
if (down)
for (col in n - 1 downTo row) result[row + n - 1 - col][col] = count++
else
for (col in row until n) result[row + n - 1 - col][col] = count++
down = !down
}
return result
}
fun printMatrix(m: Matrix) {
for (i in 0 until m.size) {
for (j in 0 until m.size) print("%2d ".format(m[i][j]))
println()
}
println()
}
 
fun main(args: Array<String>) {
printMatrix(zigzagMatrix(5))
printMatrix(zigzagMatrix(10))
}</syntaxhighlight>
 
{{out}}
<pre>
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
 
0 1 5 6 14 15 27 28 44 45
2 4 7 13 16 26 29 43 46 63
3 8 12 17 25 30 42 47 62 64
9 11 18 24 31 41 48 61 65 78
10 19 23 32 40 49 60 66 77 79
20 22 33 39 50 59 67 76 80 89
21 34 38 51 58 68 75 81 88 90
35 37 52 57 69 74 82 87 91 96
36 53 56 70 73 83 86 92 95 97
54 55 71 72 84 85 93 94 98 99
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# Produce a zig-zag array.
 
# # Variables:
#
integer DEF_SIZE=5 # Default size = 5
arr_size=${1:-$DEF_SIZE} # $1 = size, or default
 
# # Externals:
#
 
# # Functions:
#
 
 
######
# main #
######
integer i j n
typeset -a zzarr
 
for (( i=n=0; i<arr_size*2; i++ )); do
for (( j= (i<arr_size) ? 0 : i-arr_size+1; j<=i && j<arr_size; j++ )); do
(( zzarr[(i&1) ? j*(arr_size-1)+i : (i-j)*arr_size+j] = n++ ))
done
done
 
for ((i=0; i<arr_size*arr_size; i++)); do
printf "%3d " ${zzarr[i]}
(( (i+1)%arr_size == 0 )) && printf "\n"
done</syntaxhighlight>
{{out}}<pre>
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
 
0 1 5 6 14 15 27 28 44
2 4 7 13 16 26 29 43 45
3 8 12 17 25 30 42 46 59
9 11 18 24 31 41 47 58 60
10 19 23 32 40 48 57 61 70
20 22 33 39 49 56 62 69 71
21 34 38 50 55 63 68 72 77
35 37 51 54 64 67 73 76 78
36 52 53 65 66 74 75 79 80</pre>
 
=={{header|Lasso}}==
 
<langsyntaxhighlight lang="lasso">
var(
'square' = array
Line 2,764 ⟶ 3,641:
 
$square;
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
local zigzag = {}
 
Line 2,844 ⟶ 3,721:
 
print(zigzag.new(5))
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
Module Lib1 {
Module Global PrintArray(&Ar()) {
if dimension(Ar())<>2 then Error "This is for 2D arrays"
integer i, j, n=dimension(Ar(),1), n1=dimension(Ar(),2)
for i=1 to n
for j=1 to n1
print Ar(i, j),
next
print
next
}
Function Global MakeArray(n as integer=5) {
dim a(1 to n, 1 to n) as integer=0
integer i=1, j=1, z, t1=1
boolean ch=true
for z=0 to n*n-1
if ch then a(i,j)=z else a(j,i)=z
j++
if j>t1 then t1++: j=1:i=t1: ch~ else i--
if i<1 then i=t1 else.if i>n then i=n: j++
if j>n then j=i+2: i=n:ch~
next
=a() // return array (as a pointer)
}
}
Module Zig_Zag_Matrix (n as integer=5) {
Pen 15 {Report "matrix "+n+"x"+n}
integer old_column=tab
Print $(,4) // set column to 4 chars
if random(1,2)=2 then
dim ret()
ret()=makeArray(n) // this get a copy
else
object a=makeArray(n) // but this get the copy of pointer
link a to ret() // ret() is reference to a, to array
end if
PrintArray &ret()
Print $(,old_column)
}
Inline Code Lib1 // just execute the code from module lib1 like was here
Form 60, 36 \\ console 60x36 characters
Report 2, "Zig-zag matrix" // 2 for center
Pen 14 {Zig_Zag_Matrix 1}
Pen 11 {Zig_Zag_Matrix 2}
Pen 14 {Zig_Zag_Matrix 3}
Pen 11 {Zig_Zag_Matrix 4}
Pen 14 {Zig_Zag_Matrix 5}
Pen 11 {Zig_Zag_Matrix 10}
</syntaxhighlight>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">divert(-1)
 
define(`set2d',`define(`$1[$2][$3]',`$4')')
Line 2,876 ⟶ 3,806:
 
zigzag(`a',5)
show2d(`a',5,5)</langsyntaxhighlight>
 
{{out}}
Line 2,886 ⟶ 3,816:
10 18 19 23 24
</pre>
 
=={{header|Maple}}==
{{trans|Stata}}
 
Here values are starting at 1. Replace <code><v+~1,v+~2></code> with <code><v,v+~1></code> to start at 0.
 
<syntaxhighlight lang="maple">zigzag1:=proc(n)
uses ArrayTools;
local i,u,v,a;
u:=Replicate(<-1,1>,n):
v:=Vector[row](1..n,i->i*(2*i-3)):
v:=Reshape(<v+~1,v+~2>,2*n):
a:=Matrix(n,n):
for i to n do
a[...,i]:=v[i+1..i+n];
v+=u
od:
a
end:
 
zigzag2:=proc(n)
local i,v,a;
a:=zigzag1(n);
v:=Vector(1..n-1,i->i^2);
for i from 2 to n do
a[n+2-i..n,i]-=v[1..i-1]
od;
a
end:</syntaxhighlight>
 
<syntaxhighlight lang="maple">zigzag1(6);</syntaxhighlight>
 
{{out}}
 
<pre>Matrix(6, 6, [[ 1, 2, 6, 7, 15, 16],
[ 3, 5, 8, 14, 17, 27],
[ 4, 9, 13, 18, 26, 31],
[10, 12, 19, 25, 32, 42],
[11, 20, 24, 33, 41, 50],
[21, 23, 34, 40, 51, 61]])</pre>
 
<syntaxhighlight lang="maple">zigzag2(6);</syntaxhighlight>
 
{{out}}
 
<pre>Matrix(6, 6, [[ 1, 2, 6, 7, 15, 16],
[ 3, 5, 8, 14, 17, 26],
[ 4, 9, 13, 18, 25, 27],
[10, 12, 19, 24, 28, 33],
[11, 20, 23, 29, 32, 34],
[21, 22, 30, 31, 35, 36]])</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 2,891 ⟶ 3,872:
using a direct formula.
The lower-right half is then 'mirrored' from the upper-left half.
<langsyntaxhighlight Mathematicalang="mathematica">ZigZag[size_Integer/;size>0]:=Module[{empty=ConstantArray[0,{size,size}]},
empty=ReplacePart[empty,{i_,j_}:>1/2 (i+j)^2-(i+j)/2-i (1-Mod[i+j,2])-j Mod[i+j,2]];
ReplacePart[empty,{i_,j_}/;i+j>size+1:> size^2-tmp[[size-i+1,size-j+1]]-1]
]</langsyntaxhighlight>
Ported from the java-example:
<langsyntaxhighlight Mathematicalang="mathematica">ZigZag2[size_] := Module[{data, i, j, elem},
data = ConstantArray[0, {size, size}];
i = j = 1;
Line 2,910 ⟶ 3,891:
];
data
]</langsyntaxhighlight>
Examples:
<langsyntaxhighlight Mathematicalang="mathematica">ZigZag[5] // MatrixForm
ZigZag2[6] // MatrixForm</langsyntaxhighlight>
gives back:
 
Line 2,947 ⟶ 3,928:
But! It is pretty fast for n < 10000.
 
<langsyntaxhighlight MATLABlang="matlab">function matrix = zigZag(n)
 
%This is very unintiutive. This algorithm parameterizes the
Line 3,012 ⟶ 3,993:
end</langsyntaxhighlight>
 
{{out}}
Line 3,024 ⟶ 4,005:
9 11 17 20 22
10 18 19 23 24</pre>
 
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">zigzag(n) := block([a, i, j],
a: zeromatrix(n, n),
i: 1,
Line 3,048 ⟶ 4,028:
[ 3, 8, 12, 16, 21],
[ 9, 11, 17, 20, 22],
[10, 18, 19, 23, 24]) */</langsyntaxhighlight>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
%Zigzag Matrix. Nigel Galloway, February 3rd., 2020
int: Size;
array [1..Size,1..Size] of var 1..Size*Size: zigzag;
constraint zigzag[1,1]=1 /\ zigzag[Size,Size]=Size*Size;
constraint forall(n in {2*g | g in 1..Size div 2})(zigzag[1,n]=zigzag[1,n-1]+1 /\ forall(g in 2..n)(zigzag[g,n-g+1]=zigzag[g-1,n-g+2]+1));
constraint forall(n in {2*g + ((Size-1) mod 2) | g in 1..(Size-1) div 2})(zigzag[n,Size]=zigzag[n-1,Size]+1 /\ forall(g in 1..Size-n)(zigzag[n+g,Size-g]=zigzag[n+g-1,Size-g+1]+1));
constraint forall(n in {2*g+1 | g in 1..(Size-1) div 2})(zigzag[n,1]=zigzag[n-1,1]+1 /\ forall(g in 2..n)(zigzag[n-g+1,g]=zigzag[n-g+2,g-1]+1));
constraint forall(n in {2*g+((Size) mod 2) | g in 1..(Size-1) div 2})(zigzag[Size,n]=zigzag[Size,n-1]+1 /\ forall(g in 1..Size-n)(zigzag[Size-g,n+g]=zigzag[Size-g+1,n+g-1]+1));
output [show2d(zigzag)];
</syntaxhighlight>
{out}
<pre>
minizinc -DSize=5 zigzag.mzn
[| 1, 2, 6, 7, 15 |
3, 5, 8, 14, 16 |
4, 9, 13, 17, 22 |
10, 12, 18, 21, 23 |
11, 19, 20, 24, 25 |]
----------
 
minizinc -DSize=6 zigzag.mzn
[| 1, 2, 6, 7, 15, 16 |
3, 5, 8, 14, 17, 26 |
4, 9, 13, 18, 25, 27 |
10, 12, 19, 24, 28, 33 |
11, 20, 23, 29, 32, 34 |
21, 22, 30, 31, 35, 36 |]
----------
</pre>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE ZigZag EXPORTS Main;
 
IMPORT IO, Fmt;
Line 3,098 ⟶ 4,110:
BEGIN
Print(Create(5));
END ZigZag.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,110 ⟶ 4,122:
=={{header|NetRexx}}==
{{trans|REXX}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 3,151 ⟶ 4,163:
 
return
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">importfrom algorithm, strutilsimport sort
from strutils import align
 
from sequtils import newSeqWith
type Pos = tuple[x, y: int]
 
templateproc newSeqWith(len:`<` int(a, initb: exprPos): exprbool =
a.x + a.y < b.x + b.y or
var result {.gensym.} = newSeq[type(init)](len)
a.x + a.y == b.x + b.y and (a.x < b.x xor (a.x + a.y) mod 2 == 0)
for i in 0 .. <len:
result[i] = init
proc zigzagMatrix(n: int): auto =
result
var indices = newSeqOfCap[Pos](n*n)
for x in 0 ..< n:
for y in 0 ..< n:
indices.add((x,y))
sort(indices)
result = newSeqWith(n, newSeq[int](n))
for i, p in indices:
result[p.x][p.y] = i
 
proc `$`(m: seq[seq[int]]): string =
let Width = len($m[0][^1]) + 1
result = ""
for r in m:
for c in r:
result.add align($c, 2Width) & " "
result.add "\n"
 
procecho zigzagMatrix(n6): auto =</syntaxhighlight>
result = newSeqWith(n, newSeq[int](n))
 
var indices = newSeq[Pos]()
 
for x in 0 .. <n:
for y in 0 .. <n:
indices.add((x,y))
 
sort(indices, proc(a, b: Pos): int =
result = a.x + a.y - b.x - b.y
if result == 0: result =
(if (a.x + a.y) mod 2 == 0: a.y else: -a.y) -
(if (b.x + b.y) mod 2 == 0: b.y else: -b.y))
 
for i, p in indices:
result[p.x][p.y] = i
 
echo zigzagMatrix(6)</lang>
{{out}}
<pre> 0 1 5 6 14 15
Line 3,198 ⟶ 4,205:
10 19 22 28 31 33
20 21 29 30 34 35</pre>
 
===Direct coord to number===
This calculates the number for each coordinate directly. This allows to create very large zig-zag matrices. Generates the same output as above.
<syntaxhighlight lang="nim">import strutils
 
func sumTo(n: Natural): Natural = n * (n+1) div 2
 
func coord2num(row, col, N: Natural): Natural =
var start, offset: Natural
let diag = col + row
if diag < N:
start = sumTo(diag)
offset = if diag mod 2 == 0: col else: row
else:
# N * (2*diag+1-N) - sumTo(diag), but with smaller itermediates
start = N*N - sumTo(2*N-1-diag)
offset = N-1 - (if diag mod 2 == 0: row else: col)
start + offset
 
let N = 6
let width = (N*N).`$`.len + 1
for row in 0 ..< N:
for col in 0 ..< N:
stdout.write(coord2num(row, col, N).`$`.align(width))
stdout.write("\n")
</syntaxhighlight>
 
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang="ocaml">
function : native : ZigZag(size : Int) ~ Int[,] {
data := Int->New[size, size];
Line 3,241 ⟶ 4,274:
return data;
}
</syntaxhighlight>
</lang>
 
 
=={{header|OCaml}}==
Line 3,248 ⟶ 4,280:
{{trans|Common Lisp}}
 
<langsyntaxhighlight lang="ocaml">let zigzag n =
(* move takes references and modifies them directly *)
let move i j =
Line 3,266 ⟶ 4,298:
move y x
done;
a</langsyntaxhighlight>
 
=={{header|Octave}}==
{{trans|Stata}}
See the [[Zig-zag matrix#MATLAB|MATLAB solution]],
 
which works perfectly in Octave too.
<syntaxhighlight lang="octave">function a = zigzag1(n)
j = 1:n;
u = repmat([-1; 1], n, 1);
v = j.*(2*j-3);
v = reshape([v; v+1], 2*n, 1);
a = zeros(n, n);
for i = 1:n
a(:, i) = v(i+j);
v += u;
endfor
endfunction
 
function a = zigzag2(n)
a = zigzag1(n);
v = (1:n-1)'.^2;
for i = 2:n
a(n+2-i:n, i) -= v(1:i-1);
endfor
endfunction
 
>> zigzag2(5)
ans =
 
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</syntaxhighlight>
 
Alternate solution, filling pairs of diagonals.
 
<syntaxhighlight lang="octave">function a = zigzag3(n)
a = zeros(n, n);
for k=1:n
d = (2*(j = mod(k, 2))-1)*(n-1);
m = (n-1)*(k-1);
a(k+(1-j)*m:d:k+j*m) = k*(k-1)/2:k*(k+1)/2-1;
a(n*(n+1-k)+(1-j)*m:d:n*(n+1-k)+j*m) = n*n-k*(k+1)/2:n*n-k*(k-1)/2-1;
endfor
endfunction
 
>> zigzag3(5)
ans =
 
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</syntaxhighlight>
 
==Inspired by Rascal==
<syntaxhighlight lang="octave">
#{
Produce a zigzag matrix. Nigel Galloway, January 26th., 2020.
At the time of writing the Rascal solution is yellow flagged for producing a striped matrix.
Let me make the same faux pas.
#}
n=5; g=1;
for e=1:n i=1; for l=e:-1:1 zig(i++,l)=g++; endfor endfor
for e=2:n i=e; for l=n:-1:e zig(i++,l)=g++; endfor endfor
#{
I then have the following, let me call it zig.
1 2 4 7 11
3 5 8 12 16
6 9 13 17 20
10 14 18 21 23
15 19 22 24 25
To avoid being yellow flagged I must convert this striped matrix into a zigzag matrix.
#}
zag=zig'
#{
So zag is the transpose of zig.
1 3 6 10 15
2 5 9 14 19
4 8 13 18 22
7 12 17 21 24
11 16 20 23 25
#}
for e=1:n for g=1:n if(mod(e+g,2))==0 zagM(e,g)=1; endif endfor endfor; zigM=1-zagM;
#{
I now have 2 masks:
zigM =
 
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
 
zagM =
 
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
#}
zigzag=zag.*zagM+zig.*zigM;
#{
zigzag =
 
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
#}
</syntaxhighlight>
 
=={{header|ooRexx}}==
{{trans|Java}}
<syntaxhighlight lang="oorexx">
<lang ooRexx>
call printArray zigzag(3)
say
Line 3,317 ⟶ 4,459:
say line
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,338 ⟶ 4,480:
=={{header|Oz}}==
Implemented as a state machine:
<langsyntaxhighlight lang="oz">declare
%% state move success failure
States = unit(right: [ 1# 0 downLeft downInstead]
Line 3,376 ⟶ 4,518:
end
in
{Inspect {CreateZigZag 5}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
{{trans|C.23}}
<langsyntaxhighlight lang="parigp">zz(n)={
my(M=matrix(n,n),i,j,d=-1,start,end=n^2-1);
while(ct--,
Line 3,400 ⟶ 4,542:
if(start>end,return(M))
)
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight Pascallang="pascal">Program zigzag( input, output );
 
const
Line 3,469 ⟶ 4,611:
writeln;
end;
end.</langsyntaxhighlight>
 
{{out}}
Line 3,494 ⟶ 4,636:
{{trans|Seed7}} <br>
 
<syntaxhighlight lang="pascal">
<lang Pascal>
Program zigzag;
{$APPTYPE CONSOLE}
Line 3,535 ⟶ 4,677:
 
end.
</syntaxhighlight>
</lang>
 
{{out}} Size 5
Line 3,559 ⟶ 4,701:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use 5.010;
 
sub zig_zag {
Line 3,583 ⟶ 4,725:
my @zig_zag_matrix = zig_zag(5);
say join "\t", @{$_} foreach @zig_zag_matrix;
</syntaxhighlight>
</lang>
 
 
{{trans|Haskell}}
<langsyntaxhighlight lang="perl">sub zig_zag {
my ($w, $h, @r, $n) = @_;
 
Line 3,602 ⟶ 4,744:
}
 
print map{ "@$_\n" } zig_zag(3, 5);</langsyntaxhighlight>
 
=={{header|Perl 6}}==
Assuming the same Turtle class that is used in [[Spiral_matrix]]:
<lang perl6>sub MAIN($size as Int) {
my $t = Turtle.new(dir => 1);
my $counter = 0;
for 1 ..^ $size -> $run {
for ^$run {
$t.lay-egg($counter++);
$t.forward;
}
my $yaw = $run %% 2 ?? -1 !! 1;
$t.turn-right($yaw * 135); $t.forward; $t.turn-right($yaw * 45);
}
for $size ... 1 -> $run {
for ^$run -> $ {
$t.lay-egg($counter++);
$t.forward;
}
$t.turn-left(180); $t.forward;
my $yaw = $run %% 2 ?? 1 !! -1;
$t.turn-right($yaw * 45); $t.forward; $t.turn-left($yaw * 45);
}
$t.showmap;
}</lang>
 
=={{header|Phix}}==
{{Trans|C#}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer n = 9
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer zstart = 0, zend = n*n-1
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">9</span>
--integer zstart = 1, zend = n*n
<span style="color: #004080;">integer</span> <span style="color: #000000;">zstart</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">zend</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
string fmt = sprintf("%%%dd",length(sprintf("%d",zend)))
<span style="color: #000080;font-style:italic;">--integer zstart = 1, zend = n*n</span>
sequence m = repeat(repeat("??",n),n)
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%%dd"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zend</span><span style="color: #0000FF;">)))</span>
integer x = 1, y = 1, d = -1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"??"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
while 1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
m[x][y] = sprintf(fmt,zstart)
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
if zstart=zend then exit end if
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">y</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zstart</span><span style="color: #0000FF;">)</span>
zstart += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">zstart</span><span style="color: #0000FF;">=</span><span style="color: #000000;">zend</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
m[n-x+1][n-y+1] = sprintf(fmt,zend)
<span style="color: #000000;">zstart</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
zend -= 1
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zend</span><span style="color: #0000FF;">)</span>
x += d
<span style="color: #000000;">zend</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
y -= d
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span>
if x<1 then
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">d</span>
x += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">x</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
d = -d
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
elsif y<1 then
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">d</span>
y += 1
<span style="color: #008080;">elsif</span> <span style="color: #000000;">y</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
d = -d
<span style="color: #000000;">y</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">d</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
for i=1 to n do
m[i] = join(m[i])
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
puts(1,join(m,"\n"))</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
Alternative:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer n = 5
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span>
string fmt = sprintf("%%%dd",length(sprintf("%d",n*n-1)))
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%%dd"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)))</span>
sequence m = repeat(repeat("??",n),n)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"??"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
integer x = 1, y = 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
for d=0 to n*n-1 do
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
m[y][x] = sprintf(fmt,d)
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
if mod(x+y,2) then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
{x,y} = iff(y<n?{x-(x>1),y+1}:{x+1,y})
<span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;"><</span><span style="color: #000000;">n</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">})</span>
else
<span style="color: #008080;">else</span>
{x,y} = iff(x<n?{x+1,y-(y>1)}:{x,y+1})
<span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;"><</span><span style="color: #000000;">n</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span><span style="color: #0000FF;">)}:{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">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
for i=1 to n do
m[i]var = join(m[i])row
Size FOR
end for
var col
puts(1,join(m,"\n"))</lang>
row get col get tostr 32 32 chain chain 1 3 slice print drop drop
ENDFOR
nl
ENDFOR</syntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">function ZigZagMatrix($num) {
$matrix = array();
for ($i = 0; $i < $num; $i++){
Line 3,709 ⟶ 4,857:
}
return $matrix;
}</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Line 3,715 ⟶ 4,863:
a two-dimensional structure and is normally used
for simulations and board games.
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/simul.l")
 
(de zigzag (N)
Line 3,734 ⟶ 4,882:
(for This L (prin (align 3 (: val))))
(prinl) )
(zigzag 5) )</langsyntaxhighlight>
{{out}}
<pre> 1 2 6 7 15
Line 3,743 ⟶ 4,891:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">/* Fill a square matrix with the values 0 to N**2-1, */
/* in a zig-zag fashion. */
/* N is the length of one side of the square. */
Line 3,793 ⟶ 4,941:
put skip edit (a(i,*)) (f(4));
end;
end;</langsyntaxhighlight>
{{out}}
<pre> 0 1 5 6 14
Line 3,799 ⟶ 4,947:
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24 </pre>
 
=={{header|PlainTeX}}==
The code works with any etex engine.
<langsyntaxhighlight lang="tex">\long\def\antefi#1#2\fi{#2\fi#1}
\def\fornum#1=#2to#3(#4){%
\edef#1{\number\numexpr#2}\edef\fornumtemp{\noexpand\fornumi\expandafter\noexpand\csname fornum\string#1\endcsname
Line 3,824 ⟶ 4,972:
}
\zzmat{5}
\bye</langsyntaxhighlight>
 
pdf output:
Line 3,839 ⟶ 4,987:
and also draws lines to show the path.
 
<langsyntaxhighlight lang="postscript">%!PS
%%BoundingBox: 0 0 300 200
/size 9 def % defines row * column (9*9 -> 81 numbers,
Line 3,893 ⟶ 5,041:
stroke showpage
} if
%%EOF</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">function zigzag( [int] $n ) {
$zigzag=New-Object 'Object[,]' $n,$n
$nodd = $n -band 1
Line 3,934 ⟶ 5,082:
} )"
}
}</langsyntaxhighlight>
 
===An Alternate Display===
Display the zig-zag matrix using the <code>Format-Wide</code> cmdlet:
<syntaxhighlight lang="powershell">
<lang PowerShell>
zigzag 5 | Format-Wide {"{0,2}" -f $_} -Column 5 -Force
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,951 ⟶ 5,099:
 
=={{header|Prolog}}==
{{Works with|SWiSWI-Prolog}}
<langsyntaxhighlight Prologlang="prolog">zig_zag(N) :-
zig_zag(N, N).
 
Line 4,029 ⟶ 5,177:
print_val(V) :-
writef('%3r ', [V]).
</syntaxhighlight>
</lang>
{{out}}
<pre>?- zig_zag(5).
Line 4,060 ⟶ 5,208:
=={{header|PureBasic}}==
{{trans|AutoHotkey}} <br>
<langsyntaxhighlight lang="purebasic">Procedure zigZag(size)
Protected i, v, x, y
Line 4,112 ⟶ 5,260:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Zig-zag matrix of size 5
Line 4,136 ⟶ 5,284:
There is a full explanation of the algorithm used
by [http://paddy3118.blogspot.com/2008/08/zig-zag.html paddy3118].
{{Works with|Python|3}}
<lang python>def zigzag(n):
<syntaxhighlight lang="python">def zigzag(n):
indexorder = sorted(((x,y) for x in range(n) for y in range(n)),
'''zigzag rows'''
key = lambda (x,y): (x+y, -y if (x+y) % 2 else y) )
def compare(xy):
return {index: n for n,index in enumerate(indexorder)}
x, y = xy
return (x + y, -y if (x + y) % 2 else y)
xs = range(n)
return {index: n for n, index in enumerate(sorted(
((x, y) for x in xs for y in xs),
key=compare
))}
 
 
def printzz(myarray):
'''show zigzag rows as lines'''
n = int(len(myarray)** 0.5 +0.5)
n = int(len(myarray) ** 0.5 + 0.5)
for x in range(n):
xs for y in= range(n):
print "%2i" % myarray[(x,y)],'\n'.join(
[''.join("%3i" % myarray[(x, y)] for x in xs) for y in xs]
print
))
 
 
printzz(zigzag(6))</lang>
printzz(zigzag(6))</syntaxhighlight>
{{out}}
<pre> 0 12 53 69 1410 1520
21 4 78 1311 1619 2521
5 3 87 12 1718 2422 2629
6 13 9 11 1817 23 2728 3230
14 16 24 10 19 22 2827 31 3334
15 25 26 32 20 21 29 30 3433 35</pre>
 
===Alternative version, {{trans|Common Lisp}}===
<syntaxhighlight lang ="python">def zigzag(n):
# pylint: disable=invalid-name
def move(i, j):
# pylint: disable=unused-argument
if j < (n - 1):
"ZigZag iterator."
return max(0, i-1), j+1
import sys
else:
 
return i+1, j
if sys.version_info[0] >= 3:
a = [[0] * n for _ in xrange(n)]
xrange = range
 
def move(x, y, columns, rows):
"Tells us what to do next with x and y."
if y < (rows - 1):
return max(0, x-1), y+1
return x+1, y
 
def zigzag(rows, columns):
"ZigZag iterator, yields indices."
x, y = 0, 0
forsize v= in xrange(nrows * n):columns
for _ in a[y][x] = vxrange(size):
yield y, x
if (x + y) & 1:
x, y = move(x, y, columns, rows)
else:
y, x = move(y, x, rows, columns)
 
return a
# test code
i, rows, cols = 0, 5, 5
mat = [[0 for x in range(cols)] for y in range(rows)]
for (y, x) in zigzag(rows, cols):
mat[y][x], i = i, i + 1
 
from pprint import pprint
pprint(zigzag(5mat))</lang>
</syntaxhighlight>
{{out}}
 
<lang python>[[0, 1, 5, 6, 14],
<syntaxhighlight lang="python">[[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]]</langsyntaxhighlight>
 
===Alternative version, inspired by the Common Lisp Alternative Approach===
<langsyntaxhighlight lang="python">
COLS = 9
def CX(x, ran):
Line 4,201 ⟶ 5,377:
print(repr(next(ran[y])).rjust(3), end = ' ')
print()
</syntaxhighlight>
</lang>
{{out}} COLS = 5 Produces:
<pre>
Line 4,234 ⟶ 5,410:
</pre>
=== Another alternative version ===
<langsyntaxhighlight lang="python">
from __future__ import print_function
 
Line 4,300 ⟶ 5,476:
if __name__ == '__main__':
main(5)
</syntaxhighlight>
</lang>
<pre>
zigzag of 5:
Line 4,310 ⟶ 5,486:
</pre>
 
=={{header|RascalQuackery}}==
{{incorrect|Rascal|Output is striped rather than zig-zag
i.e. your numbers always increase going diagonally down and to the left when it should alternativly increase/decrease.}}
This is a translation of the [[Zig-zag_matrix#Python|Python]] example.
As explained on the [[Talk:Zig-zag_matrix#anti-diagonals|Talk]] page,
the key way to understand a zig-zag matrix is to write down
an example with coordinates:
<lang rascal>0 (0,0), 1 (0,1), 3 (0,2)
2 (1,0), 4 (1,1), 6 (1,2)
5 (2,0), 7 (2,1), 8 (2,2)</lang>
If you order these coordinates on the number, you create the order:
<lang rascal> 0 (0,0), 1 (0,1), 2 (1,0), 3 (0,2), 4 (1,1), 5 (2,0), 6 (1,2), 7 (2,1), 8 (2,2)</lang>
One can observe that this increases with the sum of the coordinates,
and secondly with the the first number of the coordinates.
The Rascal example uses this phenomenon:
<lang rascal>import util::Math;
import List;
import Set;
import IO;
 
===Sorting Indices===
alias cd = tuple[int,int];
 
{{trans|Python:_By_sorting_indices}}
 
public rel[cd, int] zz(int n){
indexorder = sort([<x,y>| x <- [0..n], y <- [0..n]],
bool (cd a, cd b){
if (a[0]+a[1] > b[0]+b[1])
return false;
elseif(a[0] < b[0])
return false;
else
return true;
;
});
return {<indexorder[z] , z> | z <- index(indexorder)};
}
 
<syntaxhighlight lang="quackery"> [ ]'[ tuck do dip do ] is with2 ( x x --> x x )
public void printzz(rel[cd, int] myarray){
 
n = floor(sqrt(size(myarray)));
[ fordup (xtemp <- [0..n-1]){put
for (y <- [0..n-1]){ swap
dup * times [ i^ join ]
print("<myarray[<y,x>]>\t");}
println();}sortwith
[ with2
}</lang>
[ temp share /mod
tuck + 1 &
if negate ]
> ]
sortwith
[ with2
[ temp share /mod + ]
> ]
dup witheach
[ i^ unrot poke ]
[] swap
temp share times
[ temp share split
dip [ nested join ] ]
drop
temp release ] is zigzag ( n --> [ )
 
10 zigzag
witheach
[ witheach
[ dup 10 < if sp
echo sp ]
cr ]</syntaxhighlight>
 
{{out}}
 
<lang rascal>rascal>printzz(zz(4))
<pre> 0 1 5 6 14 15 27 28 44 45
{0} {1} {3} {6} {10}
2 4 7 13 16 26 29 43 46 63
{2} {4} {7} {11} {15}
3 8 12 17 25 30 42 47 62 64
{5} {8} {12} {16} {19}
9 11 18 24 31 41 48 61 65 78
{9} {13} {17} {20} {22}
10 19 23 32 40 49 60 66 77 79
{14} {18} {21} {23} {24}
20 22 33 39 50 59 67 76 80 89
ok</lang>
21 34 38 51 58 68 75 81 88 90
35 37 52 57 69 74 82 87 91 96
36 53 56 70 73 83 86 92 95 97
54 55 71 72 84 85 93 94 98 99
</pre>
 
===Turtle style===
 
Adapted from [[Spiral matrix#Quackery]]
 
The sequence of turns for the first half of the matrix is east to southwest to south to northeast to east. In the second half the order of turns is reversed.
 
<syntaxhighlight lang="quackery"> [ stack ] is stepcount ( --> s )
[ stack ] is position ( --> s )
[ stack ] is heading ( --> s )
 
[ heading take
behead join
heading put ] is turn ( --> )
 
[ heading share 0 peek
unrot times
[ position share
stepcount share
unrot poke
over position tally
1 stepcount tally ]
nip ] is walk ( [ n --> [ )
 
[ dip [ temp put [] ]
temp share times
[ temp share split
dip
[ nested join ] ]
drop temp release ] is matrixify ( n [ --> [ )
 
[ 0 stepcount put ( set up... )
0 position put
' [ 1 ]
over 1 - join
over join
over 1 - negate join
heading put
0 over dup * of
over 1 - times ( turtle draws first half of zigzag )
[ 1 walk turn
i^ 1+ walk turn ]
heading take ( reverse the sequence of turns )
reverse heading put
over 1 - times ( turtle draws second half of zigzag )
[ turn 1 walk
turn i walk ]
1 walk
matrixify ( ...tidy up )
heading release
position release
stepcount release ] is zigzag ( n --> [ )
 
10 zigzag
witheach
[ witheach
[ dup 10 < if sp echo sp ]
cr ]</syntaxhighlight>
 
{{out}}
 
<pre> 0 1 5 6 14 15 27 28 44 45
2 4 7 13 16 26 29 43 46 63
3 8 12 17 25 30 42 47 62 64
9 11 18 24 31 41 48 61 65 78
10 19 23 32 40 49 60 66 77 79
20 22 33 39 50 59 67 76 80 89
21 34 38 51 58 68 75 81 88 90
35 37 52 57 69 74 82 87 91 96
36 53 56 70 73 83 86 92 95 97
54 55 71 72 84 85 93 94 98 99
</pre>
 
=={{header|Qi}}==
Line 4,367 ⟶ 5,619:
The code can probably be simplified somewhat.
 
<syntaxhighlight lang="qi">
<lang qi>
(define odd? A -> (= 1 (MOD A 2)))
(define even? A -> (= 0 (MOD A 2)))
Line 4,393 ⟶ 5,645:
(range 0 N)))
(range 0 N)))
</syntaxhighlight>
</lang>
 
=={{header|R}}==
{{trans|JavaOctave}}
<langsyntaxhighlight Rlang="rsplus">zigzagzigzag1 <- function(sizen) {
j <- seq(n)
{
digitsu <- seq_lenrep(c(size^2) -1, 1), n)
matv <- matrixj * (0,2 nrow* =j size,- ncol=size1) - 1
iv <- as.vector(rbind(v, v + 1))
ja <- 1matrix(0, n, n)
for (elementi in digitsseq(n)) {
a[i, ] <- v[j + i - 1]
{
mat[i,j]v <- elementv + u
}
if((i + j) %% 2 == 0)
{a
# Even stripes
if(j < size) j <- j + 1 else i <- i + 2
if(i > 1) i <- i - 1
} else
{
# Odd stripes
if(i < size) i <- i + 1 else j <- j + 2
if(j > 1) j <- j - 1
}
}
mat
}
 
zigzagzigzag1(5)</langsyntaxhighlight>
 
{{out}}
 
<pre> [,1] [,2] [,3] [,4] [,5]
[1,] 0 1 5 6 14
[2,] 2 4 7 13 16
[3,] 3 8 12 17 25
[4,] 9 11 18 24 31
[5,] 10 19 23 32 40</pre>
 
<syntaxhighlight lang="rsplus">zigzag2 <- function(n) {
a <- zigzag1(n)
v <- seq(n - 1)^2
for (i in seq(n - 1)) {
a[n - i + 1, seq(i + 1, n)] <- a[n - i + 1, seq(i + 1, n)] - v[seq(n - i)]
}
a
}
 
zigzag2(5)</syntaxhighlight>
 
{{out}}
 
<pre> [,1] [,2] [,3] [,4] [,5]
[1,] 0 1 5 6 14
[2,] 2 4 7 13 15
[3,] 3 8 12 16 21
[4,] 9 11 17 20 22
[5,] 10 18 19 23 24</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 4,446 ⟶ 5,716:
 
(zigzag 4)
</syntaxhighlight>
</lang>
{{out}}
<pre>
<lang racket>
'((0 2 3 9)
(1 4 8 10)
(5 7 11 14)
(6 12 13 15))
</langpre>
 
=={{header|REXXRaku}}==
(formerly Perl 6)
<lang rexx>/*REXX program produces and displays a zig─zag matrix (a square array). */
parse arg n start inc . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n=5 /*Not specified? Then use the default.*/
if start=='' | start=="," then start=0 /* " " " " " " */
if inc=='' | inc=="," then inc=1 /* " " " " " " */
row=1; col=1 /*start with the 1st row, 1st column.*/
size=n**2 /*the size of array. */
do j=start by inc for size; @.row.col=j
if (row+col)//2==0 then do; if col<n then col=col+1; else row=row+2
if row\==1 then row=row-1
end
else do; if row<n then row=row+1; else col=col+2
if col\==1 then col=col-1
end
end /*j*/ /* [↑] // is REXX ÷ remainder.*/
 
Using the same Turtle class as in the [[Spiral_matrix#Raku|Spiral matrix]] task:
w=max(length(start), length(start + size*inc) ) /*maximum width of any matrix element. */
 
<syntaxhighlight lang="raku" line>class Turtle {
do r=1 for n ; _= right(@.r.1, w) /*show all the rows of the matrix. */
my @dv = [0,-1], [1,-1], [1,0], [1,1], [0,1], [-1,1], [-1,0], [-1,-1];
do c=2 for n-1; _=_ right(@.r.c, w) /*build a line for the output for a row*/
my $points = 8; # 'compass' points of neighbors on grid: north=0, northeast=1, east=2, etc.
end /*c*/ /* [↑] matrix elements are aligned. */
say _ /*show the matrix row just constructed.*/
has @.loc = 0,0;
end /*r*/ /*stick a fork in it, we're all done. */</lang>
has $.dir = 0;
'''output''' &nbsp; when using the default input of: &nbsp; <tt> 5 </tt>
has %.world;
has $.maxegg;
has $.range-x;
has $.range-y;
method turn-left ($angle = 90) { $!dir -= $angle / 45; $!dir %= $points; }
method turn-right($angle = 90) { $!dir += $angle / 45; $!dir %= $points; }
method lay-egg($egg) {
%!world{~@!loc} = $egg;
$!maxegg max= $egg;
$!range-x minmax= @!loc[0];
$!range-y minmax= @!loc[1];
}
method look($ahead = 1) {
my $there = @!loc »+« @dv[$!dir] »*» $ahead;
%!world{~$there};
}
method forward($ahead = 1) {
my $there = @!loc »+« @dv[$!dir] »*» $ahead;
@!loc = @($there);
}
method showmap() {
my $form = "%{$!maxegg.chars}s";
my $endx = $!range-x.max;
for $!range-y.list X $!range-x.list -> ($y, $x) {
print (%!world{"$x $y"} // '').fmt($form);
print $x == $endx ?? "\n" !! ' ';
}
}
}
 
sub MAIN(Int $size = 5) {
my $t = Turtle.new(dir => 1);
my $counter = 0;
for 1 ..^ $size -> $run {
for ^$run {
$t.lay-egg($counter++);
$t.forward;
}
my $yaw = $run %% 2 ?? -1 !! 1;
$t.turn-right($yaw * 135); $t.forward; $t.turn-right($yaw * 45);
}
for $size ... 1 -> $run {
for ^$run -> $ {
$t.lay-egg($counter++);
$t.forward;
}
$t.turn-left(180); $t.forward;
my $yaw = $run %% 2 ?? 1 !! -1;
$t.turn-right($yaw * 45); $t.forward; $t.turn-left($yaw * 45);
}
$t.showmap;
}</syntaxhighlight>
 
=={{header|Rascal}}==
{{incorrect|Rascal|Output is striped rather than zig-zag
i.e. your numbers always increase going diagonally down and to the left when it should alternativly increase/decrease.}}
This is a translation of the [[Zig-zag_matrix#Python|Python]] example.
As explained on the [[Talk:Zig-zag_matrix#anti-diagonals|Talk]] page,
the key way to understand a zig-zag matrix is to write down
an example with coordinates:
<syntaxhighlight lang="rascal">0 (0,0), 1 (0,1), 3 (0,2)
2 (1,0), 4 (1,1), 6 (1,2)
5 (2,0), 7 (2,1), 8 (2,2)</syntaxhighlight>
If you order these coordinates on the number, you create the order:
<syntaxhighlight lang="rascal"> 0 (0,0), 1 (0,1), 2 (1,0), 3 (0,2), 4 (1,1), 5 (2,0), 6 (1,2), 7 (2,1), 8 (2,2)</syntaxhighlight>
One can observe that this increases with the sum of the coordinates,
and secondly with the the first number of the coordinates.
The Rascal example uses this phenomenon:
<syntaxhighlight lang="rascal">import util::Math;
import List;
import Set;
import IO;
 
alias cd = tuple[int,int];
 
public rel[cd, int] zz(int n){
indexorder = sort([<x,y>| x <- [0..n], y <- [0..n]],
bool (cd a, cd b){
if (a[0]+a[1] > b[0]+b[1])
return false;
elseif(a[0] < b[0])
return false;
else
return true;
;
});
return {<indexorder[z] , z> | z <- index(indexorder)};
}
 
public void printzz(rel[cd, int] myarray){
n = floor(sqrt(size(myarray)));
for (x <- [0..n-1]){
for (y <- [0..n-1]){
print("<myarray[<y,x>]>\t");}
println();}
}</syntaxhighlight>
{{out}}
<pre>rascal>printzz(zz(4))
{0} {1} {3} {6} {10}
{2} {4} {7} {11} {15}
{5} {8} {12} {16} {19}
{9} {13} {17} {20} {22}
{14} {18} {21} {23} {24}
ok</pre>
 
=={{header|REXX}}==
This REXX version allows the optional specification of the &nbsp; '''start''' &nbsp; and &nbsp; '''increment''' &nbsp; values.
===Version 1===
<syntaxhighlight lang="rexx">/*REXX program produces and displays a zig─zag matrix (a square array). */
parse arg n start inc . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 5 /*Not specified? Then use the default.*/
if start=='' | start=="," then start= 0 /* " " " " " " */
if inc=='' | inc=="," then inc= 1 /* " " " " " " */
row= 1; col= 1; size= n**2 /*start: 1st row & column; array size.*/
do j=start by inc for size; @.row.col= j
if (row+col)//2==0 then do; if col<n then col= col+1; else row= row+2
if row\==1 then row= row-1
end
else do; if row<n then row= row+1; else col= col+2
if col\==1 then col= col-1
end
end /*j*/ /* [↑] // is REXX ÷ remainder.*/
call show /*display a (square) matrix──►terminal.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: w= max(length(start), length(start+size*inc)) /*max width of any matrix elements,*/
do r=1 for n ; _= right(@.r.1, w) /*show the rows of the matrix. */
do c=2 for n-1; _= _ right(@.r.c, w) /*build a line for output of a row.*/
end /*c*/; say _ /* [↑] align the matrix elements. */
end /*r*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
0 1 5 6 14
Line 4,486 ⟶ 5,878:
9 11 17 20 22
10 18 19 23 24
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 5 &nbsp; 1 </tt>}}
<pre>
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 4 &nbsp; -1000 &nbsp; -1 </tt>}}
<pre>
-1000 -1001 -1005 -1006
-1002 -1004 -1007 -1012
-1003 -1008 -1011 -1013
-1009 -1010 -1014 -1015
</pre>
 
===Version 2 - simplified logic===
<syntaxhighlight lang="rexx">/*REXX program produces and displays a zig-zag matrix (a square array) */
Parse Arg n start inc . /* obtain optional arguments from command line */
if n=='' | n=="," then n= 5 /*Not specified? use the default*/
if start=='' | start=="," then start= 0 /* " " " " " */
if inc=='' | inc=="," then inc= 1 /* " " " " " */
Parse Value 1 1 n**2 With row col size
Do x=start By inc For size
m.row.col=x
If (row+col)//2=0 Then do /* moving upward */
Select
when row=1 Then Do /* at upper bound */
If col<n Then
col=col+1 /* move right */
Else
row=2 /* move down */
End
when col=n Then /* at right border */
row=row+1 /* move down */
Otherwise Do /* in all other cases */
row=row-1 /* move up */
col=col+1 /* and to the right */
End
End
End
Else Do /* moving downward */
Select
When col=1 Then Do /* at lower bound */
If row=n Then /* in bottom row */
col=2 /* move right */
Else /* otherwise */
row=row+1 /* move down */
End
When row=n Then /* at lower bound */
col=col+1 /* move right */
Otherwise Do /* in all other cases */
row=row+1 /* move down */
col=col-1 /* and to the left */
End
End
End
End
Call show
Exit
/*-----------------------------------------------------------------------*/
show:
w=length(start+size*inc) /* max width of any matrix element */
Do row=1 To n /* loop over rows */
line=right(m.row.1,w) /* first element */
Do column=2 To n /* loop over other elements */
line=line right(m.row.column,w) /* build output line */
End
Say line
End /* display the line */
Return</syntaxhighlight>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project Zig-zag matrix
 
load "guilib.ring"
load "stdlib.ring"
new qapp
{
win1 = new qwidget() {
setwindowtitle("Zig-zag matrix")
setgeometry(100,100,600,400)
n = 5
a = newlist(n,n)
zigzag = newlist(n,n)
for j = 1 to n
for i = 1 to n
a[j][i] = 0
next
next
i = 1
j = 1
k = 1
while k < n * n
a[j][i] = k
k = k + 1
if i = n
j = j + 1
a[j][i] = k
k = k + 1
di = -1
dj = 1
ok
if j = 1
i = i + 1
a[j][i] = k
k = k + 1
di = -1
dj = 1
ok
if j = n
i = i + 1
a[j][i] = k
k = k + 1
di = 1
dj = -1
ok
if i = 1
j = j + 1
a[j][i] = k
k = k + 1
di = 1
dj = -1
ok
i = i + di
j = j + dj
end
for p = 1 to n
for m = 1 to n
zigzag[p][m] = new qpushbutton(win1) {
x = 150+m*40
y = 30 + p*40
setgeometry(x,y,40,40)
settext(string(a[p][m]))
}
next
next
show()
}
exec()
}
</syntaxhighlight>
Output:
 
[http://kepkezelo.com/images/kk86ng7p4gcl7z3p7vo1.jpg Zig-Zag matrix]
 
=={{header|RPL}}==
{{works with|RPL|HP-48}}
Turtle's way.
« 1 -1 → n way val
« n DUP 2 →LIST 0 CON
2 n DUP + '''FOR''' s
n s 1 - MIN s OVER -
'''IF''' way 0 > '''THEN''' SWAP '''END'''
'''FOR''' j
j s OVER - 2 →LIST 'val' INCR PUT
way '''STEP'''
'way' SNEG
'''NEXT'''
» » '<span style="color:blue">ZIGZAG</span>' STO
 
6 <span style="color:blue">ZIGZAG</span>
{{out}}
<pre>
1: [[0 2 3 9 10 20]
[1 4 8 11 19 21]
[5 7 12 18 22 29]
[6 13 17 23 28 30]
[14 16 24 27 31 34]
[15 25 26 32 33 35]]
</pre>
 
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def zigzag(n)
(seq=*0...n).product(seq)
.sort_by {|x,y| [x+y, (x+y).even? ? y : -y]}
Line 4,501 ⟶ 6,065:
end
 
print_matrix zigzag(5)</langsyntaxhighlight>
{{out}}
<pre>
Line 4,509 ⟶ 6,073:
9 11 17 20 22
10 18 19 23 24
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use std::cmp::Ordering;
use std::cmp::Ordering::{Equal, Greater, Less};
use std::iter::repeat;
 
#[derive(Debug, PartialEq, Eq)]
struct SortIndex {
x: usize,
y: usize,
}
 
impl SortIndex {
fn new(x: usize, y: usize) -> SortIndex {
SortIndex { x, y }
}
}
 
impl PartialOrd for SortIndex {
fn partial_cmp(&self, other: &SortIndex) -> Option<Ordering> {
Some(self.cmp(other))
}
}
 
impl Ord for SortIndex {
fn cmp(&self, other: &SortIndex) -> Ordering {
let lower = if self.x + self.y == other.x + other.y {
if (self.x + self.y) % 2 == 0 {
self.x < other.x
} else {
self.y < other.y
}
} else {
(self.x + self.y) < (other.x + other.y)
};
 
if lower {
Less
} else if self == other {
Equal
} else {
Greater
}
}
}
 
fn zigzag(n: usize) -> Vec<Vec<usize>> {
let mut l: Vec<SortIndex> = (0..n * n).map(|i| SortIndex::new(i % n, i / n)).collect();
l.sort();
 
let init_vec = vec![0; n];
let mut result: Vec<Vec<usize>> = repeat(init_vec).take(n).collect();
for (i, &SortIndex { x, y }) in l.iter().enumerate() {
result[y][x] = i
}
result
}
 
fn main() {
println!("{:?}", zigzag(5));
}
 
</syntaxhighlight>
{{out}}
<pre>
[[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]]
</pre>
 
Line 4,514 ⟶ 6,146:
Uses the array indices sort solution used by others here.
 
<langsyntaxhighlight lang="scala"> def zigzag(n:int Int): Array[Array[Int]] = {
val l = for (i <- 0 until n*n) yield (i%n, i/n)
var l = List[Tuple2[int,int]]()
val lSorted = l.sortWith {
(0 until n*n) foreach {i=>l = l + (i%n,i/n)}
l = l.sort{ case ((x,y), (u,v)) => if (x+y == u+v)
if ((x+y) % 2 == 0) x<u else y<+v)
if ((x+y) % 2 == 0) x<u else (x+y) < (u+v) }
else x+y < u+v
var a = new Array[Array[int]](n,n)
}
l.zipWithIndex foreach {case ((x,y),i) => a(y)(x) = i}
val res = Array.ofDim[Int](n, n)
a
lSorted.zipWithIndex foreach {
}</lang>
case ((x,y), i) => res(y)(x) = i
}
res
}
zigzag(5).foreach{
ar => ar.foreach(x => print("%3d".format(x)))
println
}</syntaxhighlight>
Output:
<pre> 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
</pre>
 
=={{header|Scilab}}==
Or, compressed into just one statement
{{trans|Octave}}
 
<syntaxhighlight lang="scilab">function a = zigzag3(n)
<lang scala>def zigzag(n:int) = {
a = zeros(n, n)
var indices = List[Tuple2[Int,Int]]()
for k=1:n
var array = new Array[Array[Int]](n,n)
j = modulo(k, 2)
d = (2*j-1)*(n-1)
m = (n-1)*(k-1)
a(k+(1-j)*m:d:k+j*m) = k*(k-1)/2:k*(k+1)/2-1
a(n*(n+1-k)+(1-j)*m:d:n*(n+1-k)+j*m) = n*n-k*(k+1)/2:n*n-k*(k-1)/2-1
end
endfunction
 
-->zigzag3(5)
(0 until n*n).foldLeft(indices)((l,i) => l + (i%n,i/n)).
ans =
sort{case ((x,y),(u,v)) => if (x+y == u+v)
if ((x+y) % 2 == 0) x<u else y<v
0. 1. 5. 6. 14. else (x+y) < (u+v) }.
2. 4. 7. 13. 15.
zipWithIndex.foldLeft(array) {case (a,((x,y),i)) => a(y)(x) = i; a}
3. 8. 12. 16. 21.
}
9. 11. 17. 20. 22.
</lang>
10. 18. 19. 23. 24.</syntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const type: matrix is array array integer;
Line 4,584 ⟶ 6,241:
writeln;
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,599 ⟶ 6,256:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func zig_zag(w, h) {
 
var r = [];
var n = 0;
 
h.of { |e|
w.of { |f|
[e-1, f-1]
}
}.reduce('+').sort { |a, b|
} \
-> reduce('+') \
-> sort { |a, b|
(a[0]+a[1] <=> b[0]+b[1]) ||
(a[0]+a[1] -> is_even ? a[0]<=>b[0]
: a[1]<=>b[1])
}.each \{ |a|
-> each { |r[a|[1]][a[0]] = n++
r[a[1]][a[0]] = n++;
}
 
return r;
}
 
zig_zag(5, 5).each { say .join('', {|i| "%4i" % i}) };</langsyntaxhighlight>
{{out}}
<pre>
Line 4,631 ⟶ 6,285:
10 18 19 23 24
</pre>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">fun rowprint r = (List.app (fn i => print (StringCvt.padLeft #" " 3 (Int.toString i))) r;
print "\n");
fun zig lst M = List.app rowprint (lst M);
 
fun sign t = if t mod 2 = 0 then ~1 else 1;
 
fun zag n = List.tabulate (n,
fn i=> rev ( List.tabulate (n,
fn j =>
let val t = n-j+i and u = n+j-i in
if i <= j
then t*t div 2 + sign t * ( t div 2 - i )
else n*n - 1 - ( u*u div 2 + sign u * ( u div 2 - n + 1 + i) )
end
)));
 
zig zag 5 ;</syntaxhighlight>
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
val it = () : unit
 
 
 
=={{header|Stata}}==
The requested zig-zag matrix can be constructed as a correction of another zig-zag matrix, which is a square "view" of the infinite zig-zag matrix. Here is the latter:
 
<syntaxhighlight lang="stata">function zigzag1(n) {
j = 0::n-1
u = J(1, n, (-1, 1))
v = (j:*(2:*j:+3))
v = rowshape((v,v:+1), 1)
a = J(n, n, .)
for (i=1; i<=n; i++) {
a[i, .] = v[j:+i]
v = v+u
}
return(a)
}
 
zigzag1(5)
1 2 3 4 5
+--------------------------+
1 | 0 1 5 6 14 |
2 | 2 4 7 13 16 |
3 | 3 8 12 17 25 |
4 | 9 11 18 24 31 |
5 | 10 19 23 32 40 |
+--------------------------+</syntaxhighlight>
 
Now the corrected matrix, which solves the task:
 
<syntaxhighlight lang="stata">function zigzag2(n) {
a = zigzag1(n)
v = (1..n-1):^2
for (i=1; i<n; i++) {
a[n-i+1, i+1..n] = a[n-i+1, i+1..n] - v[1..n-i]
}
return(a)
}
 
zigzag2(5)
1 2 3 4 5
+--------------------------+
1 | 0 1 5 6 14 |
2 | 2 4 7 13 15 |
3 | 3 8 12 16 21 |
4 | 9 11 17 20 22 |
5 | 10 18 19 23 24 |
+--------------------------+</syntaxhighlight>
 
The correction is given by the difference:
 
<syntaxhighlight lang="stata">zigzag1(5)-zigzag2(5)
[symmetric]
1 2 3 4 5
+--------------------------+
1 | 0 |
2 | 0 0 |
3 | 0 0 0 |
4 | 0 0 1 4 |
5 | 0 1 4 9 16 |
+--------------------------+</syntaxhighlight>
 
=={{header|Tcl}}==
Using <code>print_matrix</code> from [[Matrix Transpose#Tcl|Matrix Transpose]]…
<langsyntaxhighlight lang="tcl">proc zigzag {size} {
set m [lrepeat $size [lrepeat $size .]]
set x 0; set dx -1
Line 4,669 ⟶ 6,410:
}
 
print_matrix [zigzag 5]</langsyntaxhighlight>
{{out}}
<pre> 0 1 5 6 14
Line 4,679 ⟶ 6,420:
=={{header|uBasic/4tH}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="ubasic">S = 5
 
i = 1
Line 4,717 ⟶ 6,458:
Next
Print
Next</langsyntaxhighlight>
{{out}}
<pre> 0 1 5 6 14
Line 4,729 ⟶ 6,470:
=={{header|Ursala}}==
adapted from the J solution
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
zigzag = ~&mlPK2xnSS+ num+ ==+sum~~|=xK9xSL@iiK0+ iota</langsyntaxhighlight>
test program (three examples):
<langsyntaxhighlight Ursalalang="ursala">#cast %nLLL
 
tests = zigzag* <4,5,6></langsyntaxhighlight>
{{out}}
<pre><
Line 4,759 ⟶ 6,500:
 
=={{header|VBA}}==
<langsyntaxhighlight lan= VBA>
Public Sub zigzag(n)
Dim a() As Integer
Line 4,819 ⟶ 6,560:
Next
End Sub
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,843 ⟶ 6,584:
=={{header|VBScript}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="vb">ZigZag(Cint(WScript.Arguments(0)))
 
Function ZigZag(n)
Line 4,878 ⟶ 6,619:
WScript.StdOut.WriteLine
Next
End Function</langsyntaxhighlight>
 
{{out}}
Line 4,897 ⟶ 6,638:
20 22 32 35 41 44 46
21 33 34 42 43 47 48
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Conv, Fmt
 
var zigzag = Fn.new { |n|
var r = List.filled(n*n, 0)
var i = 0
var n2 = n * 2
for (d in 1..n2) {
var x = d - n
if (x < 0) x = 0
var y = d - 1
if (y > n - 1) y = n - 1
var j = n2 - d
if (j > d) j = d
for (k in 0...j) {
if (d&1 == 0) {
r[(x+k)*n+y-k] = i
} else {
r[(y-k)*n+x+k] = i
}
i = i + 1
}
}
return r
}
 
var n = 5
var w = Conv.itoa(n*n - 1).count
var i = 0
for (e in zigzag.call(n)) {
Fmt.write("$*d ", w, e)
if (i%n == n - 1) System.print()
i = i + 1
}</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
def N=6;
int A(N,N), X, Y, I, D;
Line 4,922 ⟶ 6,709:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,933 ⟶ 6,720:
20 21 29 30 34 35
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">Size = 5
DIM array(Size-1, Size-1)
 
i = 1
j = 1
FOR e = 0 TO Size^2-1
array(i-1, j-1) = e
IF and((i + j), 1) = 0 THEN
IF j < Size then j = j + 1 ELSE i = i + 2 end if
IF i > 1 i = i - 1
ELSE
IF i < Size then i = i + 1 ELSE j = j + 2 end if
IF j > 1 j = j - 1
ENDIF
NEXT e
 
FOR row = 0 TO Size-1
FOR col = 0 TO Size-1
PRINT array(row,col) USING "##";
NEXT col
PRINT
NEXT row</syntaxhighlight>
 
=={{header|zkl}}==
{{trans|Groovy}}
<langsyntaxhighlight lang="zkl">fcn zz(n){
grid := (0).pump(n,List, (0).pump(n,List).copy).copy();
ri := Ref(0);
Line 4,946 ⟶ 6,757:
}
grid.pump(String,'wrap(r){("%3s"*n+"\n").fmt(r.xplode())});
}</langsyntaxhighlight>
{{trans|C}}
Using list comprehension (and side effects) for the double loop,
the resulting list is just thrown away,
which is easier than creating an enumerated list and sorting.
<langsyntaxhighlight lang="zkl">fcn ceg(m){
s := (0).pump(m*m,List).copy(); // copy to make writable
rn := Ref(0);
Line 4,957 ⟶ 6,768:
'{ s[ if(i.isOdd) j*(m-1)+i else (i-j)*m+j ] = rn.inc(); }]];
s.pump(String,T(Void.Read,m-1), ("%3s"*m+"\n").fmt);
}</langsyntaxhighlight>
To be pedantic, the same as above,
but using the output of the list comprehension:
<langsyntaxhighlight lang="zkl">fcn ceg2(m){
rn := Ref(0);
[[(i,j); [0..m*2-1]; '{[(0).max(i-m+1) .. i.min(m-1)]};
Line 4,966 ⟶ 6,777:
.sort(fcn([(a,_)], [(b,_)]){ a<b }).apply("get",1)
.pump(String,T(Void.Read,m-1), ("%3s"*m+"\n").fmt);
}</langsyntaxhighlight>
{{out}} The results are the same:
<pre>
1,150

edits