a(n) = { my (r = [], d, l, v = 0); while (n, d = centerlift(Mod(n, 3)); l = 0; while (centerlift(Mod(n, 3))==d, n = (n-d)/3; l++;); r = concat(l, r);); for (k = 1, #r, v = (v+k%2)*2^r[k]-k%2); v } btertxt(n) = { my (s="", d); while (1, d = n%3; s = Str(["0", "1", "T"][1+d], s); n = (n-[0,1,-1][1+d])/3; if (n==0, return (s););); } tobter(n) = { my (dd=[], d); while (n, dd=concat(d = [0,1,-1][1+n%3], dd); n=(n-d)/3;); return (dd); } frombter(d) = fromdigits(d,3) sumbterdigits(n) = { my (s=0, d); while (n, s += d = [0,1,-1][1+n%3]; n=(n-d)/3;); return (s); } { w = 9; vv = vector(2^w-1, n, []); for (n = 1, (3^w-1)/2, v = a(n); vv[v] = concat(vv[v], n); ); a = vector((3^w-1)/2); m = 0; for (n = 1, #vv, for (k = 1, #vv[n], a[vv[n][#vv[n]+1-k]] = vv[n][k]; ); ); print ("0 0"); for (n = 1, #a, print (n " " a[n]); ); } quit