Problem:
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
9 = 7 + 2[×]12
15 = 7 + 2[×]22
21 = 3 + 2[×]32
25 = 7 + 2[×]32
27 = 19 + 2[×]22
33 = 31 + 2[×]12
It turns out that the conjecture was false.
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
Solution:
5777
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{
public String run();
}
/*
* Solution to Project Euler problem 46
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/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 = 2; ; i++) {
if (!satisfiesConjecture(i))
return Integer.toString(i);
}
}
private boolean satisfiesConjecture(int n) {
if (n % 2 == 0 || 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))
return true;
}
return false;
}
private boolean[] isPrime = {};
private boolean isPrime(int n) {
if (n >= isPrime.length)
isPrime = Library.listPrimality(n * 2);
return isPrime[n];
}
}
No comments :
Post a Comment