Last active
June 13, 2016 18:42
-
-
Save Mizzlr/f94ed9be9c718fadaeb87ca2fbf9d061 to your computer and use it in GitHub Desktop.
Fast Square Root Algorithm, Code in Haskell
This file contains hidden or 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
module Main where | |
-- x -> number whose sqrt has to be found | |
-- y -> approximate guessed sqrt of x | |
mysqrt x y = let z = x + y ^ 2 | |
in z / (4 * y) + x * y / z | |
main = do | |
-- lets compute sqrt(10) with guess=3 | |
putStr "Actual Value: " | |
print $ sqrt 10 | |
putStr "My sqrt value, 1st iteration: " | |
print $ mysqrt 10 3 | |
-- substitute computed value again | |
putStr "My sqrt value, 2nd iteration: " | |
print $ mysqrt 10 (mysqrt 10 3) | |
putStrLn "Behold! They are the same" | |
-- Output of this haskell program | |
-- Actual Value: 3.1622776601683795 | |
-- My sqrt value, 1st iteration: 3.162280701754386 | |
-- My sqrt value, 2nd iteration: 3.1622776601683795 | |
-- Behold! They are the same |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment