/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Name: COS 126 Staff *
* Login: cos126 *
* Precept: P00 *
* *
* Description: Provides 3 functions related to aliquot sums. *
* *
* Dependencies: None. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public class Perfect {
// returns the aliquot sum of n
public static int aliquot(int n) {
int aliquotSum = 0;
// check (1, 2, ..., n/2) for factors of n
for (int i = 1; i <= n/2; i++)
if (n % i == 0) aliquotSum += i;
return aliquotSum;
}
// returns true if n is a perfect number, false otherwise
public static boolean isPerfect(int n) {
return n == aliquot(n);
}
// returns true if n is an amicable number, false otherwise
public static boolean isAmicable(int n) {
int aliquotN = aliquot(n);
return n != aliquotN && n == aliquot(aliquotN);
}
}