Created
September 29, 2016 08:48
-
-
Save itzbernoulli/da69416cf92f3ccd07aa78aa02473792 to your computer and use it in GitHub Desktop.
This code is meant to calculate the nearest possible value to the square-root of a number.
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
def mid(x,y) | |
midpoint = (x + y)/2.0 | |
end | |
def sqrt(n,d) | |
lower_range = 0 | |
higher_range = n | |
div = mid(higher_range,lower_range) | |
#midpoint = (higher_range + lower_range) / 2.0 | |
until (div * div) - n <= d and (div * div) -n >= 0 do | |
higher_range = div if (div * div) - n > d | |
lower_range = div if (div * div) - n < 0 | |
div =mid(lower_range,higher_range) | |
#midpoint = (lower_range + higher_range) / 2 | |
end | |
div | |
end | |
puts sqrt(100000,0.0000001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment