\\ move to the next location alongside a square spiral move(z) = { my (w=max(abs(real(z)), abs(imag(z)))); if (imag(z)==-w, z+1, real(z)==-w, z-I, imag(z)==+w, z-1, z+I); } \\ index of location on square spiral index(z) = { my (x=real(z), y=imag(z)); if (x>abs(y), ((2*x-1)^2) + (x+y-1), x>y, 2*-y*(1-2*y) + (x-y), y>=abs(x), 2*y*(2*y-1) + (y-x), (4*x^2) + (-y-x) ) + 1; } \\ see / seen functions big = 1 000 000 s = 0 S = [] unseen = 1 seen(v) = if (v < big, bittest(s, v), setsearch(S, v)) see(v) = if (v < big, s = bitor(s, 2^v), S = setunion(S, [v])); while (seen(unseen), unseen++) \\ knight moves dz = powers(I, 3) dz = concat(dz * (2+I), dz * (2-I)) { \\ A361377 Squares visited by a knight moving on a spirally numbered board always to the lowest unvisited coprime square. z = 0; for (n=1, 10 000, see(i = index(z)); print (n " " i); j = oo; for (k=1, #dz, zk = z + dz[k]; jj = index(zk); if (jj < j && !seen(jj) && gcd(i, jj)==1, j = jj; zz = zk; ); ); if (j==oo, break, z = zz; ); ); }