Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <vector>
- #include <cmath>
- using namespace std;
- const long long N =100000000;
- bool esPrimo[N];
- void criba(long long ini,long long n){
- for (long long i=0;i<=n;i++) esPrimo[i]=true;
- esPrimo[0]=esPrimo[1]=false;
- for (long long p=ini;p<=n;p++){
- if (esPrimo[p])
- for(long long mul=p*p;mul<N;mul+=p) esPrimo[mul]=false;
- }
- }
- int numgrandeEsPrimo(long long n){
- long long tope=((long long) pow(n,0.5)) +1;
- //cout<<"tope: "<<tope<<endl;
- for (long long i=2;i<=tope;i++){
- if ((n % i) ==0){
- return 0;}
- }
- //cout<<n<<" es primo"<<endl;
- return 1;
- }
- int main(){
- long long l,u,uant=-1;
- criba(2,100000000);
- while(cin>>l>>u){
- long long d1=-1,d2=-1,c1=-1,c2=-1,ant,difmin=u,difmax=0;
- for(long long i=l;i<=u;i++){
- if (esPrimo[i]){
- if (d1==-1)
- d1=i;
- else{
- if ((i-ant)>difmax){
- d1=ant;d2=i;difmax=d2-d1;
- }
- }
- if (c1==-1)
- c1=i;
- else{
- if ((i-ant)<difmin){
- c1=ant;c2=i;difmin=c2-c1;
- }
- }
- ant=i;
- }
- }
- if(c2!=-1)
- cout<<c1<<','<<c2<<" are closest, ";
- if(d2!=-1)
- cout<<d1<<','<<d2<<" are most distant."<<endl;
- else
- cout<<"There are no adjacent primes."<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment