Label: two points

Title: square root of X

Question No.: 69

Stem: implement the int sqrt (int x) function. Calculates and returns the square root of X, where x is a nonnegative integer. Since the return type is an integer, only the integer part will be retained in the result, and the decimal part will be rounded offExample 1:

Input: 4

Output: 2Example 2:

Input: 8

Output: 2

Explanation: the square root of 8 is 2.82842, Since the return type is an integer, the decimal part will be rounded off

The original question is relatively simple, so here is a simple deformation,**Finding the square root of X requires that the result with the specified accuracy can be obtained**

```
Example:
Input: 8 0.000001
Output: 2.828427
Note: 0.00000 1 means accurate to six decimal places
```

Idea:

- The easiest thing to think of when we ask for the square root of X is to try one by one from 1 to X. the time complexity of this method is O (n), which is not what we want
- Binary search algorithm is obviously used to find the algorithm, and our purpose is also to find. If you read my last article on binary search, it said that you would think of using binary search under any circumstances. For example, the series of numbers we want to find are ordered. We require the square root of X, and it must be found in the interval [1, x] (x > 1). It is obviously ordered. This will naturally think of using two points to find
- Using the binary search algorithm, two cursors, low and high, are required. What are their initial values to consider? And how do low and high change when mid is not satisfied?
- Since we are looking for the square root of X, there are two cases for the value of X. The first is x < 1. In this case, the result we require must be in the interval [0,1], so we can initialize Low = 0, high = 1, and the mid is (low + high) / 2
- The second case of X is very simple. When x > 1, the value of its square root must be in the [1, n] interval, so low = 1, high = x, mid = (low + high) / 2
- It is not difficult to think that when x-mid * mid < 0, it means that the mid is too large. At this time, we should find between [1, mid], that is, let height = mid-1
- If x-mid
*If mid > 0, x-mid should be considered*Is the mid greater than the precision we require? If it is greater than this precision, then low = mid + 1 - With the above conditions, basically this problem comes out.
**If you write the code according to the above idea, you will find that there is a problem. When x < 1, the program will not run normally. The reason is that when low and high change, if x < 1, the result must be less than 1. If high or low is directly added or subtracted to mid, it must be wrong** - Therefore, when x-mid * mid < 0, let
**high=(mid+high)/2**

code implementation

```
func SolutionSqrt(num float64, precision float64) float64 {
if num < 0 {
fmt. Println ("illegal parameter")
return -1.000
}
var low,high float64
if num > 1 {
low = 1
high = num
} else {
low = num
high = 1
}
return Sqrt(num, low, high, precision)
}
func Sqrt(num float64, low float64, high float64, precision float64) float64 {
mid := (low+high) / 2
if num - (mid * mid) < 0 {
return Sqrt(num, low, (mid+high)/2, precision)
} else {
if num - (mid * mid) > precision {
return Sqrt(num, (low + mid)/2, high, precision)
}
return mid
}
}
```