forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeFactors.js
More file actions
Latest commit
20 lines (19 loc) · 460 Bytes
/
Copy pathPrimeFactors.js
File metadata and controls
20 lines (19 loc) · 460 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py
*/
exportconstPrimeFactors=(n)=>{
// input: n: int
// output: primeFactors: Array of all prime factors of n
constprimeFactors=[]
for(leti=2;i*i<=n;i++){
while(n%i===0){
primeFactors.push(i)
n=Math.floor(n/i)
}
}
if(n>1){
primeFactors.push(n)
}
returnprimeFactors
}