karlangadas

Prime Distance - inconcluso

Dec 23rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <cmath>
  5. using namespace std;
  6. const long long N =100000000;
  7.  
  8. bool esPrimo[N];
  9. void criba(long long ini,long long n){
  10.     for (long long i=0;i<=n;i++) esPrimo[i]=true;
  11.  
  12.     esPrimo[0]=esPrimo[1]=false;
  13.  
  14.     for (long long p=ini;p<=n;p++){
  15.         if (esPrimo[p])
  16.             for(long long mul=p*p;mul<N;mul+=p) esPrimo[mul]=false;
  17.     }
  18. }
  19. int numgrandeEsPrimo(long long n){
  20.     long long tope=((long long) pow(n,0.5)) +1;
  21.     //cout<<"tope: "<<tope<<endl;
  22.     for (long long i=2;i<=tope;i++){
  23.         if ((n % i) ==0){
  24.             return 0;}
  25.     }
  26.     //cout<<n<<" es primo"<<endl;
  27.     return 1;
  28. }
  29.  
  30. int main(){
  31.    
  32.     long long l,u,uant=-1;
  33.     criba(2,100000000);
  34.     while(cin>>l>>u){
  35.         long long d1=-1,d2=-1,c1=-1,c2=-1,ant,difmin=u,difmax=0;
  36.         for(long long i=l;i<=u;i++){
  37.             if (esPrimo[i]){
  38.                 if (d1==-1)
  39.                     d1=i;
  40.                 else{
  41.                     if ((i-ant)>difmax){
  42.                         d1=ant;d2=i;difmax=d2-d1;
  43.                     }
  44.                 }
  45.                 if (c1==-1)
  46.                     c1=i;
  47.                 else{
  48.                     if ((i-ant)<difmin){
  49.                         c1=ant;c2=i;difmin=c2-c1;
  50.                     }
  51.                 }
  52.                 ant=i;
  53.             }
  54.         }
  55.         if(c2!=-1)
  56.             cout<<c1<<','<<c2<<" are closest, ";
  57.         if(d2!=-1)
  58.             cout<<d1<<','<<d2<<" are most distant."<<endl;
  59.         else
  60.             cout<<"There are no adjacent primes."<<endl;
  61.     }
  62.  
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment