Skip to content

Instantly share code, notes, and snippets.

@EnricoMiccoli
Last active December 28, 2015 12:09
Show Gist options
  • Save EnricoMiccoli/b0dd52eb1d0e756cd281 to your computer and use it in GitHub Desktop.
Save EnricoMiccoli/b0dd52eb1d0e756cd281 to your computer and use it in GitHub Desktop.
Approximates pi using Archimedes' method
module Pigrec where
pigrec :: Int -> Float
pigrec x = foldl (ptwon) (2 * sqrt 2) (map (\x -> 2^x) [2..x])
ptwon :: Double -> Int -> Double
-- Given the perimeter pn of a n-sided polygon inscribed in a
-- circle of diameter 1 returns the perimeter of the 2n-sided
-- polygon inscribed in the same circle
ptwon pn n = n' * ( sqrt (1 + l) - sqrt (1 - l))
where
n' = fromIntegral n
l = pn / n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment