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

Problem21

阅读更多
Problem 21
05 July 2002

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a  b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000

package com.yao;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: shuimuqinghua77
 * Date: 12-3-11
 * Time: 下午7:55
 */
public class Problem21 {

    public static void main(String[] args) {
         int top=10000;
         int  result=0;
        Set<Integer> invalid=new HashSet<Integer>();
        long start=System.currentTimeMillis();
         for(int i=2;i<top;i++){
             if(invalid.contains(i))
                 continue;
             int sum=sum(i);
             if(sum<=top&&sum!=i&&i==sum(sum)){
                 invalid.add(sum);
                 result+=(sum+i);
                 System.out.println("("+i+","+sum+")");
             }

         }
        long end=System.currentTimeMillis();
        System.out.println(end-start);
        System.out.println(result);
    }

    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;
    }
}
分享到:
评论

相关推荐

    计算机网络第六版答案

    21. The maximum emission rate is 500 packets/sec and the maximum transmission rate is 350 packets/sec. The corresponding traffic intensity is 500/350 =1.43 &gt; 1. Loss will eventually occur for each ...

    MySQL数据库考试试题.docx

    21. 删除视图:DROP VIEW 语句(Problem 21) DROP VIEW 语句用于删除一个视图。 22. 修改表结构:ALTER TABLE 语句(Problem 22) ALTER TABLE 语句用于修改表的结构,例如添加或删除列。 23. 全文本搜索:...

    Computer-Based.Problem.Solving.Process

    Title: Computer-Based Problem Solving Process Author: Teodor Rus Length: 350 pages ...Chapter 21. Efficiency of Batch Operating Systems Chapter 22. Convenience of the BOS Chapter 23. Real-Time Systems

    谐波激励下变压器结构件杂散损耗的模拟与验证.pdf

    基于TEAM (testing electromagnetic Problem 21 基准模型,从实验和仿真计算两方面系统地研究 多谐波激励下变压器结构件杂散损耗建模与验证方法。考虑 线圈涡流损耗的影响,提出一种基于实验更准确地确定结构 件杂散...

    C#,21根火柴棍问题(21 Matchticks Problem)的算法与源代码

    C#,21根火柴棍问题(21 Matchticks Problem)的算法与源代码 21根火柴棍问题是西方经典游戏之一。 给定21根火柴,2个人A和B(比如:分别是计算机和用户)。 每个人一次可以挑选 1-- 4 根火柴。 被迫挑最后一根...

    改进的解析法计算薄钢板的涡流损耗

    例如,在对TEAM Problem 21 (Model B) 进行计算时,可以采用以下形式的双重傅立叶级数逼近: \[ B_z(x,y) = \sum_{m=1}^{\infty} \sum_{n=1}^{\infty} A_{mn} \cos(u_m \pi x / a) \cos(v_n \pi y / b) \] 其中,$...

    八年级下册英语词汇测试.doc

    21. 〔用 手 或 器 具 〕击 ; 打 - hit 22. 立即;马上 - immediately 23. 陷入;参与 - get involved 24. 绷带 - bandage 25. 生病的;有病的 - sick 26. 膝;膝盖 - knee 27. 鼻出血 - nosebleed 28. 呼吸 - ...

    Artificial Intelligence and Problem Solving

    1 Introduction......Page 21 --- Problem Solving & Traditional AI Problems......Page 27 POLyA’s five sTePs fOr PrOBLem sOLvinG......Page 28 PrOBLem-sOLvinG Techniques......Page 33 humAn WinDOW.........

    Problem Solving in Data Structures & Algorithms Using Java

    Title: Problem Solving in Data Structures & Algorithms Using Java: The Ultimate Guide to Programming Author: Hemant Jain Length: 436 pages Edition: First Edition Language: English Publisher: ...

    0-1-knapsack-problem-master (21).zip

    【标题】"0-1-knapsack-problem-master (21).zip" 提到的是一个与优化问题相关的项目,具体来说是0-1背包问题的实现。在计算机科学和运筹学中,0-1背包问题是一个经典的组合优化问题,它的目标是在容量有限的背包里...

    0-1-knapsack-problem-master (21)c.zip

    在本压缩包"0-1-knapsack-problem-master (21)c.zip"中,我们可以预见到它包含了用C语言实现的0-1背包问题解决方案。 0-1 背包问题的定义如下:假设我们有一组物品,每个物品都有一个重量和价值,我们需要选择一些...

    ASP.NET Core Recipes A Problem-Solution Approach(2nd) epub

    ASP.NET Core Recipes A Problem-Solution Approach(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Problem.Solving.in.Data.Structures.and.Algorithms.Using.Cplusplus.epub

    Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This is the skill which tech companies like Google, Amazon, Microsoft, Adobe and many others ...

    JEDEC JESD671D:2018 Device Quality Problem Analysis and Corrective Action Resolution Methodology - 完整英文电子版(21页).pdf

    "JEDEC JESD671D:2018 Device Quality Problem Analysis and Corrective Action Resolution Methodology" 本资源是JEDEC(Joint Electron Devices Engineering Council,电子器件工程理事会)发布的一份标准文档,...

    APIO2015 Problemset

    ### APIO2015 Problemset - Bali Sculptures #### 问题背景 在亚洲太平洋信息学奥林匹克(Asia-Pacific Informatics Olympiad,简称APIO)2015年的竞赛中,有一道名为“Bali Sculptures”的题目。这道题目的背景...

    JEDEC JESD671D:2018 Device Quality Problem Analysis and Correcti

    JEDEC JESD671D:2018 Device Quality Problem Analysis and Corrective Action Resolution Methodology - 完整英文电子版(21页).zip

    0-1-knapsack-problem-master (22)c.zip

    压缩包中的 "0-1-knapsack-problem-master (21)c.zip" 文件可能是误写或者重复,正常情况下应该只有一个 "0-1-knapsack-problem-master (22)c.zip" 文件,其中包含源代码、测试数据、编译脚本等资源。解压后,我们...

    大型电力变压器的电磁场和涡流损耗的计算

    为了验证所提出的方法,研究人员选择了Team Problem 21-A作为磁场测试模型,并对模型的测量损失与计算损失进行了对比分析。结果表明,计算出的磁通量密度Bx与实际测量值非常吻合。 此外,本文还提供了大型电力...

Global site tag (gtag.js) - Google Analytics