-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler46.java
55 lines (42 loc) · 940 Bytes
/
Euler46.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
class Euler46 {
public static void main(String arg[]) {
LinkedList<Integer> primes = new LinkedList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
Iterator<Integer> it;
int i = 9;
int j;
int prime, sum;
a: while(true) {
i += 2;
if(!isPrime(i)) {
it = primes.iterator();
while(it.hasNext()) {
prime = it.next();
j = 1;
sum = prime + 2*j*j;
while(sum < i) {
j++;
sum = prime + 2*j*j;
}
if(sum == i)
continue a;
}
break a;
}
else
primes.add(i);
}
System.out.println(i);
}
public static boolean isPrime(int n) {
int sqrt = (int) Math.sqrt(n);
for(int i = 3; i <= sqrt; i+=2)
if(n % i == 0)
return false;
return true;
}
}