forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp050.java
More file actions
41 lines (32 loc) · 973 Bytes
/
p050.java
File metadata and controls
41 lines (32 loc) · 973 Bytes
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
/*
* Solution to Project Euler problem 50
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p050 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p050().run());
}
private static final int LIMIT = Library.pow(10, 6);
public String run() {
boolean[] isPrime = Library.listPrimality(LIMIT);
int[] primes = Library.listPrimes(LIMIT);
long maxSum = 0;
int maxRun = -1;
for (int i = 0; i < primes.length; i++) { // For each index of a starting prime number
int sum = 0;
for (int j = i; j < primes.length; j++) { // For each end index (inclusive)
sum += primes[j];
if (sum > LIMIT)
break;
else if (j - i > maxRun && sum > maxSum && isPrime[sum]) {
maxSum = sum;
maxRun = j - i;
}
}
}
return Long.toString(maxSum);
}
}