-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
added Problem 23 to Project Euler #803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Problem 23 - Non-Abundant Sums | ||
* | ||
* @see {@link https://projecteuler.net/problem=23} | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
/** | ||
* collect the abundant numbers, generate and store their sums with each other, and check for numbers not in the llst of sums, adds them and returns their sum. | ||
* @param {number} [n = 28123] | ||
* @returns {number} | ||
*/ | ||
|
||
function sumOfNonAbundantNumbers (n = 28123) { | ||
const abundantNumbers = [] // array to store the abundant numbers | ||
const sumOfAbundantNumbers = {} // instead of an array, checking an object takes way less time. sets may be used as well. | ||
let sum = 0 | ||
|
||
for (let i = 1; i <= n; i++) { | ||
if (isAbundant(i)) { | ||
abundantNumbers.push(i) // collect the abundant numbers | ||
abundantNumbers.forEach(num => { // collect their sums | ||
const sum = num + i | ||
sumOfAbundantNumbers[sum] = true | ||
}) | ||
} | ||
} | ||
|
||
for (let i = 1; i <= n; i++) { | ||
if (!sumOfAbundantNumbers[i]) { // if the number is not found in the list of sums, then it is added | ||
sum += i | ||
} | ||
} | ||
|
||
return sum | ||
} | ||
|
||
/** | ||
* generates the divisors of the number and checks if it is abundant | ||
* @param {number} number | ||
* @returns {bool} | ||
*/ | ||
|
||
function isAbundant (number) { | ||
let sum = 0 | ||
for (let i = 1; i <= number / 2; i++) { | ||
if (number % i === 0) { // generate divisors | ||
sum += i // calculate their sums | ||
} | ||
} | ||
return sum > number | ||
} | ||
|
||
export { sumOfNonAbundantNumbers } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { sumOfNonAbundantNumbers } from '../Problem023' | ||
|
||
describe('Check Problem 23 - Non-Abundant Sums', () => { | ||
it('Sum of all positive integers <= 10000 which cannot be written as the sum of two abundant numbers', () => { | ||
expect(sumOfNonAbundantNumbers(10000)).toBe(3731004) | ||
}) | ||
|
||
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => { | ||
expect(sumOfNonAbundantNumbers(15000)).toBe(4039939) | ||
}) | ||
|
||
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => { | ||
expect(sumOfNonAbundantNumbers(20000)).toBe(4159710) | ||
}) | ||
|
||
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => { | ||
expect(sumOfNonAbundantNumbers(28123)).toBe(4179871) | ||
}) | ||
|
||
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => { | ||
expect(sumOfNonAbundantNumbers(30000)).toBe(4179871) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.