go ?=> member(N,1..10), Perm = 1..N, Rev = Perm.reverse(), Max = 0, while(Perm != Rev) next_permutation(Perm), C = topswops(Perm), if C > Max then Max := C end end, printf("%2d: %2d\n",N,Max), fail, nl. go => true. topswops([]) = 0 => true. topswops([1]) = 0 => true. topswops([1|_]) = 0 => true. topswops(P) = Count => Len = P.length, Count = 0, while (P[1] > 1) Pos = P[1], P := [P[I] : I in 1..Pos].reverse() ++ [P[I] : I in Pos+1..Len], Count := Count + 1 end. % Inline next_permutation(Perm) => N = Perm.length, K = N - 1, while (Perm[K] > Perm[K+1], K >= 0) K := K - 1 end, if K > 0 then J = N, while (Perm[K] > Perm[J]) J := J - 1 end, Tmp := Perm[K], Perm[K] := Perm[J], Perm[J] := Tmp, R = N, S = K + 1, while (R > S) Tmp := Perm[R], Perm[R] := Perm[S], Perm[S] := Tmp, R := R - 1, S := S + 1 end end.