226 A Scoop of Blancmange

Problem 226 – Project Euler

変な図形の面積を求める.

普通に数値積分する.

ステップ幅などは適当に(収束するか,目的の精度が得られるか試行錯誤).

sawtooth :: Double -> Double
sawtooth x = abs $ x - fromIntegral (round x)
blancmange :: Int -> Double -> Double
blancmange n x = sum.map f $ [..n]
where f m = let pow2 = fromIntegral $ 2^m
in sawtooth (pow2 * x) / pow2
lowCurve :: Double -> Double
lowCurve x = 0.5 - sqrt (x / 2 - x * x)
p226 :: Double -> Int -> Double
p226 h n = (h*).sum.map f.takeWhile ( <= 0.5) $ [,+h..]
where f x = g $ blancmange n x - lowCurve x
g x | x >      = x
| otherwise = 
main :: IO ()
main = print $ p226 1.0e-6 32

あんまり賢くないね.