base = 10 cur = nxt = [1..base-1] other(p) = { my (h = digits(p, base)[1]); if (digits(cur[h]++)[1] != h, cur[h] = nxt[h] *= base; ); my (v = oo); for (d = 1, base-1, if (d!=h, v = min(v, cur[d]); ); ); return (v); } for (n = 1, 10 000, print (n " " v = if (n==1, 1, other(v)))) quit