简体   繁体   English

项目Euler#23(Java)。 我不知道怎么了。 答案是64

[英]Project Euler #23 (Java). I can't figure out what's wrong. Answer is off by 64

The problem is: 问题是:

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. 例如,适当除数为28的总和将为1 + 2 + 4 + 7 + 14 = 28,这意味着28是一个理想数。 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. 如果数字n的适当除数的总和小于n,则称其为n;如果该数字之和超过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. 因为12是最小的整数,所以1 + 2 + 3 + 4 + 6 = 16,可以写成两个整数的和的最小数字是24。通过数学分析,可以证明所有大于28123可以写为两个丰富数字的总和。 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. 找出所有不能写为两个丰富数字之和的正整数之和。

My method creates a list of abundant numbers below the limit, creates a list of numbers created by adding abundant numbers to each other, then find the numbers which do not appear on this list, and adds them to find their sum. 我的方法创建了一个低于限制的大量数字的列表,创建了一个通过相互添加大量数字而创建的数字列表,然后找到未出现在该列表中的数字,并将它们相加以求和。 The answer is supposed to be 4179871, while I get 4179935. I am off by 64 and I can't figure out why. 答案应该是4179871,而我得到4179935。我已经64岁了,我不知道为什么。 My code is: 我的代码是:

public static void main(String[] args) {

    int limit = 28124;
    int sum=0;
    int tempNum;
    int listSize;
    ArrayList<Integer> list = new ArrayList<Integer>();
    ArrayList<Integer> sumList = new ArrayList<Integer>();
    for (int i=0; i<limit; i++) {

        if (isAbundant(i)) {

            list.add(i);
        }
    }
    listSize = list.size();

    for (int i=0; i<listSize; i++) {

        for (int j=i+1; j<listSize; j++) {

            tempNum = list.get(i) + list.get(j);
            if (tempNum < limit) {
                sumList.add(tempNum);
            }
        }
    }

    for (int i=1; i<limit; i++) {

        if (sumList.contains(i) == false) {

            sum += i;
        }
    }
    System.out.println(sum);
}

public static boolean isAbundant(int n) {

    int sum=0;
    for (int i=1; i<n; i++) {

        if (n%i==0) {

            sum += i;
        }
    }
    if (sum>n) { return true; }
    else { return false; }
}

Any assistance is appreciated. 任何帮助表示赞赏。

The nested for loop should start with i instead of i+1 (otherwise you ignore sum with same indices): 嵌套的for循环应以i而不是i+1开头(否则,您将忽略具有相同索引的sum):

for (int i=0; i<listSize; i++) {

    for (int j=i; j<listSize; j++) {

        tempNum = list.get(i) + list.get(j);
        if (tempNum < limit) {
            sumList.add(tempNum);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM