seen = 0
unseen = 1

K = 5

other(p) = my (v=unseen); while (bittest(seen, v) || #factor(p+v)~<K, v++); return (v)

for (n=1, 10 000, a = if(n==1, 1, other(a)); seen += 2^a; while (bittest(seen, unseen), unseen++); print (n " " a))

quit