2 whereas the PRIMES 11, 19, 23, and 31 cannot be expressed as a sum of two squares. write a program to count the number of Primes that can be expressed as sum of squares within a given interval.
Enter your program will be tested on one or more test cases. each test case is specified on a separate input line that specifies two integers l, u where L ≤ U <1,000,000 the last line of the input file between des a dummy test case with both L = u =? 1. output L u x ywhere L and u are as specified in the input. X is the total number of Primes within the interval [L, u] (random Sive,) and Y is the total number of Primes (also within [L, u]) that can be expressed as a sum of squares. sample Input10 2011 19100 1000-1 -1
Sample output10 20 4 211 19 4 2100 1000 143 69
Sure enough, it's a tough question. If the meaning of the question is very clear, we can use the prime number to screen the brute force. There is a pitfall, for example, the range is 1-2. At this time, 1 is also qualified, because 1 = 4*0 + 1 and 1 = 0*0 + 1*1 (although 1 is not a prime number, why is there such data ?)
<pre name="code" class="html">#include <iostream>#include <cstring>#include <cstdio>#include <cctype>#include <cstdlib>#include <algorithm>#include <set>#include <vector>#include <string>#include <map>#include <queue>using namespace std;const int maxn= 1000010;int num=0;int vis[maxn],prime[maxn];void init_prime(){memset(vis,1,sizeof(vis));vis[0]=0;vis[1]=0;for(int i=0;i<=maxn;i++){if(vis[i]){prime[++num]=i; for(int j=1;j*i<=maxn;j++)vis[j*i]=0;}}}int main(){int L,U,i;init_prime();while(scanf("%d%d",&L,&U)!=EOF){int cnt1=0,cnt2=0;if(L==-1&&U==-1) break;for(i=0;i<=num;i++){if(prime[i]&&prime[i]>=L&&prime[i]<=U){if((prime[i]-1)%4==0)cnt2++;cnt1++;}}if(L<=2&&U>=2)cnt2++;printf("%d %d %d %d\n",L,U,cnt1,cnt2);}return 0;}
Sdut Fermat's Chirstmas theorem (prime screening)