Advertisement
MeehoweCK

Untitled

Dec 17th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. bool czy_pierwsza(unsigned x)
  7. {
  8.     if(x == 1 || x == 0)
  9.         return false;
  10.     if(x == 2)
  11.         return true;
  12.     for(unsigned i = 2; i <= pow(x,0.5); ++i)
  13.     {
  14.         if(x % i == 0)
  15.             return false;
  16.     }
  17.     return true;
  18. }
  19.  
  20. unsigned nastepna_pierwsza(unsigned n)
  21. {
  22.     for(unsigned i = n + 1; true; ++i)
  23.         if(czy_pierwsza(i))
  24.             return i;
  25. }
  26.  
  27. int main()
  28. {
  29.     unsigned n;
  30.     cout << "Podaj liczbe naturalna: ";
  31.     cin >> n;
  32.  
  33.     if(czy_pierwsza(n))
  34.         cout << "Podana liczba jest liczba pierwsza" << endl;
  35.     else
  36.     {
  37.         cout << "Podana liczba nie jest liczba pierwsza. Najmniejsza liczba pierwsza wieksza od " << n << " jest liczba ";
  38.         cout << nastepna_pierwsza(n) << endl;
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement