mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
21 lines
460 B
JavaScript
21 lines
460 B
JavaScript
/*
|
|
Modified from:
|
|
https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py
|
|
*/
|
|
|
|
export const PrimeFactors = (n) => {
|
|
// input: n: int
|
|
// output: primeFactors: Array of all prime factors of n
|
|
const primeFactors = []
|
|
for (let i = 2; i * i <= n; i++) {
|
|
while (n % i === 0) {
|
|
primeFactors.push(i)
|
|
n = Math.floor(n / i)
|
|
}
|
|
}
|
|
if (n > 1) {
|
|
primeFactors.push(n)
|
|
}
|
|
return primeFactors
|
|
}
|