mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add tests, remove main
in SquareRootBinarySearch
(#5676)
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Given an integer x, find the square root of x. If x is not a perfect square,
|
||||
* then return floor(√x).
|
||||
@ -18,20 +16,6 @@ public final class SquareRootBinarySearch {
|
||||
private SquareRootBinarySearch() {
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the driver method.
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Enter a number you want to calculate square root of : ");
|
||||
int num = sc.nextInt();
|
||||
long ans = squareRoot(num);
|
||||
System.out.println("The square root is : " + ans);
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calculates the floor of square root of a number. We use
|
||||
* Binary Search algorithm to calculate the square root in a more optimised
|
||||
@ -40,7 +24,7 @@ public final class SquareRootBinarySearch {
|
||||
* @param num Number
|
||||
* @return answer
|
||||
*/
|
||||
private static long squareRoot(long num) {
|
||||
static long squareRoot(long num) {
|
||||
if (num == 0 || num == 1) {
|
||||
return num;
|
||||
}
|
||||
|
Reference in New Issue
Block a user