完数即指一个数的所有因数相加为它本身(如6=1+2+3:不包含6本身),求出10000以内的完数。
代码一:
public class PerfectNumber{
public static void main(String[] args){
for(int i=1;i<=10000;i++){
int factor=0
for(int j=1;j<=i/2;j++){
if(i%j==0){
factor+=j;
}
}
if(factor==i){
System.out.println(i);
}
}
}
}
代码二:
package com;
import java.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
t.aa();
}
public void aa(){
List<Integer> list = new ArrayList<Integer>();
long t1 = System.currentTimeMillis();
for(int i=2;i<100000;i++){
list.clear();
int temp = i;
for(int j=2;j<i;j++){
if(i % j == 0){
list.add(j);
}
}
int temp2 = 0;
for(int m=0;m<list.size();m++){
temp2 = temp2 + list.get(m);
}
if(temp == temp2+1){
System.out.println("temp="+temp);
long t2 = System.currentTimeMillis();
System.out.println("time=" + (t2 - t1));
}
}
}
}