#include<bits/stdc++.h>
using namespace std;
#define N 1000100
int p[N+1];
int a[N+1];
int anik(int n)
{
int s = 0;
while(n != 0)
{
s = s + (n%10);
n = n/10;
}
return s;
}
void prime()
{
int i, j, l, b;
l = sqrt(N);
p[0] = p[1] = 1;
a[2] = 1;
for(i=4;i<=N;i+=2)
p[i] = 1;
for(i=3;i<=N;i+=2)
{
if(p[i] == 0)
{
b = anik(i);
if(p[b] == 0)
a[i] = 1;
if(i <= l)
{
for(j=i*i;j<=N;j+=i*2)
{
p[j] = 1;
}
}
}
}
for(i=1;i<=N;i++)
{
a[i] = a[i] + a[i-1];
}
}
int main()
{
prime();
int i, n, m, x, y;
while(scanf("%d", &x) == 1)
{
for(i=1;i<=x;i++)
{
scanf("%d %d", &n, &m);
y = a[m] - a[n-1];
printf("%d\n", y);
}
}
return 0;
}
using namespace std;
#define N 1000100
int p[N+1];
int a[N+1];
int anik(int n)
{
int s = 0;
while(n != 0)
{
s = s + (n%10);
n = n/10;
}
return s;
}
void prime()
{
int i, j, l, b;
l = sqrt(N);
p[0] = p[1] = 1;
a[2] = 1;
for(i=4;i<=N;i+=2)
p[i] = 1;
for(i=3;i<=N;i+=2)
{
if(p[i] == 0)
{
b = anik(i);
if(p[b] == 0)
a[i] = 1;
if(i <= l)
{
for(j=i*i;j<=N;j+=i*2)
{
p[j] = 1;
}
}
}
}
for(i=1;i<=N;i++)
{
a[i] = a[i] + a[i-1];
}
}
int main()
{
prime();
int i, n, m, x, y;
while(scanf("%d", &x) == 1)
{
for(i=1;i<=x;i++)
{
scanf("%d %d", &n, &m);
y = a[m] - a[n-1];
printf("%d\n", y);
}
}
return 0;
}
Comments
Post a Comment