big = 1 000 000 s = 0 S = Set([]) unseen = 1 seen(v) = if (v < big, bit test(s, v), set search(S, v)) see(v) = if (v < big, s = bit or(s, 2^v), S = set union(S, Set([v]))); while (seen(unseen), unseen++) base = 3 other(p) = { see(p); my (d=Vecrev(digits(p, base))); \\ alter leftmost digit (no a leading zero) forstep (x=#d-1, 0, -1, for (t=0, base-1, my (m=p+(t-d[x+1])*base^x); if (!seen(m), return (m); ); ); ); \\ alter rightmost leading zero for (x=#d, oo, for (t=0, base-1, my (m=p+t*base^x); if (!seen(m), return (m); ); ); ); } v=0; for (n=0, base^logint(10 000, base)-1, print (n " " v); v=other(v)) quit