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();
}
}
/*
* 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
- 21 Amicable numbers - Project Euler
- project euler 21 Amicable numbers
- Project Euler:Problem 21 Amicable numbers
- Project Euler Problem 21 Amicable numbers
- Matlab 编程 Project Euler Problem 21 Amicable numbers
- Euler: Amicable numbers
- PE 21 Amicable numbers
- (Problem 21)Amicable numbers
- Amicable numbers
- projecteuler---->problem=21----Amicable numbers
- No_21:Amicable numbers
- Amicable numbers -- Javascript 实现
- 【Project Euler】【Problem 2】Even Fibonacci numbers
- Project Euler:Problem 42 Coded triangle numbers
- Project Euler:Problem 44 Pentagon numbers
- Project Euler:Problem 55 Lychrel numbers
- Project Euler:Problem 61 Cyclical figurate numbers
- Project Euler:Problem 88 Product-sum numbers
- Android Activity 、 Window 、 View之间的关系
- 异常处理
- Unity3d 用TextAsset打包加密
- 如何避免FragmentTabHost切换Fragment时重复加载UI
- *LeetCode-Walls and Gates
- 21 Amicable numbers - Project Euler
- Winsock IO模型之select模型
- 在线文档
- JAVA面试宝典
- Valid Parentheses - Leetcode
- iphone屏幕尺寸
- struts2的核心和工作原理
- OpenCV经常会出现一些内存泄露问题
- 如何在unity3D里面引用dll