\\ move forward move(z) = { my (x=real(z), y=imag(z)); forstep (k=1+max(#binary(x), #binary(y)), 2, -1, my (w=2^k, m=(1+I)*2^(k-1), zz=x%w + y%w*I); if (zz==m, return (z-I), zz==m-1-I, return (z+I), zz==m-I, return (z-1), zz==m+1-I, return (z+I), zz==m-2, return (z-I), zz==m-1-2*I, return (z+1) ); ); if (y%2==0, if (x%2==0, z+1, z+I), if (x%2==0, z-I, z-1) ); } \\ move backward moveb(z) = { my (x=real(z), y=imag(z)); forstep (k=1+max(#binary(x), #binary(y)), 2, -1, my (w=2^k, m=(1+I)*2^(k-1), zz=x%w + y%w*I); if (zz==m-I, return (z+I), zz==m-1-I, return (z+1), zz==m-1, return (z-I), zz==m-2-I, return (z+I), zz==m-2*I, return (z-1), zz==m+1, return (z-I) ); ); if (y%2==0, if (x%2==0, z+I, z-1), if (x%2==0, z+1, z-I) ); } T(x,y) = { my (z=x+I*y, b=0, f=0); for (k=0, oo, if (z==b, return (-k), z==f, return (+k), b=moveb(b), f=move(f)) ); } M = matrix(100, 100) rem = #M^2 { M[1,1] = 0; rem--; f = b = 0; for (k=1, oo, f = move(f); if (max(real(f), imag(f))<#M, M[1+real(f), 1+imag(f)] = +k; rem--; ); b = moveb(b); if (max(real(b), imag(b))<#M, M[1+real(b), 1+imag(b)] = -k; rem--; ); if (rem==0, for (d=1, #M, for (k=1, d, print (i++-1 " " M[d+1-k, k]); ); ); break; ); ); } quit