-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem027.cpp
66 lines (62 loc) · 1.33 KB
/
problem027.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
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
vector<bool> sieve(int n)
{
vector<bool> primes(n, true);
int squareN = sqrt(n);
for (int i = 2; i < squareN; i++)
{
if (primes[i])
{
for (int j = pow(i, 2); j < n; j += i)
{
primes[j] = false;
}
}
}
return primes;
}
int main()
{
vector<bool> primes = sieve(100000);
int a = 0;
int b = 0;
int bestCount = 0;
for (int i = -999; i < 1000; i++)
{
for (int j = -999; j < 1000; j++)
{
int n = 0;
int count = 0;
int index = pow(n, 2) + (i * n) + j;
if (index < 0)
{
if (count > bestCount)
{
bestCount = count;
a = i;
b = j;
}
continue;
}
while (primes[index])
{
count++;
n++;
index = pow(n, 2) + (i * n) + j;
if (index < 0)
break;
}
if (count > bestCount)
{
bestCount = count;
a = i;
b = j;
}
}
}
cout << a * b << endl;
return 0;
}