A(n, k) = fromdigits(binary(n), 3) + fromdigits(binary(k), 3) nb(n) = my (t = digits(n, 3)); 2^sum(k=1, #t, t[k]==1) { T = vector((3^7-1)/2, n, vector(nb(n))); occ = vector(#T); u = 1; print ("0 0"); m = 0; i = 0; for (d = 1, oo, for (k = 0, d, m++; v = A(d-k, k); if (v <= #T, T[v][occ[v]++] = m; while (occ[u]==nb(u), apply (w -> print (i++ " " w), T[u]); if (u++ > #T, break (3); ); ); ); ); ); } quit