mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
@@ -18,28 +18,24 @@ public class AliquotSum {
|
||||
* @return aliquot sum of given {@code number}
|
||||
*/
|
||||
public static int getAliquotValue(int number) {
|
||||
var sumWrapper = new Object() {
|
||||
int value = 0;
|
||||
};
|
||||
var sumWrapper = new Object() { int value = 0; };
|
||||
|
||||
IntStream
|
||||
.iterate(1, i -> ++i)
|
||||
IntStream.iterate(1, i -> ++i)
|
||||
.limit(number / 2)
|
||||
.filter(i -> number % i == 0)
|
||||
.forEach(i -> sumWrapper.value += i);
|
||||
|
||||
return sumWrapper.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Function to calculate the aliquot sum of an integer number
|
||||
*
|
||||
* @param n a positive integer
|
||||
* @return aliquot sum of given {@code number}
|
||||
*/
|
||||
public static int getAliquotSum(int n) {
|
||||
if (n <= 0)
|
||||
return -1;
|
||||
if (n <= 0) return -1;
|
||||
int sum = 1;
|
||||
double root = Math.sqrt(n);
|
||||
/*
|
||||
@@ -56,9 +52,9 @@ public class AliquotSum {
|
||||
sum += i + n / i;
|
||||
}
|
||||
}
|
||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root from sum
|
||||
if (root == (int) root)
|
||||
sum -= root;
|
||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
||||
// from sum
|
||||
if (root == (int) root) sum -= root;
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user