Repository: TheAlgorithms/Java
Pull request #3027 (2022-04-20 22:59:13)

Code refactor for AliquotSum improvements
### **Describe your change:**

- [ ] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [x] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

See: https://github.com/TheAlgorithms/Java/issues/3026

<!-- Remove items that do not apply. 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.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AliquotSum.java b/src/main/java/com/thealgorithms/maths/AliquotSum.java
index 329bcf2f..7565d49c 100644
--- a/src/main/java/com/thealgorithms/maths/AliquotSum.java
+++ b/src/main/java/com/thealgorithms/maths/AliquotSum.java
@@ -1,5 +1,7 @@
 package com.thealgorithms.maths;
 
+import java.util.stream.IntStream;
+
 /**
  * In number theory, the aliquot sum s(n) of a positive integer n is the sum of
  * all proper divisors of n, that is, all divisors of n other than n itself. For
@@ -9,26 +11,22 @@ package com.thealgorithms.maths;
  */
 public class AliquotSum {
 
-    public static void main(String[] args) {
-        assert aliquotSum(1) == 0;
-        assert aliquotSum(6) == 6;
-        assert aliquotSum(15) == 9;
-        assert aliquotSum(19) == 1;
-    }
-
     /**
-     * Finds the aliquot sum of an integer number
+     * Finds the aliquot sum of an integer number.
      *
      * @param number a positive integer
      * @return aliquot sum of given {@code number}
      */
-    public static int aliquotSum(int number) {
-        int sum = 0;
-        for (int i = 1, limit = number / 2; i <= limit; ++i) {
-            if (number % i == 0) {
-                sum += i;
-            }
-        }
-        return sum;
+    public static int getAliquotValue(int number) {
+        var sumWrapper = new Object() {
+            int value = 0;
+        };
+
+        IntStream.iterate(1, i -> ++i)
+                .limit(number / 2)
+                .filter(i -> number % i == 0)
+                .forEach(i -> sumWrapper.value += i);
+
+        return sumWrapper.value;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AliquotSumTest.java b/src/test/java/com/thealgorithms/maths/AliquotSumTest.java
new file mode 100644
index 00000000..eca726ae
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/AliquotSumTest.java
@@ -0,0 +1,16 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class AliquotSumTest {
+
+    @Test
+    void testGetMaxValue() {
+        assertEquals(0, AliquotSum.getAliquotValue(1));
+        assertEquals(6, AliquotSum.getAliquotValue(6));
+        assertEquals(9, AliquotSum.getAliquotValue(15));
+        assertEquals(1, AliquotSum.getAliquotValue(19));
+    }
+}