a(n) = { my (r = [], d, l, v = 0); while (n, d = n%3; l = 0; while ((n%3)==d, n\=3; l++; ); r = concat(l, r); ); for (k=1, #r, v=(v+k%2)*2^r[k]-k%2); v } { w = 9; vv = vector(2^w-1, n, []); for (n = 1, 3^w-1, v = a(n); vv[v] = concat(vv[v], n); ); print ("0 0"); m = 0; for (n = 1, #vv, for (k = 1, #vv[n], print (m++ " " vv[n][k]); ); ); } quit