程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Prime Generator,primegenerator

Prime Generator,primegenerator

編輯:C++入門知識

Prime Generator,primegenerator


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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved