日曜日に蛍池で開催されたHaskell勉強会に参加してきました。
Project Eulerの問題を超苦戦しながら解いたのでコードを貼っておきます。
数学苦手すぎる
Problem 1
ts = [3, 6 .. 1000] fs = [5, 10 .. 999] sumTwoList :: [Int] -> [Int] -> Int sumTwoList xs ys = sum xs + sum ys main = print $ sumTwoList ts fs
Problem 2
problem2 :: Int -> Int problem2 n = sum $ takeWhile (< n) [x | x <- fibs, x `mod` 2 == 0] where fibs = 1:1:zipWith(+) fibs (tail fibs) main = print $ problem2 4000000
Problem 3
-- transribed as sya kyo primeFactor :: Integer -> Integer -> [Integer] primeFactor n i | n `div` 2 < i = [n] | n `mod` i == 0 = i : primeFactor (div n i) 2 | otherwise = primeFactor n $ i + 1 main = print $ primeFactor 600851475143 2
Problem 5
main = print $ foldl lcm 1 [2..20]
Problem 6
xs = [1..100] main = print $ (-) ((^2) $ sum xs) (sum $ map (^2) xs)