-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeTest.java
More file actions
44 lines (43 loc) · 1.49 KB
/
PrimeTest.java
File metadata and controls
44 lines (43 loc) · 1.49 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
import java.util.concurrent.*;
import java.util.*;
public class PrimeTest {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) throws InterruptedException {
int limit = 500000000;
int threads = 22;
long startTime = System.nanoTime();
ExecutorService executor = Executors.newFixedThreadPool(threads);
List<Future<Integer>> futures = new ArrayList<>();
int chunk = limit / threads;
for (int i = 0; i < threads; i++) {
final int start = i * chunk;
final int end = (i + 1) * chunk;
futures.add(executor.submit(() -> {
int count = 0;
for (int j = start; j < end; j++) {
if (isPrime(j)) count++;
}
return count;
}));
}
int totalPrimes = 0;
for (Future<Integer> f : futures) {
try {
totalPrimes += f.get();
} catch (Exception e) {
e.printStackTrace();
}
}
executor.shutdown();
System.out.println("Total primes: " + totalPrimes);
long endTime = System.nanoTime();
double elapsedSeconds = (endTime - startTime) / 1_000_000_000.0;
System.out.printf("Tempo de execução: %.5f segundos%n", elapsedSeconds);
}
}