-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeFactor.java
More file actions
24 lines (21 loc) · 556 Bytes
/
Copy pathPrimeFactor.java
File metadata and controls
24 lines (21 loc) · 556 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package algorithm.module;
import java.util.ArrayList;
public class PrimeFactor {
public int[] primeFactors(int n) {
if (n <= 0) return new int[]{};
ArrayList<Integer> temp = new ArrayList<>();
int p = 2;
while (n > p * p) {
if (n % p == 0) {
temp.add(p);
n /= p;
} else {
p++;
}
}
temp.add(n);
return temp.stream()
.mapToInt(a -> a)
.toArray();
}
}