Problem 25
http://projecteuler.net/index.php?section=problems&id=25
-- Haskell, 090104 import List (findIndex) import Maybe (fromJust) import Sequence (fibonacci) main = print $ (+) 1 $ fromJust $ findIndex ((<=) 1000 . length . show) fibonacci
数列モジュールを使用.
素数モジュール
-- Haskell, 090105 module Prime (prime, isprime) where -- 素数 prime :: Integral a => [a] prime = 2 : filter (\n -> (all (\x -> n`mod`x/=0) . takeWhile (\x -> x*x<=n)) prime) [3..] -- 素数判定 isprime :: Integral a => a -> Bool isprime n = any (\x -> n`mod`x==0) $ takeWhile (\x -> x*x<=n) [2..]
prime関数はsatzz online 2.0様より.
数列モジュール
-- Haskell, 090104 module Sequence (prime, fibonacci, factorial, trianglar) where import qualified Prime (prime) -- 素数 prime = Prime.prime -- フィボナッチ数 fibonacci :: Integral a => [a] fibonacci = 1 : 1 : zipWith (+) fibonacci (tail fibonacci) -- 階乗 factorial :: Integral a => [a] factorial = 1 : zipWith (*) factorial [2..] -- 三角数 trianglar :: Integral a => [a] trianglar = zipWith (+) [1..] (0 : trianglar)
MakeYourDay
無用の科学あるいは錬金術様にて公開されたMakeYourDayのスクリプトがtwitterの仕様変更のためか動かなかったので修正.
致命的なのは41行目の正規表現.以下のように修正すれば動きました.
# Ruby, 20090102 /<span class="entry-content">\s*(.+?)(\s*<a href="http:\/\/twitter\.com\/[^\/]+\/status\/\d+">\.\.\.<\/a>|\s*)<\/span>\s*<span class="meta entry-meta">\s*<a href="http:\/\/twitter\.com\/[^\/]+\/status\/(\d+)" class="entry-date" rel="bookmark"><span class="published" title="(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d)\:(\d\d)\:\d\d\+00\:00">/
また,この正規表現のすぐ上にあるif文は必要なさそうなので,削っても良いでしょう.
Problem 26
http://projecteuler.net/index.php?section=problems&id=26
--Haskell (needs -package containers), 20090102 import List (elemIndex) import Maybe (fromJust) import Data.Map (findMax, fromList) search :: Int -> (Int, Int) search n = (fromJust $ search' Nothing 1 [], n) where search' :: Maybe Int -> Int -> [Int] -> Maybe Int search' _ 0 _ = Just 0 search' (Just k) _ _ = Just (k+1) search' Nothing t l = search' (elemIndex t l) (t*10`mod`n) (t:l) main = print $ snd $ findMax $ fromList $ map search [2..999]
改善の余地あり.
Problem 18
http://projecteuler.net/index.php?section=problems&id=18
--Haskell (Input data from STDIN), 20090101 process :: [Int] -> [Int] -> [Int] process f t = zipWith max (zipWith (+) f (init t) ++ [last t]) (head t : zipWith (+) f (tail t)) main = getContents >>= print . maximum . foldl1 process . map (map read . words) . lines