Skip to content

Instantly share code, notes, and snippets.

@stanroze
Created June 14, 2013 15:35
Show Gist options
  • Save stanroze/5782786 to your computer and use it in GitHub Desktop.
Save stanroze/5782786 to your computer and use it in GitHub Desktop.
Binary Search of Square Root
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