Problem:
The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
Solution:
142913828922
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{
public String run();
}
/*
* Solution to Project Euler problem 10
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p010 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p010().run());
}
private static final int LIMIT = 2000000;
public String run() {
long sum = 0;
for (int p : Library.listPrimes(LIMIT - 1))
sum += p;
return Long.toString(sum);
}
}
No comments :
Post a Comment