forked from wandering007/ProjectEuler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP27.cpp
More file actions
77 lines (77 loc) · 1.51 KB
/
Copy pathP27.cpp
File metadata and controls
77 lines (77 loc) · 1.51 KB
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
72
73
74
75
76
77
#include<iostream>
#include<fstream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#define MAXN 1000
#define LL long long
#define eps 1e-8
#define inf 0x3f3f3f3f
using namespace std;
int prim[3000];
int vis[100000];
void getprim()
{
prim[1] = 2;
int p = 2, t = 2;
while(t<3000)
{
for(int i=p*2;i<100000;i+=p)
vis[i] = 1;
p++;
while(vis[p]) p++;
prim[t++] = p;
}
return;
}
bool isPrim(int n)
{
int m = sqrt(n)+1.0+eps;
for(int i=1;prim[i]<m;i++)
if(n%prim[i]==0)
return false;
return true;
}
int primesForConVal(int a, int b)
{
int val, k = abs(b),n;
for(n=0;n<k;n++)
{
val = n*n+a*n+b;
if(val<=2||isPrim(val)==false)
return n;
}
return n;
}
int main()
{
int maxNumOfPrims = 0, cnt, reca,recb;
getprim();
for(int a=-999; a<1000; a++)
for(int b=prim[1],j=1; b<1000; b = prim[++j])
{
for(int k =0;k<2;k++)
{
cnt = primesForConVal(a, b);
if(cnt>maxNumOfPrims)
{
maxNumOfPrims = cnt;
reca = a;
recb = b;
}
}
}
printf("a = %d, b = %d, the product is %d\n",reca, recb, reca*recb);
return 0;
}