-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10533-Digit Primes
66 lines (53 loc) · 1.1 KB
/
10533-Digit Primes
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<bits/stdc++.h>
using namespace std;
const int N=1000000;
int val[1000001]={0};
bool prime[1000001];
void Sieve(int n)
{
memset(prime, true, sizeof(prime));
int c=0;
for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
/* for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " "; */
}
void digit(){
for(long long i=2;i<N;i++){
int sum=0,m;
int p=i;
while(p!=0)
{
m=p%10;
sum=sum+m;
p=p/10;
}
if(prime[sum]==true && prime[i])
val[i]=val[i-1]+1;
else
val[i]=val[i-1];
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
Sieve(N);
digit();
int t,n1,n2;
cin>>t ;
while(t--){
cin>>n1>>n2;
cout<<val[n2]-val[n1-1]<<endl;
}
return 0;
}