Skip to content

Commit 80a52f1

Browse files
authored
Merge pull request vJechsmayr#166 from dhkamp/maths/find_prime_factor
Added prime factor algorithm
2 parents 9004884 + 256b207 commit 80a52f1

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎Maths/findPrimeFactors.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Returns the prime factors for the provided number.
2+
// Prime factors are all prime numbers which multiplied form the provided value.
3+
4+
function findPrimeFactors(num = 0) {
5+
if (num === 0) return [];
6+
7+
const primeFactors = [];
8+
let divisor = 2;
9+
10+
while (num >= Math.pow(divisor, 2)) {
11+
if (num % divisor === 0) {
12+
primeFactors.push(divisor);
13+
num /= divisor;
14+
} else {
15+
divisor++;
16+
}
17+
}
18+
19+
primeFactors.push(num);
20+
21+
return primeFactors;
22+
}
23+
24+
console.log(findPrimeFactors(100)); // prints [2,2,5,5];

0 commit comments

Comments
 (0)