-
Notifications
You must be signed in to change notification settings - Fork 0
/
summation-of-primes.cpp
59 lines (52 loc) · 1.07 KB
/
summation-of-primes.cpp
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
54
55
56
57
// https://www.hackerrank.com/contests/projecteuler/challenges/euler010/problem
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
bool prime[2000001] = {0};
prime[0] = 1;
prime[1] = 0;
for(int i = 2; i*i <= 2000000; i++ )
{
if(!prime[i])
{
for(int j = i * i ; j < 2000001 ; j+= i)
{
prime[j] = true;
}
}
}
vector < ll > p;
vector < ll > sum;
ll present = 0;
for(ll i =2 ; i <= 2000000 ; i ++)
{
if(!prime[i])
{
p.push_back(i);
sum.push_back(present + i);
present += i;
}
}
ll t;
cin >> t;
while( t-- )
{
ll n;
cin >> n;
int pos = lower_bound(p.begin() , p.end() , n) - p.begin();
if(pos == p.size())
{
pos --;
}
else if(p[pos] > n)
{
pos --;
}
if(pos < 0)
cout << "0" <<endl;
else
cout << sum[pos] << endl;
}
}