Skip to content

Instantly share code, notes, and snippets.

@timjb
Created November 1, 2016 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timjb/309052550d04371b24de9332575acc94 to your computer and use it in GitHub Desktop.
Save timjb/309052550d04371b24de9332575acc94 to your computer and use it in GitHub Desktop.
Approximate pi using Archimedes' method
module Main (main) where
import Control.Monad (forM_)
-- | Given the length l of a cord from points A to B on the unit circle, compute
-- the length of the cords A-C and C-B, where C is the point on the unit circle
-- exactly in between A and B.
iterArchimedes :: Double -> Double
iterArchimedes l = l / sqrt (2 + sqrt (4 - l*l))
main :: IO ()
main =
forM_ (zip [0..1000] (iterate iterArchimedes 2)) $ \(n, b_twotothen) ->
let bb_twotothen = 2^n * b_twotothen
in putStrLn ("n=" ++ show n ++ ", b_{2^n} = " ++ show b_twotothen ++ ", B_{2^n} = " ++ show bb_twotothen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment