Created
May 29, 2020 12:16
-
-
Save RyanSusana/770515a082b7238344facff2b950d170 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
def sqrt(x: Double): Double = { | |
def abs(x: Double) = if (x > 0) x else -x | |
def isGood(guess: Double, x: Double): Boolean = | |
abs(guess * guess - x) < 0.0001 | |
def improve(guess: Double, x: Double) = | |
(guess + x / guess) / 2 | |
def sqrtIter(x: Double, guess: Double): Double = | |
if (isGood(guess, x)) guess else sqrtIter(improve(guess, x), x) | |
sqrtIter(x, 1.0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment