eva(n,b) = subst(Pol(n), x, b) find_index_a(vec) = my(r=#vec-1); while(1, if(vec[r] < vec[r+1], return(r)); r--; if(r==0, return(-1))) find_index_b(r, vec) = my(s=#vec); while(1, if(vec[r] < vec[s], return(s)); s--; if(s==r, return(-1))) switch_elements(vec, firstpos, secondpos) = my(g); g=vec[secondpos]; vec[secondpos]=vec[firstpos]; vec[firstpos] = g; vec reverse_order(vec, r) = my(v=[], w=[]); for(x=1, r, v=concat(v, vec[x])); for(y=r+1, #vec, w=concat(w, vec[y])); w=Vecrev(w); concat(v, w) next_permutation(vec) = my(r=find_index_a(vec)); if(r==-1, return(0), my(s=find_index_b(r, vec)); vec=switch_elements(vec, r, s); vec=reverse_order(vec, r)); vec is(n,b) = if(n < b, return(1)); my(d=vecsort(digits(n,b))); if(vecmin(d)==0, return(0)); while(1, if(!ispseudoprime(eva(d,b)), return(0)); d=next_permutation(d); if(d==0, return(1))) a(b)=forprime(p=1, b^6, if(is(p,b), f(p,b); print1(", "))) z(m)=for(n=7, 600, my(S=[], r=(m^n-1)/(m-1)); for(a=1, (m-1)^(n>1), for(b=if(n>2, 1-a), m-1-a, for(j=0, if(b, n-1), ispseudoprime(a*r+b*m^j)||next(2)); S=concat(S, vector(if(b, n, 1), k, a*r+m^(k-1)*b)))); apply(t->f(t,m);print1(", "), Set(S))) y(n)=a(n);z(n) g(n)=if(n<10,n+48,if(n<36,n+55,if(n<62,n+61,if(n<77,n-29,if(n<84,n-19,if(n<90,n+7,if(n<94,n+33,n+67))))))) f(n,b)=for(k=1,length(digits(n,b)),print1(Strchr(g(digits(n,b)[k])))) for(k=2,256,print1(k, ": ");y(k);print())