Crazy Librarian's Interesting Prime Permutation Index Number Generator
Haskell, 166 161 bytes
p n=mod(product[1..n-1]^2)n>0
q=p.read
n#c=[h++c:t|i<-[0..length n],(h,t)<-[splitAt i n]]
[y]%i|q y= -1|1<2=0
(y:z)%i|q y=i|1<2=z%(i+1)
f n=((n#)=<<['1'..'9'])%1
Usage examples: f "8"
-> 6
, f "14"
-> -1
, f "20"
-> 0
.
How it works: p
is the primality test (stolen from @Mauris' answer in a different challenge). q
a wrapper for p
to convert types from strings to integer. n # c
inserts c
at every position in n
. %
takes a list of numbers and an index i
. When the first element of the list is prime, return i
, else recure with the tail of the list and i+1
. Stop when there's a single element left and return -1
if it's prime and 0
otherwise.