akar kuadrat di java menggunakan pencarian biner
public class SquareRoot {
static void square_root(int n){
int start = 0;
int end = n;
double root = 0.0;
while (start <= end){
int mid = start + (end-start)/2;
if (mid*mid == n) {
root = mid;
System.out.println(mid);
System.exit(0);
}
if (mid*mid > n) end = mid-1;
else start = mid+1;
}
double increment = 0.1;
for (int i = 0; i <=3 ; i++) {
while(root*root <=n){
root+=increment;
}
root-=increment;
increment/=10;
}
System.out.printf("%.3f", root);
}
public static void main(String[] args) {
square_root(144);
}
}
Arrogant Angelfish