June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,22 +1,25 @@
// Spiral Matrix
n=10
mat=zeros(n,n);
botcol=1; topcol=n
botrow=1; toprow=n
ndir=0; col=1; row=1;
for i=0:n*n-1
mat(row,col)=i;
if ndir==0
if col<topcol then col=col+1; else ndir=1, row=row+1; botrow=botrow+1; end
elseif ndir==1
if row<toprow then row=row+1; else ndir=2, col=col-1; topcol=topcol-1; end
elseif ndir==2
if col>botcol then col=col-1; else ndir=3, row=row-1; toprow=toprow-1; end
elseif ndir==3
if row>botrow then row=row-1; else ndir=0, col=col+1; botcol=botcol+1; end
end
end i
printf("n=%4d\n",n);
for i=1:n;
for j=1:n; printf("%4d",mat(i,j)); end j; printf("\n");
end i;
function a = spiral(n)
a = ones(n*n, 1)
v = ones(n, 1)
u = -n*v;
i = n
for k = n-1:-1:1
j = 1:k
u(j) = -u(j)
a(j+i) = u(j)
v(j) = -v(j)
a(j+(i+k)) = v(j)
i = i+2*k
end
a(cumsum(a)) = (1:n*n)'
a = matrix(a, n, n)'-1
endfunction
-->spiral(5)
ans =
0. 1. 2. 3. 4.
15. 16. 17. 18. 5.
14. 23. 24. 19. 6.
13. 22. 21. 20. 7.
12. 11. 10. 9. 8.