Project Euler 67,69

Problem 67

http://projecteuler.net/index.php?section=problems&id=67


上から順に計算していく。上に前と後ろどちらかに0を付加して加算する。


3 3 0 0 3
7 4 7 4 7 4 ->
11 4 7 7 11 7

add a b = [ m + n | (m, n) <- zip a b ]
next a b = [ max m n | (m, n) <- zip (add (a ++ [0]) b) (add (0:a) b) ]

max_path :: [Char] -> Int
max_path s = foldr max 0 (foldl1 next a) where
        a = map (\line -> [ read w | w <- words line ]) (lines s)

main = do
    cs <- readFile "triangle.txt"
    print (max_path cs)

Problem 69

http://projecteuler.net/index.php?section=problems&id=69


小さいほうから素数の積を取る。

is_prime n = all (\p -> mod n p /= 0) (takeWhile (\p -> p * p <= n) primes)
primes = 2:(filter is_prime [3,5..])
a = 1:[ n * p | (n, p) <- zip a primes ]
main = print (last (takeWhile (<=10^6) a))