-
Notifications
You must be signed in to change notification settings - Fork 0
/
10394 - Twin Primes.cpp
71 lines (56 loc) · 1.09 KB
/
10394 - Twin 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
#define MAX 20000001
struct primePair{
int a;
int b;
};
vector<primePair> pairList;
bool p[MAX];
void sieveMethod()
{
p[0] = p[1] = true;
for(int i = 4; i < MAX; i += 2)
{
p[i] = true;
}
int sq = sqrt(MAX);
for(int i = 3; i < sq; i+=2)
{
if(p[i] == true) continue;
for(int j = i*i; j < MAX; j += (2*i))
{
p[j] = true;
}
}
}
void setPrimePair()
{
for(int i = 1; i < MAX-2; ++i)
{
if(p[i] == false && p[i+2] == false)
{
primePair demo;
demo.a = i;
demo.b = i+2;
pairList.push_back(demo);
}
}
cout << pairList.size() << endl;
}
int main (void)
{
sieveMethod();
setPrimePair();
int n;
while(scanf("%d", &n) != EOF)
{
n--;
cout <<"(" <<pairList[n].a << ", " << pairList[n].b << ")" << endl;
}
return 0;
}