Advertisement
Carbastan

Descompunerea in factori primi

Nov 30th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int n, p = 0, d = 3;
  8.     cin >> n;
  9.    
  10.     while(n % 2 == 0)
  11.     {
  12.         ++p;
  13.         n /= 2;
  14.     }
  15.    
  16.     if(p) cout << 2 << " "; // faci ce vrei tu
  17.    
  18.     while(n != 1)
  19.     {
  20.         p = 0;
  21.        
  22.         while(n % d == 0)
  23.         {
  24.             ++p;
  25.             n /= d;
  26.         }
  27.        
  28.         if(p) cout << d << " ";
  29.        
  30.         d += 2;
  31.        
  32.         if(d * d > n) d = n;
  33.     }
  34.    
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement