-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem23.js
65 lines (58 loc) · 1.3 KB
/
problem23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function getDivisors(number) {
let divisors = new Set([1])
let sqrt = Math.round(Math.sqrt(number))
for (let i = 2; i <= sqrt; i++) {
if (number % i === 0) {
divisors.add(i)
divisors.add(number / i)
}
}
return divisors
}
function summation(divisors) {
let sum = 0
for (let div of divisors) {
sum += div
}
return sum
}
function problem23(limit) {
let abundant = []
let divisors = 0, sum = 0
// get the abundant integers
for (let i = 2; i <= limit; i++) {
divisors = getDivisors(i)
if (summation(divisors) > i) {
abundant.push(i)
}
}
// mark any integers that can be gotten
// from the sum of two abundant integers
let arr = new Array(limit)
for (let ab1 of abundant) {
for (let ab2 of abundant) {
if (ab2 < ab1) {
continue
}
if ((ab1 + ab2) <= limit) {
arr[ab1 + ab2] = true
}
else {
break
}
}
}
// sum the unmarked integers (which correspond to the array index)
for (let i = 1; i < limit; i++) {
if (!arr[i]) {
sum += i
}
}
return sum
}
const startTime = Date.now()
const number = problem23(28123)
const endTime = Date.now()
console.log('number: ', number)
const runningTime = endTime - startTime
console.log(`Running time (s): ${runningTime / 1000}`)