I have some approach, and partly implemented it, but I am struggling for some reason with one of the last steps.
Couldn’t find any maths for now for some better approach to this one.
Question at the lower part of code.
const sumOfEveryIntegerBelowInclusive = (num) => {
let result = 0;
for (let i = 1; i <= num; i++) {
result += i;
}
return result;
}
const sumOfProperDivisors = (num) => {
let sum = 0;
let upperLimit = num / 2;
//TO OPT >>> can use sqrt as condition and add divisors in pairs
for (let i = 1; i <= upperLimit; i++) {
if (num % i === 0) {
sum += i;
}
}
return sum;
}
const isAbundant = (num) => {
//The smallest abundant number not divisible by 2 or by 3 is 5391411025
if (num % 2 !== 0 && num % 3 !== 0) {return false;}
return sumOfProperDivisors(num) > num ? true : false
}
const abundantBelowNumberInclusive = (num) => {
let abundants = [];
// 12 is the smallest abundant number
for (let i = 12; i <= num; i++) {
if (isAbundant(i)) {
abundants.push(i);
}
}
return abundants;
}
// console.log(abundantBelowNumberInclusive(100));
// [12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100]
// check oeis sequence here https://en.wikipedia.org/wiki/Abundant_number
function sumOfNonAbundantNumbers(n) {
/*
the smallest number that can be written as the sum of two abundant numbers is 24
thus
EDGE case 1
*/
if (n <= 23) {
return sumOfEveryIntegerBelowInclusive(n);
}
/*
all integers greater than 28123 can be written as the sum of two abundant numbers
thus
EDGE case 2
*/
else if (n > 28123) {
return sumOfNonAbundantNumbers(28123);
}
/*
need to perform calculations
only for n in range (24, 28123)
*/
else {
//can get result by substraction:
// sum of all integers <= n
// minus
// sum of all integers <= n which CAN BE represented as sum of two abundants
//generate sum
const sumOfAll = sumOfEveryIntegerBelowInclusive(n);
//generate abundants
//max possible abundant we need for sum >>> n - 12
const abundantsForSums = abundantBelowNumberInclusive(n - 12);
//???????????????QUESTION
//HOW do I go about generating sums which I need
//NO IDEA how to do this efficiently
}
return n;
}
// console.log(sumOfNonAbundantNumbers(28123));