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

目录
相关文章
|
Oracle 关系型数据库 Linux
Linux下 su命令与su - 命令有什么区别?
Linux下 su命令与su - 命令有什么区别?
320 0
vue+element Form键盘回车事件页面刷新解决
问题描述:如下代码所示,使用element-ui 中的el-form组件对table进行条件查询,当查询条件仅有一项时,使用@keyup.
1695 0
|
SQL 存储 分布式计算
CDP的Hive3系列之Hive Metastore介绍
CDP的Hive Metastore (HMS) 是一种服务,用于在后端 RDBMS(例如 MySQL 或 PostgreSQL)中存储与 Apache Hive 和其他服务相关的元数据。Impala、Spark、Hive 和其他服务共享元存储。与 HMS 的连接包括 HiveServer、Ranger 和代表 HDFS 的 NameNode。
2628 0
CDP的Hive3系列之Hive Metastore介绍
|
10月前
多层嵌套对象的解构赋值时,如果对象的属性名相同怎么办?
在多层嵌套对象的解构赋值中遇到属性名相同时,使用别名是一种有效的解决方法,它能够帮助我们准确地提取和使用对象中的数据,避免变量名冲突,提高代码的质量和可维护性。
165 7
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
python如何实现AI问答与举例
python如何实现AI问答与举例
231 0
|
存储 分布式计算 供应链
Spark在供应链核算中应用问题之调整Spark读取ODPS离线表分区大小如何解决
Spark在供应链核算中应用问题之调整Spark读取ODPS离线表分区大小如何解决
|
SQL 分布式计算 DataWorks
实时数仓 Hologres操作报错合集之如何解决报错:internal error: Queryis cancelled
实时数仓Hologres是阿里云推出的一款高性能、实时分析的数据库服务,专为大数据分析和复杂查询场景设计。使用Hologres,企业能够打破传统数据仓库的延迟瓶颈,实现数据到决策的无缝衔接,加速业务创新和响应速度。以下是Hologres产品的一些典型使用场景合集。
|
缓存 Web App开发 自然语言处理
关于解决chatGPT注册不了报错:chatGPT邮箱不支持
ChatGPT 开放了免费注册功能。然而,在用户创建过程中,一些人遇到了如下所示的提示信息:“Oops! The email you provided is not supported”,中文翻译为“糟糕,邮箱不支持”。
882 4
|
SQL JavaScript 关系型数据库
Sequelize操作MySQL基本用法1
Sequelize操作MySQL基本用法
|
人工智能 算法 Java
深度优先搜索(Depth-First Search,DFS)是一种用于遍历或搜索树或图的算法。
深度优先搜索(Depth-First Search,DFS)是一种用于遍历或搜索树或图的算法。