-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem35.cpp
More file actions
66 lines (60 loc) · 2.1 KB
/
Problem35.cpp
File metadata and controls
66 lines (60 loc) · 2.1 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
/*
Krishna Mohan
Project Euler - Problem 35
Date: 3/22/2015
*/
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for(int (i)=0;(i)<(n); (i)++)
#define MAX 1000001
bitset<MAX> isPrime;
void init()
{
isPrime.set();
isPrime[0]=isPrime[1]=0;
for(int i=4;i<MAX;i+=2)
isPrime[i]=0;
for(int i=3;i<MAX;i+=2)
{
if(isPrime[i])
{
for(int j=2*i;j<MAX;j+=i)
isPrime[j]=0;
}
}
}
int main() {
init();
int dat[10], num , len, cnt=0;
for(int i=2;i<MAX;i++)
{
if(isPrime[i])
{
num=i;
len=0;
while(num)
{
dat[len++]=num%10;
num/=10;
}
int f=0;
for(int i=len-1;i>=0;i--)
{
num=0;
for(int j=i, k=0;k<len;j--, k++)
{
num=num*10+dat[(j+len)%len];
}
if(!isPrime[num])
f=1;
}
if(!f)
{
cnt++;
//cout<<i<<endl;
}
}
}
cout<<"Ans: "<<cnt<<endl;
return 0;
}