Advertisement
Carbastan

DetPrim

Dec 8th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. //ifstream cin("maxd.in");
  7. //ofstream cout("maxd.out");
  8.  
  9. bool Prim(int n)
  10. {
  11.     if(n <= 1) return false;
  12.     else if(n <= 3) return true;
  13.     else if(n % 2 == 0 or n % 3 == 0) return false;
  14.     else
  15.     {
  16.         for(int i = 5; i * i <= n; i += 6)
  17.         {
  18.             if(n % i == 0 or n % (i + 2) == 0) return false;
  19.         }
  20.     }
  21.  
  22.     return true;
  23. }
  24.  
  25. int main()
  26. {
  27.     long long a, b, nr = 0, MIN = 1e9;
  28.     bool gasit = false;
  29.     cin >> a >> b;
  30.  
  31.     if(a % 2 != 0)
  32.     {
  33.         for(int i = a; i <= b; i += 2)
  34.         {
  35.             int q = i;
  36.  
  37.             if(Prim(q))
  38.             {
  39.                 gasit = true;
  40.                 if(abs((a + b) - 2 * q) < MIN)
  41.                 {
  42.                     MIN = abs((a + b) - 2 * q);
  43.                     nr = q;
  44.                 }
  45.                 else if(abs((a + b) - 2 * q) == MIN)
  46.                 {
  47.                     if(q < nr) nr = q;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     else
  53.     {
  54.         for(int i = a + 1; i <= b; i += 2)
  55.         {
  56.             int q = i;
  57.  
  58.             if(Prim(q))
  59.             {
  60.                 gasit = true;
  61.                 if(abs((a + b) - 2 * q) < MIN)
  62.                 {
  63.                     MIN = abs((a + b) - 2 * q);
  64.                     nr = q;
  65.                 }
  66.                 else if(abs((a + b) - 2 * q) == MIN)
  67.                 {
  68.                     if(q < nr) nr = q;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     if(gasit) cout << nr;
  75.     else cout << "NU EXISTA";
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement