/* Wikipedia,<a href="http://en.wikipedia.org/wiki/Permutation">Permutation</a> */ permute(k,n) = \\ Express the kth Permutation of the digits of n as a decimal { local(ln,j,tmp,s=0); n=Str(n); ln=length(n); n=eval(Vec(n)); for(j=2,ln, tmp=n[k%j+1]; n[k%j+1] = n[j]; n[j]=tmp; k = floor(k/j); ); for(j=1,ln,s+=n[j]*10^(ln-j)); \\ Put it into decimal s; } permprime(n) = \\ Index of prime diversity. { local(flag,m,x,j,k,y,ln,v,v2,v3,c=0,c2=0); ln=length(Str(n))!; v2=vector(ln+1); v=vector(ln+1); for(x=1,ln, y=permute(x,n); if(isprime(y), c++; v[c]=y; ); ); v2=vecsort(v); for(j=2,ln+1, if(v2[j]<>v2[j-1], c2++; ); ); c2; }