Euler Solution 197

From ProgSoc Wiki

Jump to: navigation, search

Solutions for Problem 197

Define:

  • f(x) = (230.403243784 - x * x)* 10 -9
  • un = f(un-1)
  • u0 = -1

Find u1000000000000 + u1000000000001.

Haskell by SanguineV

Runtime: 68.299 ms

{- Define the function "f". -}
fn :: Double -> Double
fn x = (2 ** (30.403243784 - x * x)) * (10 ** (-9))

{- Define an infinite list of u's -}
uns :: [Double]
uns = inner (-1)
  where
    inner i = i : inner (fn i)

{- Some analysis of the results shows that the u's oscillate between two
 - values (or rather converges on two such values). So run through the u's
 - until they are the same (within the precision of Doubles). Then return
 - the two values that will repeat endlessly for the rest of the u's. -}
findLims :: [Double] -> [Double]
findLims (x1:x2:xs) | [x1,x2] == take 2 xs = [x1,x2]
findLims (x:xs) = findLims xs

{- We need to find the same of two elements one after the other. We know they
 - will be the two values we converge on, so find those values and add them. -}
main = print (sum (findLims uns))

Note: The project euler website is down for me right now. I am confident the solution posted is correct... but haven't put in the answer for the ProgSoc account (the next person to solve it might have that honour).

Personal tools