Add tests, remove main in SquareRootBinarySearch (#5676)

This commit is contained in:
Hardik Pawar
2024-10-11 01:27:40 +05:30
committed by GitHub
parent 38285771c8
commit a663e66782
3 changed files with 59 additions and 17 deletions

View File

@ -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;
}