mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
merge: fix: optimize PrimeFactors (#823)
This commit is contained in:
@ -7,14 +7,12 @@ export const PrimeFactors = (n) => {
|
||||
// input: n: int
|
||||
// output: primeFactors: Array of all prime factors of n
|
||||
const primeFactors = []
|
||||
for (let i = 2; i <= n; i++) {
|
||||
if (n % i === 0) {
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user