big limit = 100 000 big values = Set([]) small values = vector(4, i, []) see index(z) = { my (s=1, x=real(z), y=imag(z)); if (x<1, s += 1; x = 1-x; ); if (y<0, s += 2; y = -y; ); return ([s,x,y]); } see(z) = { my (index = see index(z)); if (max(index[2],index[3]) > big limit, big values = set union(big values, Set([z])), if (#small values[index[1]] < index[2], small values[index[1]] = concat(small values[index[1]], vector(index[2] - #small values[index[1]])) ); small values[index[1]][index[2]] = bit or(small values[index[1]][index[2]], 2^index[3]) ); } seen(z) = { my (index = see index(z)); if (max(index[2],index[3]) > big limit, return (set search(big values, z)), if (#small values[index[1]] < index[2], return (0), return (bit test(small values[index[1]][index[2]], index[3])) ); ); } neigbbours(z) = if (real(z)%2==imag(z)%2, [z-1, z+1, z-I], [z-1, z+1, z+I]) new neighbours(z) = { select(zz -> !seen(zz) && #select(seen, neigbbours(zz))==1, neigbbours(z)) } allocate mem(2^30) c = Set(0); nb = 0; { for (n=0, 10 000, print (n " " nb); nb += #c; for (i=1, #c, see(c[i]); ); c = Set(concat(apply(new neighbours, c))) ); } quit