import Data.List main = print . filter test $ perm 5 [1..10] test xs = all (\x -> Nothing /= elemIndex x ns) [1..21] where ns = map sum . filter ((<=5) . length) . concatMap tails . inits $ xs ++ xs perm 0 _ = [[]]perm n xs = concatMap (\x -> map (x :) $ perm (n - 1) (delete x xs)) xs
Standard input is empty
[[1,3,10,2,5],[1,5,2,10,3],[2,5,1,3,10],[2,10,3,1,5],[3,1,5,2,10],[3,10,2,5,1],[5,1,3,10,2],[5,2,10,3,1],[10,2,5,1,3],[10,3,1,5,2]]
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!