import Common

main = print $ head $ [ x | x <- [987654321,987654321-2..100000000], isPandigital x 9, isPrime x ] ++
                      [ x | x <- [87654321, 87654321-2.. 10000000],  isPandigital x 8, isPrime x ] ++
                      [ x | x <- [7654321,  7654321-2..  1000000],   isPandigital x 7, isPrime x ] ++
                      [ x | x <- [654321,   654321-2..   100000],    isPandigital x 6, isPrime x ] ++
                      [ x | x <- [54321,    54321-2..    10000],     isPandigital x 5, isPrime x ] ++
                      [ x | x <- [4321,     4321-2..     1000],      isPandigital x 4, isPrime x ] ++
                      [ x | x <- [321,      321-2..      100],       isPandigital x 3, isPrime x ] ++
                      [ x | x <- [21,       21-2..       10],        isPandigital x 2, isPrime x ]


isPandigital :: Int -> Int -> Bool
isPandigital n x = head digits /= 0 && haveUniqueElements digits && last digits <= x
                   where
                       digits = sort $ toDigits n
