`
水木清华77
  • 浏览: 37047 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Problem23

阅读更多
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

package com.yao;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-3-18
 * Time: 下午12:50
 */
public class Problem23 {
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<Integer>();
      for(int i=1;i<=28123;i++){
        if(sum(i)>i)
        {
            list.add(i);
        }
      }
       Set<Integer> abundant2sum =new HashSet<Integer>();
       int size=list.size();
        int sum=0;
        for(int i=1;i<=28123;i++){
            sum+=i;
        }
      for(int i=0;i<size;i++)
          for(int j=i;j<size;j++){
              int k=list.get(i)+list.get(j);
              if(k<=28123){
                  if(!abundant2sum.contains(k)){
                      abundant2sum.add(k);
                      sum-=k;
                  }
              }

          }
        System.out.println(sum);

    }
    private static int sum(int n) {
        if(n==1)return 0;
        int sum=1;
        int middle=(int)Math.sqrt(n);
        for(int j=2;j<=middle;j++){
            if(n%j==0){
                int k=n/j;
                if(k==j)
                    sum+=j;
                else
                    sum+=(k+j);
            }
        }
        return sum;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics