-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem50.java
34 lines (34 loc) · 996 Bytes
/
problem50.java
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
import java.util.*;
class problem50
{
public static void main (String [] args)
{
long max = 1000000;
long sum = 0, solution = -1, maxCounter = Integer.MIN_VALUE, trueSolution = -1;
int counter = 0, total = 0;
for(long j = 1; true; j = nextPrime(j)){
for(long i = j; sum < max; i = nextPrime(i)){
sum+=nextPrime(i);
counter++;
if (sum < max && isPrime (sum)) { total = counter; solution = sum; }
}
sum = 0;
if (total > maxCounter) { trueSolution = solution; maxCounter = total; }
total = 0;
counter = 0;
if (j > max) break;
}
System.out.println(trueSolution);
}
private static long nextPrime(long currentPrime)
{
for(long x = currentPrime+1; true; x++)
if(isPrime(x)) return x;
}
private static boolean isPrime (long x)
{
for(long i = 2; i <= x>>1; i++)
if(x % i == 0) return false;
return true;
}
}