Project Euler 解答

Project Euler Problem 047

Project Euler Problem 047

import Data.List hiding (union) --003より primes :: [Integer] primes = 2 : ([3,5..] `minus` join [[p*p, p*p+2*p..] | p <- primes']) where primes' = 3 : ([5,7..] `minus` join [[p*p, p*p+2*p..] | p <- primes']) join ((x:xs):t) = x : union xs (join (pairs t)) pairs ((x:xs):ys:t) = (x : union xs ys) : pairs t union (x:xs) (y:ys) = case (compare x y) of LT -> x : union xs (y:ys) EQ -> x : union xs ys GT -> y : union (x:xs) ys union xs [] = xs union [] ys = ys minus (x:xs) (y:ys) = case (compare x y) of LT -> x : minus xs (y:ys) EQ -> minus xs ys GT -> minus (x:xs) ys minus xs _ = xs factor :: Integer -> [Integer] factor nn = factorimpl nn primes where factorimpl n pri@(p:xs) = if p*p>n then [n] else if n`rem` p == 0 then p:factorimpl (n `quot` p) pri else factorimpl n xs fac4 :: Integer -> Bool fac4 n= length (group $ factor n ) == 4 geta :: (Eq a, Num a) => [a] -> [a] geta lst = if con lst then take 4 lst else geta (tail lst) con :: (Eq a, Num a) => [a] -> Bool con (a:b:c:d:_) | b==a+1&&c==a+2&&d==a+3=True con _ = False ans1 :: Integer ans1 = head $ geta $ filter fac4 [1..] --134043 by [134043,134044,134045,134046] -- 134043 main :: IO () main = print ans1

since 2013