Naive Approach
In this method, we will use a JavaScript loop to iterate the possible factors and Math.pow() method to get the square root of the number. Instead of Math.pow() method, we can also use Math.sqrt() or i*i < n condition.
Example:
function prime(n) {
let result = [1,n];
for (let i = 2; i < Math.pow(n, 0.5); i++) {
if (n % i == 0) {
result.push(i);
result.push(n / i);
}
}
return result.sort((a, b) => a - b);
}
const num = 90;
console.log("Prime factors of " +
num + ": " + prime(num));
Output
Prime factors of 90: 1,2,3,5,6,9,10,15,18,30,45,90
JavaScript Program to Find All Divisors of a Number
In this article, we will demonstrate different approaches to writing a JavaScript Program to Find all Divisors of a Number. We will have an input number and print all the divisors of that number in the form of a resultant array.