Repository: TheAlgorithms/Java
Pull request #5919 (2024-10-20 19:26:30)

Added another method to check Pronic number
<!--
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 another method to check Pronic number using square root of number to find divisor. 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/5919?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 [(`4994bff`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/4994bffce16314d0e663b41717d7e6fc59ecbfa7?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    #5919       +/-   ##
=============================================
+ Coverage     54.75%   66.80%   +12.05%     
- Complexity     3453     4504     +1051     
=============================================
  Files           538      611       +73     
  Lines         15583    16962     +1379     
  Branches       2961     3277      +316     
=============================================
+ Hits           8532    11332     +2800     
+ 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/5919?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/PronicNumber.java b/src/main/java/com/thealgorithms/maths/PronicNumber.java
index 4891cf3c..c2ad2a81 100644
--- a/src/main/java/com/thealgorithms/maths/PronicNumber.java
+++ b/src/main/java/com/thealgorithms/maths/PronicNumber.java
@@ -21,6 +21,9 @@ public final class PronicNumber {
      * @return true if input number is a pronic number, false otherwise
      */
     static boolean isPronic(int inputNumber) {
+        if (inputNumber == 0) {
+            return true;
+        }
         // Iterating from 0 to input_number
         for (int i = 0; i <= inputNumber; i++) {
             // Checking if product of i and (i+1) is equals input_number
@@ -34,4 +37,15 @@ public final class PronicNumber {
         // equals input_number
         return false;
     }
+
+    /**
+     * This method checks if the given number is pronic number or non-pronic number using square root of number for finding divisors
+     *
+     * @param number Integer value which is to be checked if is a pronic number or not
+     * @return true if input number is a pronic number, false otherwise
+     */
+    public static boolean isPronicNumber(int number) {
+        int squareRoot = (int) Math.sqrt(number); // finding just smaller divisor of the number than its square root.
+        return squareRoot * (squareRoot + 1) == number;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PronicNumberTest.java b/src/test/java/com/thealgorithms/maths/PronicNumberTest.java
index 5a31981b..8bf2b185 100644
--- a/src/test/java/com/thealgorithms/maths/PronicNumberTest.java
+++ b/src/test/java/com/thealgorithms/maths/PronicNumberTest.java
@@ -1,33 +1,22 @@
 package com.thealgorithms.maths;
 
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 public class PronicNumberTest {
 
-    @Test
-    void testForPronicNumber() {
-        // given
-        int number = 30;
-
-        // when
-        boolean result = PronicNumber.isPronic(number);
-
-        // then
-        assertTrue(result);
+    @ParameterizedTest
+    @ValueSource(ints = {0, 2, 6, 12, 20, 30, 42, 110, 272, 380, 420, 1260, 2550})
+    void testForPronicNumber(final int number) {
+        Assertions.assertTrue(PronicNumber.isPronic(number));
+        Assertions.assertTrue(PronicNumber.isPronicNumber(number));
     }
 
-    @Test
-    void testForNonPronicNumber() {
-        // given
-        int number = 21;
-
-        // when
-        boolean result = PronicNumber.isPronic(number);
-
-        // then
-        assertFalse(result);
+    @ParameterizedTest
+    @ValueSource(ints = {1, 4, 21, 36, 150, 2500})
+    void testForNonPronicNumber(final int number) {
+        Assertions.assertFalse(PronicNumber.isPronic(number));
+        Assertions.assertFalse(PronicNumber.isPronicNumber(number));
     }
 }