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,27 +1,21 @@
function rs = runsum(v)
for i = 1:numel(v)
rs(i) = sum(v(1:i));
function a = spiral(n)
a = ones(n*n, 1);
u = -(i = n) * (v = ones(n, 1));
for k = n-1:-1:1
j = 1:k;
a(j+i) = u(j) = -u(j);
a(j+(i+k)) = v(j) = -v(j);
i += 2*k;
endfor
a(cumsum(a)) = 1:n*n;
a = reshape(a, n, n)'-1;
endfunction
function g = grade(v)
for i = 1:numel(v)
g(v(i)+1) = i-1;
endfor
endfunction
>> spiral(5)
ans =
function spiral = make_spiral(spirald)
series = ones(1,spirald^2);
l = spirald-1; p = spirald+1;
s = 1;
while(l>0)
series(p:p+l-1) *= spirald*s;
series(p+l:p+l*2-1) *= -s;
p += l*2;
l--; s *= -1;
endwhile
series(1) = 0;
spiral = reshape(grade(runsum(series)), spirald, spirald)';
endfunction
make_spiral(5)
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