Description
求n到m之間素數的個數
Input
多組測試數據,每組先輸入一個整數t,表示組數,然后每組輸入2個正整數n和m,(1 <= n <= m <= 10000)
Output
每組一行,內容為一個整數,輸出n到m之間素數的個數
Sample Input
1 2 3
Sample Output
2
#include<stdio.h> #include<math.h> int main() { int t; int i,j,k; int n,m; int q; int count; while(scanf("%d",&t)!=EOF) { for(i=0;i<t;i++) { count=0; scanf("%d%d",&n,&m); if(n==1) { for(j=2;j<=m;j++) { q=sqrt(j); for(k=2;k<=q;k++) { if(j%k==0) break; } if(k>q) count++; } printf("%d\n",count); } if(n>1) { for(j=n;j<=m;j++) { q=sqrt(j); for(k=2;k<=q;k++) { if(j%k==0) break; } if(k>q) count++; } printf("%d\n",count); } } } }