首页 > 代码库 > Prime Generator
Prime Generator
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
Prime Generator
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。