Created
October 8, 2015 03:11
-
-
Save yoshitsugu/71821f46b99c326a6761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nwSqrt :: Int -> Double | |
nwSqrt n = let s = sqrts $ fromIntegral n in | |
snd $ last $ takeWhile inThreshold $ zip s (tail s) | |
where | |
sqrts :: Double -> [Double] | |
sqrts n = 1.0:[x - (x ^ 2 - n) / (2 * x) | x <- sqrts n] | |
inThreshold :: (Double,Double) -> Bool | |
inThreshold (a,b) = abs (a - b) > 0.00000001 | |
-- *Main> nwSqrt 2 | |
-- 1.4142135623746899 | |
-- *Main> nwSqrt 3 | |
-- 1.7320508100147276 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment