seq() = {res = List([0, 9]); my(i); nxtcan = Map(); for(i = 0, 9, mapput(nxtcan, i, nxtdigs(i)); ); for(i = 1, 9, nxt([i]); ); listsort(res, 1); res } nxt(v) = { my(i, nd); s = Set(v); if(#s != #v, return); if(s[#s] != v[1], return); nd = mapget(nxtcan, v[#v]); if(#setminus(Set(v[1]), mapget(nxtcan, v[#v])) == 0, listput(res, fromdigits(v)) ); for(i = 1, #nd, nxt(concat(v, nd[i])) )} nxtdigs(n) = setminus(Set(vector(2, i, (2*n+i-1)%10)), Set(n))