mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
@ -1,5 +1,7 @@
|
|||||||
package com.thealgorithms.maths;
|
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
|
* 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
|
* 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 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
|
* @param number a positive integer
|
||||||
* @return aliquot sum of given {@code number}
|
* @return aliquot sum of given {@code number}
|
||||||
*/
|
*/
|
||||||
public static int aliquotSum(int number) {
|
public static int getAliquotValue(int number) {
|
||||||
int sum = 0;
|
var sumWrapper = new Object() {
|
||||||
for (int i = 1, limit = number / 2; i <= limit; ++i) {
|
int value = 0;
|
||||||
if (number % i == 0) {
|
};
|
||||||
sum += i;
|
|
||||||
}
|
IntStream.iterate(1, i -> ++i)
|
||||||
}
|
.limit(number / 2)
|
||||||
return sum;
|
.filter(i -> number % i == 0)
|
||||||
|
.forEach(i -> sumWrapper.value += i);
|
||||||
|
|
||||||
|
return sumWrapper.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
src/test/java/com/thealgorithms/maths/AliquotSumTest.java
Normal file
16
src/test/java/com/thealgorithms/maths/AliquotSumTest.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user