Zig-zag matrix: Difference between revisions

Content added Content deleted
(Added Wren)
(+Maple)
Line 3,267: Line 3,267:
10 18 19 23 24
10 18 19 23 24
</pre>
</pre>

=={{header|Maple}}==
{{trans|Stata}}

<lang maple>zigzag1:=proc(n)
uses ArrayTools;
local i,j,u,v,a;
j:=Vector[row](1..n,i->i):
u:=Replicate(<-1,1>,n):
v:=j*~(2*j-~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:</lang>

<lang maple>zigzag1(6);</lang>

{{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>

<lang maple>zigzag2(6);</lang>

{{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}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==