#include <iostream>
#include <vector>
#include <bitset>
#include <cmath>
using namespace std;
vector<int> a;
bitset<46342> pr;
bitset<1000000> p;
int main(){
pr[1]=1;
ios::sync_with_stdio(0);
for(int i=2;i<46342;i++){
if(!pr[i]){
a.push_back(i);
if(i<=216)
for(int j=i*i;j<46342;j+=i)
pr[j]=1;
}
}
long long l,u;
while(cin>>l>>u){
int mn=2e9,mx=-1,al,au,bl,bu;
if(u<46342){
int last=-1,now=-1;
for(int i=l;i<=u;i++){
if(!pr[i]){
if(last==-1)
last=i,now=i;
else{
now=i;
if(mn>now-last){
mn=now-last;
al=last;
au=now;
}
if(mx<now-last){
mx=now-last;
bl=last;
bu=now;
}
last=i;
}
}
}
}
else{
p.reset();
for(int i=0;a[i]<=sqrt(u)&&i<a.size();i++){
long long j;
if(l%a[i]==0)
j=l;
else
j=(l/a[i]+1)*a[i];
for(;j<=u;j+=a[i]){
if(j!=a[i])
p[j-l]=1;
}
}
if(l==1)
p[0]=1;
int last=-1,now=-1;
for(int i=0;i<=u-l;i++){
if(!p[i]){
if(last==-1)
last=i,now=i;
else{
now=i;
if(mn>now-last){
mn=now-last;
al=last;
au=now;
}
if(mx<now-last){
mx=now-last;
bl=last;
bu=now;
}
last=i;
}
}
}
al+=l;
au+=l;
bl+=l;
bu+=l;
}
if(mx==-1)
cout<<"There are no adjacent primes."<<endl;
else
cout<<al<<','<<au<<" are closest, "<<bl<<','<<bu<<" are most distant."<<endl;
}
}
#include <vector>
#include <bitset>
#include <cmath>
using namespace std;
vector<int> a;
bitset<46342> pr;
bitset<1000000> p;
int main(){
pr[1]=1;
ios::sync_with_stdio(0);
for(int i=2;i<46342;i++){
if(!pr[i]){
a.push_back(i);
if(i<=216)
for(int j=i*i;j<46342;j+=i)
pr[j]=1;
}
}
long long l,u;
while(cin>>l>>u){
int mn=2e9,mx=-1,al,au,bl,bu;
if(u<46342){
int last=-1,now=-1;
for(int i=l;i<=u;i++){
if(!pr[i]){
if(last==-1)
last=i,now=i;
else{
now=i;
if(mn>now-last){
mn=now-last;
al=last;
au=now;
}
if(mx<now-last){
mx=now-last;
bl=last;
bu=now;
}
last=i;
}
}
}
}
else{
p.reset();
for(int i=0;a[i]<=sqrt(u)&&i<a.size();i++){
long long j;
if(l%a[i]==0)
j=l;
else
j=(l/a[i]+1)*a[i];
for(;j<=u;j+=a[i]){
if(j!=a[i])
p[j-l]=1;
}
}
if(l==1)
p[0]=1;
int last=-1,now=-1;
for(int i=0;i<=u-l;i++){
if(!p[i]){
if(last==-1)
last=i,now=i;
else{
now=i;
if(mn>now-last){
mn=now-last;
al=last;
au=now;
}
if(mx<now-last){
mx=now-last;
bl=last;
bu=now;
}
last=i;
}
}
}
al+=l;
au+=l;
bl+=l;
bu+=l;
}
if(mx==-1)
cout<<"There are no adjacent primes."<<endl;
else
cout<<al<<','<<au<<" are closest, "<<bl<<','<<bu<<" are most distant."<<endl;
}
}
Comments
Post a Comment