Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
raklaptudirm merged 3 commits into TheAlgorithms:master from byt3h3ad:master
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Project-Euler/Problem023.js
View file Open in desktop
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 }
23 changes: 23 additions & 0 deletions Project-Euler/test/Problem023.test.js
View file Open in desktop
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)
})
})

AltStyle によって変換されたページ (->オリジナル) /