so_brave 发表于 2012-03-19 17:41

Problem23

Problem23







Java代码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.

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.Java代码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;   
    }   
}

梦境照进现实 发表于 2012-03-19 20:10

谢谢分享

_Rayx 发表于 2012-08-15 15:37

面试题?还是什么?
页: [1]
查看完整版本: Problem23