fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int SNT(int n)
  4. {
  5. if(n < 2) return 0;
  6. int dem = 0;
  7. for(int i = 2; i <= sqrt(n); i++)
  8. {
  9. if(n % i == 0)
  10. return 0;
  11. }
  12. return 1;
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18. int n, a, b;
  19. while(cin >> n)
  20. {
  21. a = -1000000000;
  22. for(int i = n; i >= 1; i--)
  23. {
  24. if(SNT(i) == 1)
  25. {
  26. a = i;
  27. break;
  28. }
  29. }
  30. for(int i = n; i<= n + 10000000; i++)
  31. {
  32. if(SNT(i) == 1)
  33. {
  34. b = i;
  35. break;
  36. }
  37. }
  38. if(n - a <= b - n)
  39. cout << a << endl;
  40. else cout << b << endl;
  41. }
  42. }
  43.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty