Skip to content

Commit e1f7bee

Browse files
author
Star-325
committed
merge: fix: optimize PrimeFactors (#823)
1 parent 5fafcd6 commit e1f7bee

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Maths/PrimeFactors.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ export const PrimeFactors = (n) => {
77
// input: n: int
88
// output: primeFactors: Array of all prime factors of n
99
const primeFactors = []
10-
for (let i = 2; i <= n; i++) {
11-
if (n % i === 0) {
12-
while (n % i === 0) {
13-
primeFactors.push(i)
14-
n = Math.floor(n / i)
15-
}
10+
for (let i = 2; i * i <= n; i++) {
11+
while (n % i === 0) {
12+
primeFactors.push(i)
13+
n = Math.floor(n / i)
1614
}
1715
}
1816
if (n > 1) {

0 commit comments

Comments
 (0)