-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.java
More file actions
38 lines (32 loc) · 1.08 KB
/
primes.java
File metadata and controls
38 lines (32 loc) · 1.08 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
public class Primes {
public static void main (String[] args) {
int counter=0;
int min = 0;
int max = 20000000;
System.out.println("Java " + java.lang.Runtime.version());
double startTime = System.nanoTime();
for (int num = min; num <= max; num++) {
if(IsPrime(num)) { counter = counter + 1; }
}
double endTime = System.nanoTime();
System.out.println("Number of primes in the interval [" + min + "," + max + "]: " + counter);
System.out.println("Execution time: " + (endTime - startTime) / 1000000000 + " seconds");
}
private static boolean IsPrime(int num) {
if (num <= 1) {
return false;
}
if (num <= 3) {
return true;
}
if (((num % 2) == 0) || ((num % 3) == 0)) {
return false;
}
for (int c = 5; c * c <= num; c += 6) {
if (((num % c) == 0) || ((num % (c + 2)) == 0)) {
return false;
}
}
return true;
}
}