Created
September 30, 2016 13:02
-
-
Save itsmuriuki/33b46e8cfee0318587693f251c76c235 to your computer and use it in GitHub Desktop.
square root of numbers
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 square_root (n,d) #number and error difference | |
guess = n / 2.0 #half the guess since square root cant be more than half | |
while (((guess *guess) - n).abs > d) | |
guess = refine(guess, n) | |
end | |
puts guess | |
end | |
def refine (guess, n) | |
guess = (guess + (n / guess)) / 2.0 | |
end | |
square_root(9, 0.0000001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment