Advertisement
halexandru11

atestat_7.cpp

Nov 24th, 2020
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. // determina daca numarul x este numar prim
  7. // returneaza 1 in caz afirmativ, 0 in caz negativ
  8. unsigned int prim(unsigned int x) {
  9.     if(x < 2) {
  10.         return 0;
  11.     }
  12.  
  13.     for(unsigned int div = 2; div*div <= x; ++div) {
  14.         if(x%div == 0) {
  15.             return 0;
  16.         }
  17.     }
  18.     return 1;
  19. }
  20.  
  21. int main() {
  22.     ofstream fout("date.out");
  23.  
  24.     unsigned int n;
  25.     cin >> n;
  26.  
  27.     unsigned int x = 3;
  28.     // am nevoie de n perechi
  29.     while(n) {
  30.         // daca gasesc doua numere prime gemene le afisez
  31.         // si decrementez n pt ca am gasit o pereche
  32.         if(prim(x) && prim(x+2)) {
  33.             fout << x << " " << x+2 << "\n";
  34.             --n;
  35.         }
  36.         // doar numerele impare pot fi prime, asa ca trec peste cele pare
  37.         x += 2;
  38.     }
  39. }
  40.  
Advertisement
RAW Paste Data Copied
Advertisement