My Weblog

Blog about programming and math

Project Euler 197

Project Euler 197 was totally based on intuition ( its still working although now a days I am not very active ). After doing bit of math, I saw the pattern and rest of work was done by Haskell ( iterate function ). I just wrote the function f and ran

ghci>take 4 . drop 0 .  iterate f $ ( -1 )
[-1.0,0.7100000000000001,1.001242148,0.708777686]
ghci>take 4 . drop 10 .  iterate f $ ( -1 )
[1.005832407,0.7042658820000001,1.0068861020000002,0.7032313570000001]
ghci>take 4 . drop 500 .  iterate f $ ( -1 )
[1.029461825,0.6811758910000001,1.029461827,0.681175889]

Haskell source code

import Data.List

f :: Double -> Double
f x = 1E-9 * ret where
   a = 30.403243784
   y = a - x ^ 2
   ret = fromIntegral ( floor $ 2 ** y )

recur :: [ Double ] -> Double
recur ( a : b : c : xs )
   | abs ( a - c ) <= 1E-10   = a + b
   | otherwise = recur ( b : c : xs )

solve :: Double
solve = recur . iterate  f $ ( - 1 )

main :: IO ()
main = print $ solve  
Advertisements

August 10, 2012 - Posted by | Programming | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: