246 Tangents to an ellipse

Problem 246 – Project Euler

格子点を数える簡単なお仕事?(そう思っていた時期が(ry

直線と楕円の方程式と三角関数の加法定理を知っていて,二次方程式が解ければ出来る問題ではあるが…

数式計算が大変.この一言に尽きる.

あとは,本当に数えるだけ.

main :: IO ()
main = print.p246 7500 $ sqrt 5 * 2500
hight :: Double -> Double -> Double -> Integer
hight a b x = floor.sqrt $ 2 * sqrt ( (b^2 - a^2) * x^2 + 2 * a^4 ) - x^2 + b^2 + 3 * a^2
ground :: Double -> Double -> Double -> Integer
ground a b x | a <= x    = 
| otherwise = floor $ b * sqrt ( 1 - (x / a)^2 )
inner :: Double -> Double -> Integer
inner a b = sum.map (count.fromInteger) $ [1..hight b a ]
where count x = hight a b x - ground a b x
p246 :: Double -> Double -> Integer
p246 a b = 4 * inner a b + 2 * (hight b a  - floor a) + 2 * (hight a b  - floor b)