-
Notifications
You must be signed in to change notification settings - Fork 0
/
Primes.java
70 lines (56 loc) · 1.89 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
class Primes {
static final int RATE = 50000;
public static void main (String[] argv) {
if (argv.length != 1) {
System.out.println("Usage: java Primes max_prime");
System.exit(1);
}
int max = Integer.parseInt(argv[0]);
if (max <= 0) {
System.out.println("max_prime must be a positive integer");
System.exit(1);
}
countPrimes(max);
}
private static double percentProgress(int step, int total) {
return 100.0 * ((double) step / (double) total);
}
private static void countPrimes (int max) {
Vector<Integer> primes = new Vector<Integer>();
int nPrimes = 0;
for (int i = 2; i <= max; i++) {
Boolean prime = true;
Iterator<Integer> iter = primes.iterator();
int j = 0;
while (iter.hasNext() && prime && j * j <= i) {
j = iter.next();
if (i % j == 0) {
prime = false;
}
}
if (prime) {
primes.add(i);
nPrimes++;
if (nPrimes < 100) {
System.out.format("%3d, ", i);
if (nPrimes % 5 == 0) {
System.out.println();
}
} else if (nPrimes == 100) {
System.out.format("%3d%n", i);
}
if (nPrimes % RATE == 0) {
System.out.format("\u001B[GFound %12d primes at %7.3f%%...",
nPrimes, percentProgress(i, max)
);
System.out.flush();
}
}
}
if (nPrimes < 100 && nPrimes % 5 != 0) {
System.out.println();
}
System.out.format("\u001B[G\u001B[KFound %12d primes.%n", nPrimes);
}
}