-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeFactorer.java
More file actions
46 lines (41 loc) · 1.05 KB
/
PrimeFactorer.java
File metadata and controls
46 lines (41 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
public class PrimeFactorer {
public static ArrayList<Integer> generate(int n) {
ArrayList<Integer> res = new ArrayList();
boolean pf;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
pf = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
pf = false;
}
}
if (pf) {
res.add(i);
}
}
}
return res;
}
public static void main(String args[]) {
ArrayList<Integer> result = PrimeFactorer.generate(1);
ArrayList<Integer> answer = new ArrayList();
if (result.equals(answer)) {
System.out.println("generate(1) PASSES!");
} else {
System.out.println("generate(1) FAILS:");
System.out.println(result);
}
result = PrimeFactorer.generate(30);
answer.add(2);
answer.add(3);
answer.add(5);
if (result.equals(answer)) {
System.out.println("generate(30) PASSES!\n\n");
} else {
System.out.println("generate(30) FAILS:");
System.out.println(result);
}
}
}