euler/Problem024.hs

15 lines
434 B
Haskell

-- What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
import Data.List
factorial n = product [1..n]
deleteAt n xs = let (as, b:bs) = splitAt n xs in (b, as ++ bs)
nthPermut :: [a] -> Int -> [a]
nthPermut xs 0 = xs
nthPermut xs n = x : nthPermut xs' n'
where
(m, n') = n `divMod` factorial (length xs - 1)
(x, xs') = deleteAt m xs
main = putStrLn $ nthPermut ['0'..'9'] 999999