Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. //#include <cmath>
  5. //#include <iomanip>
  6. using namespace std;
  7.  
  8. bool simple (int n)
  9. {
  10.     int cnt = 0;
  11.     for (int i = 2; i * i <= n; i++ )
  12.             if (n % i == 0) cnt++;
  13.  
  14.     if (cnt == 0) return true;
  15.         else return false;
  16. }
  17. int main()
  18. {
  19.  
  20. ifstream in("input.txt");
  21. ofstream out("output.txt");
  22.  int a, b, cnt, ch1, ch2;
  23.  in >> a;
  24.  ch1 = 1;
  25.  ch2 = 1;
  26.  
  27.  if (simple (a) == true)
  28.      out << a;
  29.  else
  30.  {
  31.     for (int i = a + 1; simple(i) != true; i++)
  32.         ch2++;
  33.     for (int i = a - 1; simple(i) != true; i--)
  34.         ch1++;
  35.  
  36.     if (ch1 <= ch2) out << a - ch1;
  37.     else out << a + ch2;
  38.  
  39.  }
  40.    
  41.      in.close();
  42.      out.close();
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement