2011-07-01から1ヶ月間の記事一覧

Problem 22

split :: (Eq a) => a -> [a] -> [[a]] split _ [] = [] split delimiter xs = left : (split delimiter right') where (left, right) = break (== delimiter) xs right' = if null right then [] else tail right p22 = do fh <- openFile "names.txt" Read…

Problem 21

combinations :: [a] -> [[a]] combinations [] = [[]] combinations (x:xs) = concat [[t, (x:t)] | t <- combinations xs] amicable a limit = a /= d(a) && d(a) < limit && a == d(d(a)) d a = (sum $ unique $ sort $ map product $ combinations $ fac…

Problem 20

sum $ map (\x -> read [x]::Int) $ show $ product [1..100]

Problem 19

ツェラーの公式を使った p19_z :: Int -> Int -> Int -> Int p19_z year month day | month < 3 = p19_z (year-1) (month+12) day | otherwise = mod (year + (div year 4) - (div year 100) + (div year 400) + (div (13 * month + 8) 5) + day) 7 length […

Problem 5

最小公倍数を求める lcm があったのでスッキリ書き直した foldr lcm 1 [1..20]

Problem 23

factor3 :: Integral a => a -> [a] factor3 n = unique $ sort $ first ++ second where first = [x | x <- [1..limit], n `mod` x == 0] second = map (\x -> div n x) first limit = truncate $ sqrt $ fromIntegral n p23 n = p23' [1..n] $ gen_ab_list…

Problem 13

take 10 $ show $ sum $ map (\x -> read x::Integer) $ lines p13_input

Problem 12

uniqueCount :: (Eq a) => [a] -> [(a,Int)] uniqueCount [] = [] uniqueCount [x] = [(x,1)] uniqueCount all@(x:xs) = (x, length left) : uniqueCount right where (left, right) = break (\a -> a /= x) all product $ map (\x -> product (replicate (f…

Problem 67

Problem18と同じプログラムで解けるので。 p67 = do fh <- openFile "triangle.txt" ReadMode c <- hGetContents fh let triangle = map (\line -> map (\s -> read s::Integer) $ words line) $ lines c putStr $ show $ maximum $ p18 triangle

Problem 18

p18_input = "75\n95 64\n17 47 82\n18 35 87 10\n20 04 82 47 65\n19 01 23 75 03 34\n88 02 77 73 07 63 67\n99 65 04 28 06 16 70 92\n41 41 26 56 83 40 80 70 33\n41 48 72 33 47 32 37 16 94 29\n53 71 44 65 25 43 91 52 97 51 14\n70 11 33 28 77 73…

Problem 17

p17_n_to_word :: Int -> String p17_n_to_word 0 = "" p17_n_to_word 1 = "one" p17_n_to_word 2 = "two" p17_n_to_word 3 = "three" p17_n_to_word 4 = "four" p17_n_to_word 5 = "five" p17_n_to_word 6 = "six" p17_n_to_word 7 = "seven" p17_n_to_word…

Problem 16

sum $ map (\x -> read [x]::Int) $ show $ product $ replicate 1000 2

Problem 15

p15 :: Integer -> Integer -> Integer p15 w h = product [1..(w+h)] `div` product [1..w] `div` product [1..h] p15 20 20

Problem 14

ものすごい時間がかかる... いつか書き直す p14 :: (Integer, Integer) -> [Integer] -> (Integer, Integer) p14 max' [] = max' p14 max' (x:xs) = p14 new_max xs where ret = p14' x new_max = if (fst max') < (fst ret) then ret else max' p14' :: Int…

Haskellの勉強のためにProject Eulerを解いてみる

計算量とか最適化とかは考えずとりあえず解いてみる

Problem 11

かなりグダグダになった... Haskellがうまくあやつれるようになったら書き直す p11_input = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n81 49 31 73 55 79 14 29 93 71 …

Problem 10

sum $ prime 2000000

Problem 9

p9 :: [[Int]] -> [[Int]] p9 [] = [] p9 (x:xs) | ok x = x : p9 xs | otherwise = p9 xs where ok (a:b:c:_) | a*a + b*b == c*c && a+b+c == 1000 = True | otherwise = False (\x -> head x * (head $ tail x) * (head $ tail $ tail x )) $ head $ p9 […

Problem 8

split_s_to_i :: [Char] -> [Int] split_s_to_i [] = [] split_s_to_i (x:xs) = (read [x]::Int) : (split_s_to_i xs) p8 :: [Int] -> [Int] p8 (a:b:c:d:e:xs) = a*b*c*d*e : p8 (b:c:d:e:xs) p8 _ = [] maxiumu $ p8 $ split_s_to_i "7316....."

Problem 7

10001番目の判定をサボったので100000と1000000の2回やって値が同じなので正解と判定 last $ take 10001 $ prime 100000 last $ take 10001 $ prime 1000000

Problem 6

((\x->x*x) $ sum [1..100]) - (sum $ map (\x->x*x) [1..100])

Problem 5

p5 :: [Integer] -> Integer p5 s = _p5 1 s where _p5 r [] = r _p5 r (x:xs) = _p5 ( r * (foldl (*) 1 (_sub (factor x) (factor r)))) xs _sub left [] = left _sub [] right = [] _sub (l:ls) (r:rs) | l == r = _sub ls rs | l < r = l : _sub ls (r:r…

Problem 4

maximum $ filter (\n -> (reverse $ show n) == (show n)) $ map (\pair -> fst pair * snd pair) [(x,y) | x <- [1..999], y <- [1..999]]

Problem 3

prime :: Integer -> [Integer] prime m = 2 : era [3,5..m] where era [] = [] era (p:xs) | p*p > m = p : xs | otherwise = p : era [x | x<-xs, x `mod` p /= 0] factor :: Integer -> [Integer] factor x = _factor x $ prime x where _factor 1 _ = []…

Problem 2

fib :: [Int] fib = 1 : 2 : zipWith (+) fib (tail fib) sum $ filter even $ takeWhile (\x -> x < 4000000) $ fib

Problem 1

sum $ filter (\x -> x `mod` 3 == 0 || x `mod` 5 == 0) [1..999]