Project Euler > Problem 10 > Summation of primes (Java Solution)

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);
 }
 
}


1 comment :

  1. package practice;

    public 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);
    }
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact