Project Euler Problem 001
import Control.Arrow import Control.Monad import Control.Applicative import Data.Function -- 5パターンの解 divs x = x `rem` 3 ==0 || x `rem` 5 == 0 ans0 = sum $ filter divs [1..999] ans1 :: Integer ans1 = sum $ filter (((`rem`3)&&&(`rem`5))>>>(join(***)(==0))>>>(liftA2(||)fst snd)) [1..999] ans2 :: Integer ans2 = sum $ filter( ((||)<$>((==0).(`rem`3)))<*>((==0).(`rem`5)))[1..999] -- 包摂原理(inclusion-exclusion principle)の利用 -- http://en.wikipedia.org/wiki/Project_Euler -- n未満ではなく、n以下で考えたほうが-1とかないのでわかりやすい sumk :: Integral a => a -> a -> a sumk n k= sum [k*i|i<-[1..(n`quot`k)]] sumk' :: Integral a => a -> a -> a sumk' n k = k*(d*(d+1))`quot`2 where d = n`quot`k ans3 :: Integer ans3 = (sk 3) + (sk 5) - (sk 15) where sk = sumk 999 -- O(N) ans4 :: Integer ans4 = (sk 3) + (sk 5) - (sk 15) where sk = sumk' 999 -- O(1) --233168 main = print ans1