s = 0 unseen = 1 seen(v) = bit test(s, v) see(v) = s = bit or(s, 2^v); while (seen(unseen), unseen++) neg(n) = { my (v=0, b=1, t); while (n, t=centerlift(Mod(n, 3)); if (t==-1, v+=b); n=(n-t)\3; b*=2); v } pos(n) = { my (v=0, b=1, t); while (n, t=centerlift(Mod(n, 3)); if (t==+1, v+=b); n=(n-t)\3; b*=2); v } ncache = apply(neg, [0..3^9]) pcache = apply(pos, [0..3^9]) neg(n) = ncache[1+n] pos(n) = pcache[1+n] other(p) = { see(p); my (np=neg(p)); for (v=unseen, oo, if (!seen(v) && bitand(pos(v), np)==np, return (v); ); ); } print (0" "v=0); a = vector(3^8) u = 1 for (n=1, oo, v=other(v); if (v<=#a, a[v]=n; while (a[u], print (u " " a[u]); if (u++>#a, break (2))))) quit