-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrossLanguageBenchmark.java
More file actions
48 lines (44 loc) · 1.33 KB
/
Copy pathCrossLanguageBenchmark.java
File metadata and controls
48 lines (44 loc) · 1.33 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
45
46
47
48
public final class CrossLanguageBenchmark {
private CrossLanguageBenchmark() {}
private static long fib(int n) {
if (n == 0) return 0L;
long a = 0L;
long b = 1L;
for (int i = 1; i <= n; i++) {
long next = a + b;
a = b;
b = next;
}
return a;
}
private static long sumSquares(int limit) {
long total = 0L;
for (int i = 1; i <= limit; i++) {
total += (long) i * (long) i;
}
return total;
}
private static long primeCount(int limit) {
long count = 0L;
for (int candidate = 2; candidate <= limit; candidate++) {
boolean isPrime = true;
if (candidate > 2) {
for (int divisor = 2; divisor < candidate; divisor++) {
if (candidate % divisor == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) count++;
}
return count;
}
public static void main(String[] args) {
long sumPart = sumSquares(2_000_000);
long fibPart = fib(35) * 2_000L;
long primePart = primeCount(5_000);
long checksum = sumPart + fibPart + primePart;
System.out.println("CHECKSUM:" + checksum);
}
}