# Square root of x (simple)

Posted by lady1dc on Thu, 10 Feb 2022 20:28:54 +0100

LeetCode 69. Square root of X title link:

#### Title Description:

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 of the result will be retained, and the decimal part will be rounded off.

Input: 4
Output: 2

#### Example 2:

Input: 8
Output: 2
Note: the square root of 8 is 2.82842... Because the return type is an integer, the decimal part will be rounded off.

### Topic analysis:

When we see this problem, the first method, we will definitely think of directly calling mathematical functions to solve it. You must be cool in the written interview.
In the second method, we can also use the brute force method. First, if x is 0 or 1, their non negative square root is themselves. We can roughly take the median value and traverse from 1 to the median value. If the square of the current number is less than or equal to X and the square of the next digit is greater than x, the current number is the square root, but the violent solution takes a long time.
If x is greater than or equal to the square of the boundary of the left and right points in the interval, we should use the second method to find the value of the left and right points in the interval. If the value of X is smaller than the square of the boundary of the left and right points in the interval, we should use the second method to find the value of the left and right points in the interval, Update the right boundary to the element on the left of the midpoint value. If the square of the median value is less than x, it means that the answer should be in the right sub interval. We narrow the range and update the left boundary to the right element of the midpoint. If the square of the median value is just equal to x, the median value is the answer we require. If the left boundary is greater than the right boundary, the square of the left boundary is greater than x, and the square of the right boundary is less than x, that is, the right boundary value is smaller than the left boundary value, and the right boundary value can be returned.
The fourth method, Newton iterative method, we can turn this problem into a function solving problem. According to the meaning of the question, we can convert solving the square root into solving the function f ( y ) = y 2 − x = 0 f(y) = y^{2} - x = 0 The solution of f(y)=y2 − x=0. According to Newton iteration formula x n + 1 = x n − f ( x n ) f ′ ( x n ) x_{n+1} = x_{n} − \frac{f(x_{n})}{f′(x_{n})} xn+1​=xn​−f′(xn​)f(xn​)​. because f ( y ) = y 2 − x = 0 f(y) = y^{2} − x = 0 f(y)=y2 − x=0, we substitute the function into the iterative formula y n + 1 = y n + x y n 2 y_{n+1} = \frac{y_{n} + \frac{x}{y_{n}}}{2} yn+1 = 2yn + yn # x, which can be solved by Newton iterative method.

### Question solution 1 (must be hung up in the written interview):

Execution time: 1 ms
Memory consumption: 35.2 MB

class Solution {
public int mySqrt(int x) {
// The written examination interview must hang up the solution and directly call the library function
return (int) Math.sqrt(x);
}
}


### Problem solution 2 (violent solution):

Execution time: 52 ms
Memory consumption: 35.2 MB

class Solution {
public int mySqrt(int x) {
// The square root of 0 is 0, and the non negative square root of 1 is 1
if (x == 0 || x == 1)
return x;
// Take the median value
long mid = x / 2;
// Traverse from 1
for (long i = 1; i <= mid; ++i) {
// If the square of the current digit is less than or equal to X and the square of the next digit is greater than x
if (i * i <= x && (i + 1) * (i + 1) > x)
// Then the current number is the square root
return (int)i;
}
// Method must return an int value
return -1;
}
}


### Solution 3 (binary search method):

Execution time: 2 ms
Memory consumption: 35.5 MB

class Solution {
public int mySqrt(int x) {
// Define left and right boundaries
long left = 0, right = x;
// When the left boundary is less than or equal to the right boundary, the loop is executed
while(left <= right) {
// Define midpoint
long mid = (left + right) / 2;
// The square of the median value is greater than x, and the range is reduced to the left interval
if(mid * mid > x)
// The right boundary is updated to the left element of the midpoint
right = mid - 1;
// The square of the median value is less than x, and the range is reduced to the right range
else if(mid * mid < x)
// The left boundary is updated to the right element of the midpoint
left = mid + 1;
// The square of the median value is equal to x, that is, mid is the square root of X
else if(mid * mid == x)
// Returns the integer part of the square root
return (int) mid;
}
// If the square of the left boundary is greater than X and the square of the right boundary is less than x, the right boundary value meets the condition
if(left * left > x && right * right < x)
// Return right boundary value
return (int) right;
return -1;
}
}


### Solution 4 (Newton iteration method):

Execution time: 2 ms
Memory consumption: 35.5 MB

class Solution {
public int mySqrt(int x) {
long a = x;
while (a * a > x)
a = (a + x / a) / 2;
return (int) a;
}
}


Title Source: leetcode

Topics: leetcode