此代码提供了64个应有的分数,我知道存在重复的问题,但是这些解决方案都无法解决我的问题。
https://projecteuler.net/problem=23
一个完美数是一个数字,它的适当除数之和等于该数字。例如,适当除数28的总和就是1 + 2 + 4 + 7 + 14 = 28,这意味着28是一个完美的数。
如果数字n的适当除数之和小于n,则称n为不足;如果数字n的适当除数超过n,则称其为丰富。
由于12是最小的丰富数,所以1 + 2 + 3 + 4 + 6 = 16,可以写成两个丰富的数之和的最小数是24。通过数学分析,可以看出所有大于28123的整数可以写为两个丰富数字的总和。但是,即使已知无法表示为两个丰富数字之和的最大数字小于该上限,也无法通过分析进一步减小该上限。
找到所有不能写为两个丰富数字之和的正整数之和。
const abundantNumbers = [];
let result = 0;
const isItAbundant = number => {
let divisorsSum = 1;
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
divisorsSum += i;
divisorsSum += number / i;
}
}
if (Math.sqrt(number) % 1 === 0) {
divisorsSum -= Math.sqrt(number);
}
return divisorsSum > number ? 1 : 0;
};
const populateAbundantNumbers = () => {
for (let i = 12; i < 28123; i++) {
if (isItAbundant(i)) {
abundantNumbers.push(i);
}
}
};
const arrayCanSumToX = numb => {
const length = abundantNumbers.length;
let low = 0;
let high = length;
while (low < high) {
if (abundantNumbers[low] + abundantNumbers[high] === numb) {
return true;
} else if (abundantNumbers[low] + abundantNumbers[high] < numb) {
low++;
} else {
high--;
}
}
return false;
};
const checkIfProductOfTwoAbundant = () => {
for (let i = 1; i < 28123; i++) {
if (!arrayCanSumToX(i)) {
result += i;
}
}
return result;
};
populateAbundantNumbers();
checkIfProductOfTwoAbundant();
// => 4179935
// Gives 64 higher than correct solution.