欧拉#23项目,在程序中找不到问题

Project Euler #23, can't find the issue in program

本文关键字:找不到 问题 程序 项目 欧拉      更新时间:2023-10-16

链接 : http://projecteuler.net/problem=23

完美数是其适当除数之和的数 正好等于数字。例如,适当的 28 的除数将是 1 + 2 + 4 + 7 + 14 = 28,这意味着 28 是一个完美的数字。

一个数 n 称为缺陷,如果其适当的除数之和为 小于 n,如果此总和超过 n,则称为丰度。

由于 12 是最小的丰度数,1 + 2 + 3 + 4 + 6 = 16,因此 可以写成两个丰度数之和的最小数 是24。通过数学分析,可以证明所有整数 大于 28123 可以写成两个丰度数之和。 但是,这个上限不能通过分析进一步降低 即使众所周知,最大的数字不能 表示为两个丰度数之和小于此限制。

求所有不能写为 的正整数的总和 两个丰度数的总和。


问题的答案是4179871,我的程序显示3797954。

首先,我做了一个函数,用 28124 以下的所有丰度数字填充数组 abundant[ ]。这工作得很好,因为我用谷歌搜索了大量的数字,它们与我的数组完全匹配。

其次,我

有另一个数组,所有数字 1-28123,我假设它们都"不能写成两个丰富数字的总和"。这些都写入数组保持 [ ]。

最后,我摆脱了可以写成两个丰富数字之和的数字,方法是将所有 abundant[ ] 中的数字与 abundant [ ] 中的所有数字相加,并将 hold[ ] 的值设置为 0。 (保持[丰富[0 到 n]+丰富[0 到 n]] = 0(将所有剩余的数字相加[ ],我只得到3797954

我知道这个程序不是很有效,因为它将所有丰富的数字添加到所有丰富的数字中,但它应该可以正常工作。怎么了?


#include <iostream>
#include <cmath>
using namespace std;
int hold[28124];
int abundant[7000]; //hold abundant numbers, there are only 6919 abundant numbers below 28123
bool abundance(int x){  //returns true if x is abundant
    int counter = 1;    //holds "proper divisors" of numbers, default by 1 because every int is divisible by 1
    for (int i = 2; i < sqrt(x); i++){   //finds all divisors 2 - sqrt(n)
        if (x % i == 0){
            counter += i;
            counter += x / i;
        }
    }
    int y = sqrt(x);   
    if (x % y == 0){   //adds sqrt(n) if its modulus to n is 0
            counter += sqrt(x);
        }
    if (counter > x){
        return true;
    } else {
        return false;
    }
}
int main()
{
    int counter = 0;
    for (int i = 0; i < 28124; i++){ //assumes every number cannot be written as the sum of two abundant numbers,
        hold[i] = i;                 //goes up to 28123 because "it can be shown that all integers greater
    }                                //than 28123 can be written as the sum of two abundant numbers." - project euler
    for (int j = 10; j < 28124; j++){ 
        if (abundance(j) == true){  //copies all abundant numbers up to 28123 to abundant[]
            abundant[counter] = j;
            counter++;
        }
    }
    for (int m = 0; m < counter; m++){  //adds all numbers in abundant[], with all numbers in abundant[]
        for (int n = 0; n < counter; n++){
            if (abundant[m]+abundant[n] < 28124){
                hold[abundant[m]+abundant[n]] = 0; //sum of the abundant numbers in hold[] is set to 0
            } //hold[] now holds all numbers that cannot be written as the sum of 2 abundant numbers
        }
    }
    int counter2 = 0;
    for (int x = 0; x < 28124; x++){
        counter2 += hold[x];
    }
    cout << counter2 << endl;
}

问题出在您的abundance函数中,特别是这部分:

int y = sqrt(x);   
if (x % y == 0){   //adds sqrt(n) if its modulus to n is 0
    counter += sqrt(x);
}

x % (int)sqrt(x) == 0并不意味着sqrt(x)是整数。一个简单的反例是 2。 sqrt(2)大约是1.414,或者只是1为整数。但2 % 1 == 0,即使它不是平方根。

因此,要修复您的代码,请将该部分更改为:

int y = sqrt(x);   
if (y * y == x){   //adds sqrt(n) if sqrt(n) is an integer
    counter += y;
}

你会得到正确的结果。

你可以看看这里显示的代码 - 如果只是因为它得到了正确的答案。例如,无需复制那里所做的工作,您就可以确认问题是出在丰富的数字生成器中还是在另一部分。它可能会帮助您找出出错的地方。

my_set = set([i for i in range(1, 28123)])
print ('original sum',sum(my_set))
my_list = list(my_set)
my_l1 =[]
for k in range(12, int(my_list[-1]/2+1)):
    a = 0
    for j in range(1, int(k/2+1)):
        if k%j == 0:
            a += j
        
    if a > k:
        my_l1.append(k)
        my_set.remove(k*2)
#Calculating the sum of all the numbers which can be written as the sum of two abundant numbers   
l = 0
my_l2 = set([])
for d in my_l1:
    l += 1
    k = l
    while k < len(my_l1):
        a_s = d + my_l1[k]
        my_l2.add(a_s)
        k += 1
my_set.difference_update(my_l2)
print ('sum of all abbundant numbers:',sum(my_set))

这是我的问题23欧拉项目的代码,这有什么问题?我现在不关心运行时,我只想要正确的答案。