21 Amicable numbers - Project Euler

来源:互联网 发布:1.03k脱机源码 编辑:程序博客网 时间:2024/04/29 10:42
package xxx.xxx.xxx;
/*
 * Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
 * If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
 * For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
 * Evaluate the sum of all the amicable numbers under 10000.
 */
public class AmicableNumbers {

private void compute(){
int n = 1;
int amicableNumbersSum = 0;
while(n<10000){
if((this.properDivisorsSum(this.properDivisorsSum(n))==n)&&(this.properDivisorsSum(n)!=n)){
amicableNumbersSum +=  this.properDivisorsSum(n)+n;
System.out.println("n "+n+" this.properDivisorsSum(n) "+this.properDivisorsSum(n));
}
n++;
}
System.out.println(amicableNumbersSum/2);
System.out.println(properDivisorsSum(284));
}



private int properDivisorsSum(int n){
int sum = 0;
for(int i = 1; i<n; i++){
if(n%i==0){
sum+=i;
}
}
return sum;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AmicableNumbers amicableNumbers = new AmicableNumbers();
amicableNumbers.compute();
}


}
0 0
原创粉丝点击