Problem23

简介:

1.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.  
2.  
3.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.  
4.  
5.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.  
6.  
7.Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.  


1.package com.yao;  
2.import java.util.ArrayList;  
3.import java.util.HashSet;  
4.import java.util.List;  
5.import java.util.Set;  
6.  
7./** 
8. * Created by IntelliJ IDEA. 
9. * User: Administrator 
10. * Date: 12-3-18 
11. * Time: 下午12:50 
12. */  
13.public class Problem23 {  
14.    public static void main(String[] args) {  
15.        List<Integer> list=new ArrayList<Integer>();  
16.      for(int i=1;i<=28123;i++){  
17.        if(sum(i)>i)  
18.        {  
19.            list.add(i);  
20.        }  
21.      }  
22.       Set<Integer> abundant2sum =new HashSet<Integer>();  
23.       int size=list.size();  
24.        int sum=0;  
25.        for(int i=1;i<=28123;i++){  
26.            sum+=i;  
27.        }  
28.      for(int i=0;i<size;i++)  
29.          for(int j=i;j<size;j++){  
30.              int k=list.get(i)+list.get(j);  
31.              if(k<=28123){  
32.                  if(!abundant2sum.contains(k)){  
33.                      abundant2sum.add(k);  
34.                      sum-=k;  
35.                  }  
36.              }  
37.  
38.          }  
39.        System.out.println(sum);  
40.  
41.    }  
42.    private static int sum(int n) {  
43.        if(n==1)return 0;  
44.        int sum=1;  
45.        int middle=(int)Math.sqrt(n);  
46.        for(int j=2;j<=middle;j++){  
47.            if(n%j==0){  
48.                int k=n/j;  
49.                if(k==j)  
50.                    sum+=j;  
51.                else  
52.                    sum+=(k+j);  
53.            }  
54.        }  
55.        return sum;  
56.    }  
57.}  

目录
相关文章
|
6月前
|
数据挖掘
|
Go C++
P1001 A+B Problem
P1001 A+B Problem
112 0
Divisibility Problem
Divisibility Problem
151 0
|
Java
|
算法框架/工具