forked from famzah/langs-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.java
56 lines (50 loc) · 1.16 KB
/
primes.java
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
47
48
49
50
51
52
53
54
55
56
import java.util.*;
import java.lang.Math;
class PrimeNumbersGenerator {
ArrayList<Integer> get_primes7(int n) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (n < 2) return res;
if (n == 2) {
res.add(2);
return res;
}
ArrayList<Integer> s = new ArrayList<Integer>();
for (int i = 3; i < n + 1; i += 2) {
s.add(i);
}
int mroot = (int)Math.sqrt(n);
int half = s.size();
int i = 0;
int m = 3;
while (m <= mroot) {
if (s.get(i) != 0) {
int j = (int)((m*m - 3)/2);
s.set(j, 0);
while (j < half) {
s.set(j, 0);
j += m;
}
}
i = i + 1;
m = 2*i + 3;
}
res.add(2);
for (int it = 0; it < s.size(); ++it) {
if (s.get(it) != 0) {
res.add(s.get(it));
}
}
return res;
}
}
class PrimeNumbersBenchmarkApp {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long periodTime = Long.parseLong(System.getenv("RUN_TIME"), 10) * 1000;
ArrayList<Integer> res;
while ((System.currentTimeMillis() - startTime) < periodTime) {
res = (new PrimeNumbersGenerator()).get_primes7(10000000);
System.out.format("Found %d prime numbers.\n", res.size());
}
}
}