-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem10.java
50 lines (42 loc) · 958 Bytes
/
Problem10.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.javamultiplex.projecteuler;
import java.util.Arrays;
/**
*
* @author Rohit Agarwal
* @category Project Euler Problems
* @Problem 10 - Summation of primes
*
*/
public class Problem10 {
public static void main(String[] args) {
int limit=2000000; //2 Million
long sum=0;
boolean primes[]=new boolean[limit];
Arrays.fill(primes, true); //Assign true to all array elements
generatePrimes(primes);
for(int i=2;i<limit;i++)
{
if(primes[i])
{
sum=sum+i;
}
}
System.out.println("The sum of all the primes below two million is : "+sum);
}
/*
*
* Sieve of Eratosthenes - Algorithm for generating prime numbers.
*
*/
private static void generatePrimes(boolean[] primes) {
int length=primes.length;
int sqrt=(int) Math.sqrt(length);
for(int i=2;i<=sqrt;i++)
{
for(int j=i*i;j<length;j=j+i)
{
primes[j]=false;
}
}
}
}