免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Java Problem23
最近访问板块 发新帖
查看: 1746 | 回复: 2
打印 上一主题 下一主题

Problem23 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-19 17:41 |只看该作者 |倒序浏览
Problem23








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

  8. 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.

  9. 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.

  10. 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.

  11. Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
复制代码
Java代码
  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. }  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-19 20:10 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-08-15 15:37 |只看该作者
面试题?还是什么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP