-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path012-highly_divisibly_triangular_number.js
39 lines (30 loc) · 1.14 KB
/
012-highly_divisibly_triangular_number.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
/* Solution Function */
let divisibleTriangleNumber = (n) => {
/* For every divisior below root n, there is also one above root n */
let currentTriangular = 0
let count = 0
while(true){
/* Generate a new triangular number */
count = count + 1
currentTriangular = currentTriangular + count
/* Look for divisors between 2 and square root n, if we find one
there is also one above, so add 2 to the divisor count */
let divisorCount = 0
let i
for(i = 1; i < Math.sqrt(currentTriangular); i ++){
if(currentTriangular % i === 0){
divisorCount = divisorCount + 2;
}
}
if(Number.isInteger(Math.sqrt(currentTriangular))){
divisorCount = divisorCount + 1
}
console.log('Generated Triangular ' + currentTriangular + ' with ' + divisorCount + ' divisors.')
/* If number of divisors exceed n, then return the triangular number */
if(divisorCount > n){
return currentTriangular
}
}
}
/* Check Solution */
console.log('Result is ' + divisibleTriangleNumber(500))