This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -2,11 +2,10 @@ import Data.List
import Control.Arrow
import Control.Monad
derangements [1] = [[1]]
derangements xs = filter (and . zipWith (/=) [1..] ). permutations $ xs
derangements = filter (and . zipWith (/=) [1..] ). permutations
topswop = ((uncurry (++). first reverse).). splitAt
topswopIter = takeWhile((/=1).head). iterate (topswop =<< head)
swops = map (length. topswopIter). derangements
topSwops :: [Int] -> [(Int, Int)]
topSwops = zip [1..]. map (maximum. swops). drop 1. inits
topSwops = zip [1..]. map (maximum. (0:). swops). tail. inits

View file

@ -0,0 +1,21 @@
procedure main()
every n := 1 to 10 do {
ts := 0
every (ts := 0) <:= swop(permute([: 1 to n :]))
write(right(n, 3),": ",right(ts,4))
}
end
procedure swop(A)
count := 0
while A[1] ~= 1 do {
A := reverse(A[1+:A[1]]) ||| A[(A[1]+1):0]
count +:= 1
}
return count
end
procedure permute(A)
if *A <= 1 then return A
suspend [(A[1]<->A[i := 1 to *A])] ||| permute(A[2:0])
end