-
Notifications
You must be signed in to change notification settings - Fork 33
/
primes-alt.java
98 lines (84 loc) · 1.81 KB
/
primes-alt.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.*;
import java.lang.Math;
class PrimeNumbersGenerator {
IntList get_primes7(int n) {
IntList res = new IntList();
if (n < 2) return res;
if (n == 2) {
res.add(2);
return res;
}
IntList s = new IntList();
for (int i = 3; i <= n; 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.data.length; ++it) {
if (s.data[it] != 0) {
res.add(s.data[it]);
}
}
return res;
}
}
/*
* Variable length int ArrayList.
*
* (Like the Java java.util.ArrayList.
* Just without the slow java.lang.Integer unboxing/boxing)
*/
class IntList {
private static final int DEFAULT_CAPACITY = 1000;
public int[] data;
private int off = 0;
public IntList() {
data = new int[DEFAULT_CAPACITY];
}
public void add(int x) {
// if there is not enough room to store the value a new array is created.
// (like in java.lang.ArrayList)
if (off >= data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
data[off++] = x;
}
public void clear() {
off = 0;
}
public int size() {
return off;
}
public void set(int i, int x) {
data[i] = x;
}
public int get(int x) {
return data[x];
}
}
class PrimeNumbersBenchmarkApp {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long periodTime = Long.parseLong(System.getenv("RUN_TIME"), 10) * 1000;
IntList res;
while ((System.currentTimeMillis() - startTime) < periodTime) {
res = (new PrimeNumbersGenerator()).get_primes7(10000000);
System.out.format("Found %d prime numbers.\n", res.size());
}
}
}