What is your hint or solution suggestion?
I recently submitted a solution, but I realized the solution would be far faster using time complexity O(sqrt(n))
. This is because 50% of factors are less than or equal to sqrt(n). The lower half of factors are found, then the upper half is mapped by performing n/i
for each factor.
Solution 3
function factors(num) {
var arr1 = Array
.from(Array(Math.ceil(Math.sqrt(num + 1))), (_, i) => i)
.filter(i => num % i === 0)
var arr2 = arr1.concat(arr1.map(i=>num/i).reverse())
var arr3 = [...new Set(arr2)];
return arr3
}
Challenge: Factors of an integer
Link to the challenge: