-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrime Tuples.cpp
59 lines (44 loc) · 911 Bytes
/
Prime Tuples.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
#include <bits/stdc++.h>
using namespace std;
const long long MOD= 1e9+7;
const int n = 1e6+10;
int a[n]={0};
vector<int> ans(n,0);
void SieveOfEratosthenes()
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int p = 2; p <= n; p++)
if (prime[p])
a[p]=1;
ans[0]=0;
ans[1]=0;
for(int i=2;i<=n;i++){
if(a[i]==1 && a[i-2]==1)
ans[i]=ans[i-1]+1;
else
ans[i]=ans[i-1];
}
}
int main(){
int t;
cin>>t;
SieveOfEratosthenes();
while(t--){
int pl;
cin>>pl;
cout<<ans[pl]<<endl;
}
return 0;
}