-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem12.js
46 lines (41 loc) · 958 Bytes
/
problem12.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
function* triangleNumbers() {
let triangle = 0;
for (let i = 1; ; i++) {
triangle += i
yield triangle
}
// return ((limit + 1) * (0.5) * (limit))
}
function determineFactors(triangleNo) {
const sqrt = Math.round(Math.sqrt(triangleNo))
let factors = new Set(), temp
for (let i = 1; i < sqrt; i++) {
if (triangleNo % i === 0) {
if (i === sqrt) {
factors.add(i)
continue
}
temp = triangleNo / i
factors.add(i)
factors.add(temp)
}
}
return factors
}
function problem12() {
const generator = triangleNumbers()
let value
let factors
do {
value = generator.next().value
factors = determineFactors(value)
}
while (factors.size < 500)
return value
}
const startTime = Date.now()
const number = problem12()
const endTime = Date.now()
console.log('number: ', number)
const runningTime = endTime - startTime
console.log(`Running time (s): ${runningTime / 1000}`)