toruns(n) = { my (r=[]); while (n, my (v=valuation(n+n%2, 2)); n\=2^v; r=concat(v, r)); r } fromruns(r) = { my (v=0); for (k=1, #r, v=(v+k%2)*2^r[k]-k%2); v } rev(n) = fromruns(Vecrev(toruns(n))) vv = [0] nb = 0 bin(n) = fromdigits(binary(n)) explore(v, z, o) = { if (z==o, if (nb++ > #vv, vv = concat(vv, vector(#vv)); ); vv[nb] = v; ); if (z-1>=o && v, explore(2*v+0, z-1, o); ); if (o, explore(2*v+1, z, o-1); ); } explore(0, W=9, W) vv = Set(vv[1..nb]) for (n=1, #vv, print (n-1 " " setsearch(vv, rev(vv[n]))-1)) quit