Home > Enterprise >  Project Euler #23. Javascript. Why am I getting this error STATUS_BREAKPOINT?
Project Euler #23. Javascript. Why am I getting this error STATUS_BREAKPOINT?

Time:12-02

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 . 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 . 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 . Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

I've been trying to solve this problem using JavaScript, but I keep getting the error mentioned above and I haven't been able to figure out why. I suspect it's because of the loops, but none of them seems to go on to infinity.

Here's my code:

'use strict';

const limit = 28123;

function findProperDivisors(n) {
    let divisors = [];
    for (let i = n-1; i != 0; i--) {
        if (n % i === 0) {
            divisors.push(i);
        }
    }
    return divisors;
}

function sumDivisors(arr) {
    return arr.reduce((sum, curr) => sum   curr);
}

function isAbundunt(n) {
    return sumDivisors(findProperDivisors(n)) > n;
}

function equalsToSumOfTwoAbundants(n) {
    let divisors = findProperDivisors(n);
    for (let i = 0; i < divisors.length; i  ) {
        for (let j = 0; j < divisors.length; j  ) {
            if (isAbundunt(divisors[i]) && isAbundunt(divisors[j]) && (divisors[i]   divisors[j] === n)) {
                return true;
            }
        }
    }
    return false;
}

console.log(equalsToSumOfTwoAbundants(24));

let sum = 0;
// AFTER THIS LINE MY CODE BREAKS. Error code: STATUS_BREAKPOINT
for (let i = 0; i < limit; i  ) {
    if (!equalsToSumOfTwoAbundants(i)) {
        sum  = i;
    }
}

CodePudding user response:

346398923

Beside some funky larger parts, you should start with looping from one instead of zero,

for (let i = 1; i < l; i  ) {

because this generates later by calling findProperDivisors(0) an infinite loop.

for (let i = n - 1; i !== 0; i--) {

The loop has simply the wrong condition, this does not work with starting values below zero.

Some other hints:

Prevent iterating over all indices of divisors, take the abundant values only and add the values for a check, like this, for example:

function equalsToSumOfTwoAbundants(n) {
    const
        divisors = findProperDivisors(n).filter(isAbundant);

    for (let i = 0; i < divisors.length; i  ) {
        for (let j = 0; j < divisors.length; j  ) {
            if (divisors[i]   divisors[j] === n) return true;
        }
    }
    return false;
}

Another upcoming minor error is the missing startValue of reduce by having empty arrays.

function sumDivisors(arr) {
    return arr.reduce((sum, curr) => sum   curr, 0);
}
  • Related