Commit aa82c55e authored by Nayuki Minase's avatar Nayuki Minase

P46: De-optimized Java code negligibly to improve simplicity.

parent f0784c8c
......@@ -15,32 +15,23 @@ public final class p046 implements EulerSolution {
public String run() {
for (int i = 2; ; i++) {
for (int i = 9; ; i += 2) {
if (!satisfiesConjecture(i))
return Integer.toString(i);
}
}
private boolean satisfiesConjecture(int n) {
if (n % 2 == 0 || isPrime(n))
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 (isPrime(n - i * i * 2))
if (Library.isPrime(n - i * i * 2))
return true;
}
return false;
}
private boolean[] isPrime = {};
private boolean isPrime(int n) {
if (n >= isPrime.length)
isPrime = Library.listPrimality(n * 2);
return isPrime[n];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment