diff --git a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-23-non-abundant-sums.english.md b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-23-non-abundant-sums.english.md index a243b9e086..ee7e57b83f 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-23-non-abundant-sums.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-23-non-abundant-sums.english.md @@ -67,7 +67,40 @@ sumOfNonAbundantNumbers(28123);
```js -// solution required +function abundantCheck(number) { + let sum = 1; + + for (let i = 2; i <= Math.sqrt(number); i += 1) { + if(number % i === 0) { + sum += i + +(i !== Math.sqrt(number) && number / i); + } + } + return sum > number; +} + +function sumOfNonAbundantNumbers(n) { + let sum = 0; + const memo = {}; + let abundantList = []; + + // Function checkSum checks if num can be represented as a sum of numbers in the stack (array) + const checkSum = (num, stack, memo) => { + for (let i = 0; i < stack.length; i += 1) { + if ((num - stack[i]) in memo) return true; + } + return false; + }; + + for (let i = 1; i <= n; i += 1) { + if (abundantCheck(i)) { + abundantList.push(i); + memo[i] = 1; + } + if (checkSum(i, abundantList, memo)) continue; + sum += i; + } + return sum; +} ```