s = 0 unseen = 1 seen(v) = bit test(s, v) see(v) = s = bit or(s, 2^v); while (seen(unseen), unseen++) { a = vector(10 000); c = vector(#a); print(1 " " c[1]=a[1]=v=1); see(a[1]); dd=select(isprime, [1..9]); for (n=2, #a, for (i=1, oo, for (j=1, #dd, for (w=dd[j]*10^i, (dd[j]+1)*10^i-1, if (w%10 && !seen(w) && !isprime(w), d=digits(w); if (setsearch(dd, d[1]) && !seen(c[n-1]-(c[n-1]%10)+d[1]), tt=[]; h=d[1]; d[1] = a[n-1]%10; for (t=1, 9, d[#d]=t; m=fromdigits(d); if (!seen(m) && isprime(m) && m!=c[n-1]-(c[n-1]%10)+h, tt=concat(tt,t); ); ); if (#tt, print (n " " c[n]=a[n]=w); see(a[n]); d1 = digits(c[n-1]); d2 = digits(c[n]); [d1[#d1],d2[1]] = [d2[1],d1[#d1]]; see(c[n-1]=fromdigits(d1)); c[n]=fromdigits(d2); dd=tt; break (3); ); ); ); ); ); ); ); } quit