首页 > 代码库 > java每日小算法(9)

java每日小算法(9)

/*【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完数。   */
package test;
import java.util.ArrayList;
import java.util.List;
public class test {
     public static boolean overnum(int number) {
         List<Integer> list = new ArrayList<Integer>();
         list.add(1);
         boolean flag = false;
         int tem = number / 2;
         int sum = 0;
         int compare = number;
         for(int i = 2; i <= tem; i++)
         {
            if(number == 1)
                break;
             if(number % i == 0)
                 {
                     list.add(i);
                 } 
         }
         for(int i = 0; i < list.size(); i++)
             sum = sum + list.get(i);
         if(compare == sum)
             flag = true;
         return flag;
     }
          
      
 public static void main(String[] args) {
     long a = System.currentTimeMillis();
     for(int i = 0; i < 1000; i++)
         if(overnum(i))
     System.out.println(i);
     System.out.println(System.currentTimeMillis() - a);
 }
}