module Problem235 where

import Data.Ratio
import Text.Printf (printf)


type Decimal = Ratio Integer


s :: Decimal -> Decimal
s r = ((897 - 900 * r + (14103 - 14100 * r) * r ^ 5000) / ((-1 + r) ^ 2))
    + realToFrac (6 * (10 ^ 11))

main :: IO ()
-- u(k) = (900-3k)r^(k-1)
-- s(5000) = -6x10^12
main = printf "%.12f\n" $ search s 1 2


search :: (Decimal -> Decimal) -> Decimal -> Decimal -> Double
search f lo hi
    | abs v < 1 = realToFrac mid
    | v > 0     = search f mid hi
    | v < 0     = search f lo mid
  where
    mid = (lo + hi) / 2
    v   = f mid
