We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3723322 commit 09eebc4Copy full SHA for 09eebc4
Maths/PrimeFactors.js
@@ -7,12 +7,10 @@ export const PrimeFactors = (n) => {
7
// input: n: int
8
// output: primeFactors: Array of all prime factors of n
9
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
- }
+ for (let i = 2; i * i <= n; i++) {
+ while (n % i === 0) {
+ primeFactors.push(i)
+ n = Math.floor(n / i)
16
}
17
18
if (n > 1) {
0 commit comments