Advertisement
halexandru11

atestat_6.cpp

Nov 23rd, 2020
194
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 <fstream>
  3.  
  4. using namespace std;
  5.  
  6. // determina daca numarul x este puternic
  7. // returneaza 1 in caz afirmativ, 0 in caz negativ
  8. unsigned int puternic(unsigned int x) {
  9.     unsigned int aux = x;
  10.     for(unsigned int div = 2; div <= aux; ++div) {
  11.         if(aux%div == 0) {
  12.             if(x%(div*div) != 0) {
  13.                 return 0;
  14.             }
  15.             while(aux%div == 0) {
  16.                 aux /= div;
  17.             }
  18.         }
  19.     }
  20.     return 1;
  21. }
  22.  
  23. int main() {
  24.     ofstream fout("numere.out");
  25.  
  26.     unsigned int n;
  27.     cin >> n;
  28.     // am observat ca primul numar puternic este 4
  29.     // trec prin toate numerele din [4, n] si le afisez doar pe cele puternice
  30.     for(unsigned int i = 4; i <= n; ++i) {
  31.         if(puternic(i)) {
  32.             fout << i << " ";
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement