GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — merge: added Problem 23 to Project Euler (#803) · Algorithms-Learn/JavaScript@b7f1a72

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Project-Euler
    • Problem023.js
    • test
      • Problem023.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Project-Euler/Problem023.js‎

Copy file name to clipboard
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Problem 23 - Non-Abundant Sums
3+
*
4+
* @see {@link https://projecteuler.net/problem=23}
5+
*
6+
* 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.
7+
*
8+
* 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.
9+
*
10+
* 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.
11+
*
12+
* Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
13+
*
14+
*/
15+
16+
/**
17+
* 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.
18+
* @param {number} [n = 28123]
19+
* @returns {number}
20+
*/
21+
22+
function sumOfNonAbundantNumbers (n = 28123) {
23+
const abundantNumbers = [] // array to store the abundant numbers
24+
const sumOfAbundantNumbers = {} // instead of an array, checking an object takes way less time. sets may be used as well.
25+
let sum = 0
26+
27+
for (let i = 1; i <= n; i++) {
28+
if (isAbundant(i)) {
29+
abundantNumbers.push(i) // collect the abundant numbers
30+
abundantNumbers.forEach(num => { // collect their sums
31+
const sum = num + i
32+
sumOfAbundantNumbers[sum] = true
33+
})
34+
}
35+
}
36+
37+
for (let i = 1; i <= n; i++) {
38+
if (!sumOfAbundantNumbers[i]) { // if the number is not found in the list of sums, then it is added
39+
sum += i
40+
}
41+
}
42+
43+
return sum
44+
}
45+
46+
/**
47+
* generates the divisors of the number and checks if it is abundant
48+
* @param {number} number
49+
* @returns {bool}
50+
*/
51+
52+
function isAbundant (number) {
53+
let sum = 0
54+
for (let i = 1; i <= number / 2; i++) {
55+
if (number % i === 0) { // generate divisors
56+
sum += i // calculate their sums
57+
}
58+
}
59+
return sum > number
60+
}
61+
62+
export { sumOfNonAbundantNumbers }
Collapse file

‎Project-Euler/test/Problem023.test.js‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { sumOfNonAbundantNumbers } from '../Problem023'
2+
3+
describe('Check Problem 23 - Non-Abundant Sums', () => {
4+
it('Sum of all positive integers <= 10000 which cannot be written as the sum of two abundant numbers', () => {
5+
expect(sumOfNonAbundantNumbers(10000)).toBe(3731004)
6+
})
7+
8+
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => {
9+
expect(sumOfNonAbundantNumbers(15000)).toBe(4039939)
10+
})
11+
12+
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => {
13+
expect(sumOfNonAbundantNumbers(20000)).toBe(4159710)
14+
})
15+
16+
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => {
17+
expect(sumOfNonAbundantNumbers(28123)).toBe(4179871)
18+
})
19+
20+
it('Sum of all positive integers <= n which cannot be written as the sum of two abundant numbers', () => {
21+
expect(sumOfNonAbundantNumbers(30000)).toBe(4179871)
22+
})
23+
})

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.