allocate mem(2^30) s = 0 unseen = 1 seen(v) = bittest(s, v) see(v) = s = bitor(s, 2^v); while (seen(unseen), unseen++) row0(n, base = 10) = { my (r = [], d = digits(n, base)); for (i = 1, #d, if (d[i], for (j = i, #d, my (s = fromdigits(d[i..j], base)); if (!setsearch(r, s), r = setunion(r, divisors(s)); ); ); ); ); return (r); } cache = Map() row(n) = { my (r); if (!mapisdefined(cache, n, &r), mapput(cache, n, r = row0(n)) ); return (r); } ok(x, y) = setsearch(row(max(x, y)), min(x, y)) != 0 other(p) = { see(p); my (r = row(p)); for (i = 1, #r, if (!seen(r[i]), return (r[i]); ); ); forstep (m = p, oo, p, if (#digits(p) < #digits(m), break, !seen(m), return (m); ); ); for (v = 10^#digits(p), oo, if (!seen(v) && ok(p, v), return (v); ); ); } { for (n = 1, 10 000, print (n " " v = if (n==1, unseen, other(v))); ); } quit