Advertisement
namemkazaza

I

Sep 12th, 2020
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. bool isPrime(const int &n)
  7. {
  8.     if (n == 1)
  9.         return false;
  10.     if ((n == 2) || (n == 3))
  11.         return true;
  12.     int sqr = sqrt((double)n);
  13.     for (int i = 2; i<=sqr+2; i++)
  14.         if ((n%i) == 0)
  15.             return false;
  16.     return true;
  17. }
  18.  
  19. int main()
  20. {
  21.     int k, n, j = 0;
  22.     bool flag = false;
  23.     cin >> k >> n;
  24.     int size = ((double)(2*n))/log((double)(2*n)) + 2000;
  25.    
  26.     int *arr = new int[size];
  27.  
  28.     for (int i = 0; i<=2*n; i++)
  29.     {
  30.         if (isPrime(i))
  31.         {
  32.             arr[j] = i;
  33.             j++;
  34.         }
  35.     }
  36.  
  37.     for (int i = 1; i<=j; i++)
  38.     {
  39.         if ((arr[i] == ((arr[i-1]+arr[i+1])/2.0))&&(arr[i]<=n)&&(arr[i]>=k))
  40.         {
  41.             cout << arr[i] << " ";
  42.             flag = true;
  43.         }
  44.     }
  45.    
  46.     if (!flag)
  47.         cout << "0" << endl;
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement