Help:=proc(): print(`Flip(pi,i,j) `): print(`Sphere1(pi), SphereR(pi,R) , W(n)`): end: #Flip(p,i,j): Given a list pi returns #the same list but op(i..j,pi) reversed: Flip:=proc(p,i,j) local k: [op(1..i-1,p),seq(p[j-k],k=0..j-i),op(j+1..nops(p),p)]: end: #Sphere1(pi): The sphere of distance 1 from the permutation #pi (via one prefix reversal) Sphere1:=proc(pi) local j: option remember: {seq(Flip(pi,1,j),j=2..nops(pi)) }: end: #SphereR(pi,R): The spehere of distance R from the permutation #pi (via one prefix reversal) SphereR:=proc(pi,R) local i,B,S: option remember: if R=0 then RETURN({pi}): fi: B:= {seq(op(SphereR(pi,i)),i=0..R-1)}: S:=SphereR(pi,R-1): {seq(op(Sphere1(S[i])),i=1..nops(S))} minus B: end: #W(n): the number of operations that it may #take to sort any permutation of length n #followed by the most diffucult layer W:=proc(n) local C,N,pi,i: pi:=[seq(i,i=1..n)]: for i from 0 do C:=SphereR(pi,i): N:=SphereR(pi,i+1): if N={} then RETURN(i,C): fi: od: FAIL: end: