-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.cpp
More file actions
61 lines (52 loc) · 952 Bytes
/
Copy path46.cpp
File metadata and controls
61 lines (52 loc) · 952 Bytes
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
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
vector<int> primes;
#define MAX 50000
bool is_square(int n)
{
double x = sqrt(n * 1.);
return ceil(x) == floor(x);
}
int* seive(int n)
{
int *seivearr, i, j;
seivearr = (int *)malloc(n * sizeof(int));
memset(seivearr, 0, sizeof(int) * n);
for (i = 2; i * i <= n; i++)
{
if (seivearr[i])
continue;
primes.push_back(i);
for (j = 2 * i; j < n; j += i)
seivearr[j] = 1;
seivearr[i] = 0;
}
for (; i < n; i++)
if (!seivearr[i])
primes.push_back(i);
return seivearr;
}
int main()
{
int i, *seivearr;
seivearr = seive(MAX);
for (int i = 3; i < MAX; i += 2)
{
if (!seivearr[i]) continue;
int j;
for (j = 0; j < primes.size(); j++)
if (primes[j] < i && is_square((i - primes[j]) >> 1))
break;
if (j == primes.size())
{
cout << i << endl;
break;
}
}
free(seivearr);
return 0;
}