-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProject Euler #10- Summation of primes.java
53 lines (45 loc) · 1.33 KB
/
Project Euler #10- Summation of primes.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
51
52
53
import java.util.*;
import java.io.*;
public class Solution{
public static final int MAX = 1000000;
public static void main(String args[]) throws IOException {
List<Integer> list = new ArrayList<>();
list.add(2);
for(int i = 3; i <= MAX; i+=2){
boolean isPrime = true;
for(int p : list){
if(p*p > i){
break;
}
if(i%p == 0){
isPrime = false;
break;
}
}
if(isPrime){
list.add(i);
}
}
Map<Integer, Long> sums = new HashMap<>();
long sum = 0;
for (int p : list){
sum += p;
sums.put(p, sum);
}
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0){
int x = Integer.parseInt(br.readLine());
// boolean find = map.containsKey(x);
while(sums.containsKey(x) == false){
--x;
}
if(sums.containsKey(x)){
long value = sums.get(x);
System.out.println(value);
}
}
//System.out.println(Arrays.asList(sums));
}
}