Hatena::ブログ(Diary)

lounlizm

2009-01-11

[] TwitterToMail Alpha

Twitterの最低限の機能をメールで済ませてしまおうという計画のもとに作ったスクリプト.要PEAR::Mail,PEAR::Net::POP3.

APIを使わないのは回数制限を回避するため,自分のTLを使わないのは遅延を回避するため,のつもり.

まだ投稿部分ができていない.fooとかbarとか多いのは,行き当たりばったりに作っているので勘弁.

コピーレフトなのでむしろ誰か改良して私にください.

続きを読む

2009-01-04

[] 数列モジュール

-- 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)

[] 素数モジュール

-- 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様より.

[][] 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

数列モジュールを使用.

2009-01-02

[][] 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]

改善の余地あり.

[][] 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文は必要なさそうなので,削っても良いでしょう.

2008-11-29

[][] Problem 22

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

--Haskell (Input data from STDIN), 20090101

import Char (ord, isAlpha)
import List (sort)

split :: String -> [String]
split ['"'] = []
split xs    = let (f, t) = span isAlpha $ dropWhile (not . isAlpha) xs
              in f : split t

main = getLine >>= print . sum . zipWith (*) [1..] . map (sum . map (subtract 64 . ord)) . sort . split

[][] Problem 21

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

--Haskell, 20090101

import List (nub)

divisors :: Int -> Int
divisors n = sum $ 1 : concat [nub [x, n`div`x] | x<-takeWhile ((<=n).(^2)) [2..], n`mod`x==0]

main = print $ sum [x | x<-[1..9999], let y = divisors x, y /= x, x == divisors y]

[][] Problem 20

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

--Haskell, 20090104

import Char (digitToInt)
import Sequence (factorial)

main = print $ sum $ map digitToInt $ show $ factorial!!99

数列モジュールを使用.

[][] Problem 19

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

--Haskell, 20090101

zeller :: Int -> Int -> Int
zeller y m = if (y + y`div`4 - y`div`100 + y`div`400 + (13*m+8)`div`5 + 1) `mod` 7 == 0 then 1 else 0

main = print $ sum $ map (sum . flip map [3..14] . zeller) [1901..2000]

[][] 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

2008-11-26

[][] Problem 16

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

--Haskell, 20090101

import Char (digitToInt)

main = print $ sum $ map digitToInt $ show (2^1000)

[][] Problem 15

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

--Haskell, 20090104

import Sequence (factorial)

main = print $ factorial!!39`div`(factorial!!19)^2

数列モジュールを使用.