forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp046.java
More file actions
37 lines (28 loc) · 759 Bytes
/
p046.java
File metadata and controls
37 lines (28 loc) · 759 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
/*
* Solution to Project Euler problem 46
* 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 p046 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p046().run());
}
public String run() {
for (int i = 9; ; i += 2) {
if (!satisfiesConjecture(i))
return Integer.toString(i);
}
}
private static boolean satisfiesConjecture(int n) {
if (n % 2 == 0 || Library.isPrime(n))
return true;
// Now n is an odd composite number
for (int i = 1; i * i * 2 <= n; i++) {
if (Library.isPrime(n - i * i * 2))
return true;
}
return false;
}
}