allocate mem(2^30) kk = [0] nb = 0 mx = 189 \\ in order to have ~10000 terms emit(k) = { if (#digits(k) <= mx, if (nb++ > #kk, kk = concat(kk, vector(#kk)); ); kk[nb] = k; ); } { emit(1); emit(2); for (n = 2, oo, if (n > nb, break, r = digits(kk[n]); for (d0 = 1, 2, w = 0; d = d0; for (i = 1, #r, for (j = 1, r[i], w = 10*w+d; ); d = [2,1][d]; ); emit(w); ); ); ); kk = Set(kk[1..nb]); for (n = 1, #kk, print (n " " kk[n]); ); } quit