Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132937 views
License: OTHER
1
/* @author Axel Busch */
2
public class SingleCorePrimeTest {
3
4
public static boolean isPrime(int n) {
5
if (n < 2) {
6
return false;
7
}
8
9
for (int i = 2; i <= Math.sqrt(n); ++i) {
10
if (n % i == 0) {
11
return false;
12
}
13
}
14
return true;
15
}
16
17
public static void main(String[] args) {
18
int target = 10_000_000;
19
long start = System.currentTimeMillis();
20
for (int i = 2; i <= target; ++i) {
21
isPrime(i);
22
}
23
long end = System.currentTimeMillis();
24
System.out.println(end-start);
25
}
26
27
}
28