Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input ...
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Example
CODE:1 #include<iostream> 2 #include<math.h> 3 #include<string> 4 5 6 using namespace std; 7 8 string s; 9 bool Isprime(int m)//is prime 10 { 11 if(m==1) 12 return false; 13 int i=(int)sqrt(m); 14 for(int j=2;j<=i;j++) 15 { 16 if(m%j==0) 17 return false; 18 } 19 return true; 20 } 21 22 void primegen(int i,int j)//prime genaerator 23 { 24 25 while(i<=j) 26 { 27 if(Isprime(i)) 28 { 29 char t[10]; 30 sprintf(t,"%d\n",i); 31 32 s=s+t; 33 } 34 i++; 35 } 36 s=s+"\n"; 37 } 38 int main() 39 { 40 s=""; 41 int k; 42 cin>>k; 43 int i1,i2; 44 while(k>0) 45 { 46 cin>>i1; 47 cin>>i2; 48 k--; 49 primegen(i1,i2); 50 } 51 cout<<s<<endl; 52 return 0; 53 }
代碼運行結果:
2016-12-31 13:15:16