Skip to content

Instantly share code, notes, and snippets.

@brunoczim
Last active December 14, 2018 23:39
Show Gist options
  • Save brunoczim/173fefc6f11e0013293777f8e96918fb to your computer and use it in GitHub Desktop.
Save brunoczim/173fefc6f11e0013293777f8e96918fb to your computer and use it in GitHub Desktop.
Euler and Golden Ratio Approximation in Haskell
module Approx where
approxGolden :: Int -> Double
approxGolden n = approx n 1 1
where
approx 0 p q = q / p
approx n p q = approx (n - 1) q (p + q)
golden :: Double
golden = approxGolden 64
approxEuler :: Int -> Double
approxEuler n = approx 0 1 0 where
approx m a r = if m == n
then r
else approx (m + 1) a' r'
where
a' = (fromInteger . toInteger) (m + 1) * a
r' = r + (1 / a)
euler :: Double
euler = approxEuler 64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment