\\ A003188 Decimal equivalent of Gray code for n. A003188(n)=bitxor(n, n>>1) \\ A006068 a(n) is Gray-coded into n. /* the following routine needs only O(log_2(n)) operations */ A006068(n)= { my( s=1, ns ); while ( 1, ns = n >> s; if ( 0==ns, break() ); n = bitxor(n, ns); s <<= 1; ); return ( n ); } /* Joerg Arndt, Jul 19 2012 */ \\ g = f^-1 f = A006068 g = A003188 \\ neighbors of number n \\ k -- k+1 \\ f(k) -- f(k+1) neighbors(n) = { my (w=[]); if (n>0, w = concat(w, n-1); ); w = concat(w, n+1); my (m=g(n)); if (m>0, w = concat(w, f(m-1)); ); w = concat(w, f(m+1)); w } T(n,k) = { my (w=[n], p=[]); for (d=0, oo, if (setsearch(w,k), return (d), p = setunion(p, w); w = setminus(Set(concat(apply(neighbors, w))), p); ); ); } for (d=0, 100, for (k=0, d, print (n++ " " T(d-k, k)))) quit