Life Goes On

まあまあだけど楽しんでる方です

32問目

http://projecteuler.net/index.php?section=problems&id=32
39 × 186 = 7254 のように、a × b = c の各数字を合わせると1から9の数字が1回ずつ現れる a × b を、すべて求める。
要件を満たすのは1桁×4桁、2桁×3桁のときなので、その範囲で全部調べます。

import Data.List

main = print euler032

euler032 :: Int
euler032 = sum $ nub $
    map (\ (a, b) -> (read a) * (read b)) $
    filter isPandigital $
    [ splitAt n s | n <- [1, 2], s <- (perm 5 "123456789")]

isPandigital :: (String, String) -> Bool
isPandigital (a, b) =
    sort (a ++ b ++ (show ((read a) * (read b)))) == "123456789"

perm :: Int -> String -> [String]
perm 0 digits = [""]
perm n digits = [x : xs |
    x <- digits, xs <- perm (n - 1) (delete x digits)]