Problem:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
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); } }
package practice;
ReplyDeletepublic class SummationOfPrimes {
public static void main(String[] args) {
long sum=0;
for(int n=2;n<2000000;n++)
{
long c=0;
for(long i=2;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==1)
sum=sum+n;
}
System.out.println("Sum is:"+sum);
}
}