-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle-numbers-12.js
More file actions
34 lines (31 loc) · 990 Bytes
/
Copy pathtriangle-numbers-12.js
File metadata and controls
34 lines (31 loc) · 990 Bytes
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
let triNum = 1;
let iterator = 2;
let divisorCount = 0;
while(true) {
computeDivisors(triNum)
if(divisorCount < 501) {
//add iterator to triNum to create the next triangle number (1, 3, 6, 10, 15, 21, 28...)
triNum += iterator;
//increase the iterator to use the next natural number
iterator ++;
}
else {
console.log(`triNum: ${triNum}, divisors: ${divisorCount}`)
break;
}
//if divisorCount is less than 501, it needs to be reset to test the next number
divisorCount = 0
}
function computeDivisors(dividend) {
let upperLimit = dividend
//for each number below the largest divisor
for(i = 1; i < upperLimit; i++) {
//if it divides evenly
if (dividend % i == 0) {
//update the upper limit of possible remaining divisors as the quotient of the dividend and the newest sub-meridian divisor
upperLimit = dividend / i
//update the tally by two because divisors come in pairs
divisorCount += 2
}
}
}