-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRIME1(prime generator).cpp
68 lines (67 loc) · 1 KB
/
PRIME1(prime generator).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Sieve of Eratosthenes
#include<iostream>
#include<math.h>
#include<list>
using namespace std;
int main()
{
int i,k=2,cases;
long m,n;
int t=32000;
list<int> l;
for(i=2;i<=t;i++)
l.push_back(i);
list<int>:: iterator p,p1,p2;
p=l.begin();
p++;
p2=p;
while(1)
{
p=p2;
while(p!=l.end())
{
if((*p)%k==0)
{
p1=p;
p++;
l.erase(p1);
}
else
p++;
}
k=*p2;
p2++;
if(*p2>=sqrt(t)) break;
}
cin>>cases;
while(cases--)
{
cin>>m>>n;
if(m==1) m++;
list<int> l2;
for(i=m;i<=n;i++)
l2.push_back(i);
p=l.begin();
while(1)
{
k=*p;
p++;
p2=l2.begin();
while(p2!=l2.end())
{
if((*p2)%k==0 && (*p2)/k !=1 )
{
p1=p2;
p2++;
l2.erase(p1);
}
else
p2++;
}
if(*p>sqrt(n)) break;
}
p2=l2.begin();
while(p2!=l2.end())
{ cout<<*p2<<endl; p2++; }
}
}