I noticed there wasn’t a solution for this one.
What is your hint or solution suggestion?
It basically generates new triangular numbers and counts its divisors up to root n. For each one, it adds 2 since there is also a factor above root n. When we reach the count, just return it. There might be a more efficient way, but this one is reasonable fast with large numbers.
/* 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 root n, 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
}
}
}
Challenge: Problem 12: Highly divisible triangular number
Link to the challenge: