Created
June 14, 2013 15:35
-
-
Save stanroze/5782786 to your computer and use it in GitHub Desktop.
Binary Search of Square Root
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
public static decimal Sqrt(decimal i) | |
{ | |
decimal lo = 0; | |
decimal hi = i/2; | |
while(lo < hi) | |
{ | |
decimal mid = lo + (hi-lo)/2; | |
if(mid * mid > i ) | |
hi = mid - 1; | |
else if(mid * mid < i) | |
lo = mid + 1; | |
else | |
return mid; | |
} | |
return lo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment