Skip to content

Instantly share code, notes, and snippets.

@yoshitsugu
Created October 8, 2015 03:11
Show Gist options
  • Save yoshitsugu/71821f46b99c326a6761 to your computer and use it in GitHub Desktop.
Save yoshitsugu/71821f46b99c326a6761 to your computer and use it in GitHub Desktop.
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