File tree Expand file tree Collapse file tree 2 files changed +30
-16
lines changed
main/java/com/thealgorithms/maths
test/java/com/thealgorithms/maths Expand file tree Collapse file tree 2 files changed +30
-16
lines changed Original file line number Diff line number Diff line change 11package com .thealgorithms .maths ;
22
3+ import java .util .stream .IntStream ;
4+
35/**
46 * In number theory, the aliquot sum s(n) of a positive integer n is the sum of
57 * all proper divisors of n, that is, all divisors of n other than n itself. For
911 */
1012public class AliquotSum {
1113
12- public static void main (String [] args ) {
13- assert aliquotSum (1 ) == 0 ;
14- assert aliquotSum (6 ) == 6 ;
15- assert aliquotSum (15 ) == 9 ;
16- assert aliquotSum (19 ) == 1 ;
17- }
18-
1914 /**
20- * Finds the aliquot sum of an integer number
15+ * Finds the aliquot sum of an integer number.
2116 *
2217 * @param number a positive integer
2318 * @return aliquot sum of given {@code number}
2419 */
25- public static int aliquotSum (int number ) {
26- int sum = 0 ;
27- for (int i = 1 , limit = number / 2 ; i <= limit ; ++i ) {
28- if (number % i == 0 ) {
29- sum += i ;
30- }
31- }
32- return sum ;
20+ public static int getAliquotValue (int number ) {
21+ var sumWrapper = new Object () {
22+ int value = 0 ;
23+ };
24+
25+ IntStream .iterate (1 , i -> ++i )
26+ .limit (number / 2 )
27+ .filter (i -> number % i == 0 )
28+ .forEach (i -> sumWrapper .value += i );
29+
30+ return sumWrapper .value ;
3331 }
3432}
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .maths ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6+
7+ public class AliquotSumTest {
8+
9+ @ Test
10+ void testGetMaxValue () {
11+ assertEquals (0 , AliquotSum .getAliquotValue (1 ));
12+ assertEquals (6 , AliquotSum .getAliquotValue (6 ));
13+ assertEquals (9 , AliquotSum .getAliquotValue (15 ));
14+ assertEquals (1 , AliquotSum .getAliquotValue (19 ));
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments