Determine if an Integer's Square Root Is an Integer in Java Article by Abdallah Sawan
This commit is contained in:
parent
6e54323282
commit
6526ccbf35
|
@ -35,7 +35,7 @@ public class PerfectSquareUtil {
|
||||||
return x1 == x2 && n % x1 == 0L;
|
return x1 == x2 && n % x1 == 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSquareNumberWithOptimization(long n) {
|
public static boolean isPerfectSquareWithOptimization(long n) {
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return false;
|
return false;
|
||||||
switch ((int) (n & 0xF)) {
|
switch ((int) (n & 0xF)) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ public class PerfectSquareUnitTest {
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isSquareNumberWithOptimization(n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareWithOptimization(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -22,6 +22,6 @@ public class PerfectSquareUnitTest {
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
||||||
assertEquals(expectedValue, PerfectSquareUtil.isSquareNumberWithOptimization(n));
|
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareWithOptimization(n));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue