Advertisement
halexandru11

atestat_4.cpp

Nov 23rd, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. // determina daca numarul x este numar prim
  6. unsigned int prim(unsigned int x) {
  7.     if(x < 2) {
  8.         return 0;
  9.     }
  10.  
  11.     for(int d = 2; d*d <= x; ++d) {
  12.         if(x%d == 0) {
  13.             return 0;
  14.         }
  15.     }
  16.     return 1;
  17. }
  18.  
  19. int main() {
  20.     ifstream fin("atestat.in");
  21.     ofstream fout("atestat.out");
  22.  
  23.     unsigned int x, y, k;
  24.     fin >> x >> y >> k;
  25.     unsigned int cnt = 0;
  26.     // ne oprim cand am verificat toate numerele din interval
  27.     // sau cand am gasit k numere prime
  28.     for(unsigned int i = x; i <= y && cnt < k; ++i) {
  29.         if(prim(i)) {
  30.             fout << i << " ";
  31.             ++cnt;
  32.         }
  33.     }
  34.     // raportam in cazul in care nu s-au gasit k numere prime
  35.     if(cnt < k) {
  36.         fout << "\ns-au gasit mai putine numere prime: " << cnt;
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement