Repository: TheAlgorithms/Java
Pull request #5917 (2024-10-20 14:52:56)

Added new method to check Perfect Square
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->
Added a new method to check Perfect Square using Pow() method. Also added test cases.
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5917?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 66.80%. Comparing base [(`b190cb7`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b190cb72def8d926e2f73d9f80cfd27c88ff3a3a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`0092d97`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0092d97ff3a825b3597e42764550a2765fd5331f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 143 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@              Coverage Diff              @@
##             master    #5917       +/-   ##
=============================================
+ Coverage     54.75%   66.80%   +12.05%     
- Complexity     3453     4503     +1050     
=============================================
  Files           538      611       +73     
  Lines         15583    16960     +1377     
  Branches       2961     3276      +315     
=============================================
+ Hits           8532    11330     +2798     
+ Misses         6694     5182     -1512     
- Partials        357      448       +91     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5917?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/PerfectSquare.java b/src/main/java/com/thealgorithms/maths/PerfectSquare.java
index fbc7a6f1..e9318bd7 100644
--- a/src/main/java/com/thealgorithms/maths/PerfectSquare.java
+++ b/src/main/java/com/thealgorithms/maths/PerfectSquare.java
@@ -18,4 +18,16 @@ public final class PerfectSquare {
         final int sqrt = (int) Math.sqrt(number);
         return sqrt * sqrt == number;
     }
+
+    /**
+     * Check if a number is perfect square or not
+     *
+     * @param number number to be checked
+     * @return {@code true} if {@code number} is perfect square, otherwise
+     * {@code false}
+     */
+    public static boolean isPerfectSquareUsingPow(long number) {
+        long a = (long) Math.pow(number, 1.0 / 2);
+        return a * a == number;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PerfectSquareTest.java b/src/test/java/com/thealgorithms/maths/PerfectSquareTest.java
index 08c96bc7..2bda5bfd 100644
--- a/src/test/java/com/thealgorithms/maths/PerfectSquareTest.java
+++ b/src/test/java/com/thealgorithms/maths/PerfectSquareTest.java
@@ -9,11 +9,13 @@ public class PerfectSquareTest {
     @ValueSource(ints = {0, 1, 2 * 2, 3 * 3, 4 * 4, 5 * 5, 6 * 6, 7 * 7, 8 * 8, 9 * 9, 10 * 10, 11 * 11, 123 * 123})
     void positiveTest(final int number) {
         Assertions.assertTrue(PerfectSquare.isPerfectSquare(number));
+        Assertions.assertTrue(PerfectSquare.isPerfectSquareUsingPow(number));
     }
 
     @ParameterizedTest
     @ValueSource(ints = {-1, -2, -3, -4, -5, -100, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 15, 17, 99, 101, 257, 999, 1001})
     void negativeTest(final int number) {
         Assertions.assertFalse(PerfectSquare.isPerfectSquare(number));
+        Assertions.assertFalse(PerfectSquare.isPerfectSquareUsingPow(number));
     }
 }