-
Notifications
You must be signed in to change notification settings - Fork 0
/
10650DeterminatePrime.cpp
121 lines (115 loc) · 1.77 KB
/
10650DeterminatePrime.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define MAX 32050
using namespace std;
bool primeFlag[MAX];
void SieveEratosthenes(int L,int U){
int i,j,k;
memset(primeFlag, true, sizeof(primeFlag));
primeFlag[0]=false;
primeFlag[1]=false;
k=sqrt(U);
for(i=2;i<k;i++){
if(primeFlag[i]){
for(j=2;(j*i)<U;j++){
primeFlag[j*i]=false;
}
}
}
}
int dis[MAX]={0};
void PrimeDistance(){
int tmp;
for(int i=0;i<MAX;i++){
if(i==2){
tmp=2;
}else{
if(primeFlag[i]){
dis[tmp]=i-tmp;
tmp=i;
}
}
}
}
int disPrime[670];
void FindDiterminatePrime(){
int a,b,d,cx,cx2,arr[20];;
d=0;
cx=0;
cx2=1;
disPrime[0]=-1;
for(int i=0;i<MAX;i++){
if(dis[i]>0){
if(dis[i]==d){
arr[cx++]=i;
d=dis[i];
}else{
if(cx>1){
arr[cx++]=i;
for(int j=0;j<cx;j++){
disPrime[cx2++]=arr[j];
}
disPrime[cx2++]=-1;
}
cx=0;
arr[cx++]=i;
d=dis[i];
}
}
}
disPrime[cx2++]=-1;
}
int main(){
//freopen("in2.txt","r",stdin);
SieveEratosthenes(0,MAX);
PrimeDistance();
FindDiterminatePrime();
int a,b,c,beg,end;
while(scanf("%d %d",&a,&b)==2){
if(a==0 && b==0){
return 0;
}else{
if(a>b){
int tmp=a;
a=b;
b=tmp;
}
c=0;
for(int i=0;;){
beg=i+1;
if(disPrime[beg]==-1){
break;
}
for(int j=beg;disPrime[j]!=-1;j++){
c=j;
}
end=c;
if(disPrime[beg]>=a && disPrime[end]<=b){
for(int k=beg;k<=end;k++){
if(k==end){
printf("%d\n",disPrime[k]);
}else{
printf("%d ",disPrime[k]);
}
}
}
i=end+1;
}
}
}
return 0;
}
//critical input
/*
250 268
252 270
255 269
250 269
251 270
251 269
250 270
0 0
*/
//out put 251 257 263 269 4 last 4 input