The process of determining which prime numbers multiply together to produce the original number is known as “prime factorization.”
Algorithm
Example - What are the prime factors of 12?
We will start with the smallest prime number, which is 2, so let's see:
12 ÷ 2 = 6
Yes, it divided by two exactly. We've already taken the first step!
However, 6 is not a prime number, so we must proceed. Let's try number two again:
6 ÷ 2 = 3
Yes, that also worked. And because 3 is a prime number, we have the answer:
12 = 2 × 2 × 3
$ads={1}
JavaScript Code
function primeFactors(num) {
let result = [];
if (num <= 1) {
return result;
}
let i = 2;
while (num > 1) {
while (num % i == 0) {
result.push(i);
num = num / i;
}
i++;
}
return result;
}