#include <stdio.h>
int s1[1005000],s2[1005000];
If main is an array of global variables defined in the array's outer function, initialize array 0 by itself;
To define an array in main, remember to initialize
int main ()
{
int n,l,r;
int a,b,c,d,g=1;
The method of prime number playing table
S1[0]=s1[1]=1;
for (a=0;a<1000000;a++)
{
if (S1[a])
Continue
for (B=a+a;b<1000000;b+=a)
s1[b]++;
}
D=0;
for (a=0;a<1000000;a++)
{
B=a;
c=0;
while (b)
{
c+=b%10;
b/=10;
}
if (s1[c]==0&&s1[a]==0)
D++;//D is the number of US primes
S2[a]=d;//s2 "A" is the number of us primes from 0 to a
}
scanf ("%d", &n);
Write the data to the outside. Each time you use a direct call, the data is calculated only once. So it saves time.
while (n--)
{
scanf ("%d%d", &l,&r);
printf ("Case #%d:", g);
g++;
printf ("%d\n", S2[r]-s2[l-1]);
}
return 0;
}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
1407251735-hd-US Prime. cpp