From 6e54323282acaecd3f8fbca9a93bb2545e4de1ce Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Sun, 22 Nov 2020 13:20:43 +0200 Subject: [PATCH] Determine if an Integer's Square Root Is an Integer in Java Article by Abdallah Sawan --- .../perfectsquare/PerfectSquareUtil.java | 49 +++++++++++++++++++ .../perfectsquare/PerfectSquareUnitTest.java | 27 ++++++++++ 2 files changed, 76 insertions(+) create mode 100644 java-numbers-4/src/main/java/com/baeldung/perfectsquare/PerfectSquareUtil.java create mode 100644 java-numbers-4/src/test/java/com/baeldung/perfectsquare/PerfectSquareUnitTest.java diff --git a/java-numbers-4/src/main/java/com/baeldung/perfectsquare/PerfectSquareUtil.java b/java-numbers-4/src/main/java/com/baeldung/perfectsquare/PerfectSquareUtil.java new file mode 100644 index 0000000000..bd08fbe60b --- /dev/null +++ b/java-numbers-4/src/main/java/com/baeldung/perfectsquare/PerfectSquareUtil.java @@ -0,0 +1,49 @@ +package com.baeldung.perfectsquare; + +public class PerfectSquareUtil { + + public static boolean isPerfectSquareByUsingSqrt(long n) { + if (n <= 0) + return false; + double perfectSquare = Math.sqrt(n); + long tst = (long)(perfectSquare + 0.5); + return tst*tst == n; + } + + public static boolean isPerfectSquareByUsingBinarySearch(long low, long high, long number) { + long check = (low + high) / 2L; + if (high < low) + return false; + if (number == check * check) { + return true; + } else if (number < check * check) { + high = check - 1L; + return isPerfectSquareByUsingBinarySearch(low, high, number); + } else { + low = check + 1L; + return isPerfectSquareByUsingBinarySearch(low, high, number); + } + } + + public static boolean isPerfectSquareByUsingNewtonMethod(long n) { + long x1 = n; + long x2 = 1L; + while (x1 > x2) { + x1 = (x1 + x2) / 2L; + x2 = n / x1; + } + return x1 == x2 && n % x1 == 0L; + } + + public static boolean isSquareNumberWithOptimization(long n) { + if (n < 0) + return false; + switch ((int) (n & 0xF)) { + case 0: case 1: case 4: case 9: + long tst = (long) Math.sqrt(n); + return tst * tst == n; + default: + return false; + } + } +} diff --git a/java-numbers-4/src/test/java/com/baeldung/perfectsquare/PerfectSquareUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/perfectsquare/PerfectSquareUnitTest.java new file mode 100644 index 0000000000..30ab95f963 --- /dev/null +++ b/java-numbers-4/src/test/java/com/baeldung/perfectsquare/PerfectSquareUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.perfectsquare; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class PerfectSquareUnitTest { + + @Test + public void test0xFFAssignedToInteger() { + long n = 18676209273604L; //‬ 18676209273604 = 43621598 * 43621598 + boolean expectedValue = true; + assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n)); + assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n)); + assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n)); + assertEquals(expectedValue, PerfectSquareUtil.isSquareNumberWithOptimization(n)); + } + + @Test + public void test0xFFAssignedToByte() { + long n = 549790047707L; // prime number + boolean expectedValue = false; + assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n)); + assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n)); + assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n)); + assertEquals(expectedValue, PerfectSquareUtil.isSquareNumberWithOptimization(n)); + } +}