Project Euler 解答

Project Euler Problem 032

Project Euler Problem 032

import Control.Monad import Data.Char import Data.List import Data.IntSet(fromList,toList) str :: [Char] str = "123456789" dig :: [Int] dig = map digitToInt str idp :: (Num a, Show a) => a -> a -> Bool idp l r = ((=="123456789") .sort.(++show l).(++show r).show) (l*r) idpp :: (Num a, Show a) => a -> a -> [Char] idpp l r = (sort.(++show l).(++show r).show) (l*r) ansl :: [(Int, Int)] ansl = [(l,r) | l<-[1..9876],r<-[l..9876],idp l r] ans1 :: Int ans1 = sum (toList $ fromList $ map (\(l,r)->l*r) ansl) ansl2 :: [(Int, Int)] ansl2 = [(l,r) | l<-[4..49],r<-[l+1..1987],idp l r] ans2 :: Int ans2 = sum (toList $ fromList $ map (\(l,r)->l*r) ansl2) ansl3 :: [Integer] ansl3 = nub $ sort [(read ll)*(read rr) | v<-permutations "123456879",let r = check v,r/=("",""),let (ll,rr)=r] ans3 :: Integer ans3 = sum ansl3 check :: [Char] -> ([Char], [Char]) check str1 = let r = take 5 str1 in let (l1,r1) = splitAt 2 r in let (l2,r2) = splitAt 1 r in case idp2 l1 r1 of True -> (l1,r1) _ -> case idp2 l2 r2 of True -> (l2,r2) _ -> ("","") where idp2 l r = idp(read l :: Integer) (read r) --45228 main :: IO () main = print ans2

since 2013