Skip to content

Commit fcffcf3

Browse files
committed
readme
1 parent fa017c8 commit fcffcf3

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,25 @@
4848
* [Github:#69 Sqrt(x)](/Math/Math.Lib/Sqrtx.cs)
4949
* [CSDN:#69 Sqrt(x)](http://blog.csdn.net/daigualu/article/details/72578272)
5050
* Tips:
51-
* careful to prevent overflowing for bas*digits, so declaring bas is long.
51+
* careful to prevent overflowing for bas*digits.
52+
```C#
53+
public int MySqrt(int x)
54+
{
55+
int lo = 0, hi = x ;
56+
while (lo - hi < -1)
57+
{
58+
//get [lo,hi] middle point,then compare pow2 to x,
59+
// lo or hi is setted by mid
60+
//so accelarate the process
61+
long mid = lo + (hi - lo) / 2; //prevent overflowing
62+
long pow2 = mid * mid; //prevent overflowing
63+
if (pow2 < x) lo = (int)mid;
64+
else if (pow2 > x) hi = (int)mid;
65+
else return (int)mid;
66+
}
67+
return lo;
68+
}
69+
```
5270
---
5371
---
5472

0 commit comments

Comments
 (0)