Compute and return the square root of x.
You do not care about the accuracy of the result, we will help you to output results.
public double sqrt(double x) {
if (x == 1) {
return 1;
}
double xi = 1.0;
double xi1 = 2.0;
while (true) {
xi1 = (xi + x / xi) / 2;
if (Math.abs(xi1 - xi) < 0.000000001) {
break;
}
xi = xi1;
}
return xi1;
}
public double sqrt(double x) {
if (x < 0) {
return 0.0;
}
double start = 0;
double end = x;
if (x < 1) {
end = 1;
}
double delta = 0.000000001;
while ((end - start) > delta) {
double mid = start + (end - start) / 2;
if (mid < x / mid) {
start = mid;
} else {
end = mid;
}
}
return start + (end - start) / 2;
}