MohamedAbdel3al

integer factorization

Oct 7th, 2021 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std ;
  4. #define sz(s) int(s.size())
  5. #define all(s) s.begin() , s.end()
  6. #define getline(s) getline(cin >> ws, s)
  7. #define cin(v) for (auto& i : v) cin >> i ;
  8. #define cout(v) for (auto& i : v) cout << i << " "
  9. #define pb(x) push_back(x)
  10. #define ll long long
  11. #define ull unsigned long long
  12. #define Mod 1'000'000'007
  13. #define INF 2'000'000'000
  14. #define Num_of_Digits(n) ((int)log10(n)+1)
  15. #define fixed(n) fixed << setprecision(n)
  16. #define imin INT_MIN
  17. #define imax INT_MAX
  18. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n" ;
  19.  
  20. void ABDEL3AL () {
  21. ios_base::sync_with_stdio(false); cin.tie(nullptr) , cout.tie(nullptr) ;
  22. #ifndef ONLINE_JUDGE
  23. freopen("input.txt" , "r" , stdin) , freopen("output.txt" , "w" , stdout) ;
  24. #endif
  25. Time
  26. }
  27.  
  28. vector < ll > prime_factorization (ll n) {
  29. vector < ll > factors ;
  30. while (n % 2 == 0) factors.push_back(2) , n /= 2 ;
  31. for (int i = 3; i <= n; i += 2)
  32. while (n % i == 0) factors.push_back(i) , n /= i ;
  33. if (n > 2) factors.push_back(n) ;
  34. return factors ;
  35. }
  36.  
  37.  
  38. int main() {
  39. ABDEL3AL() ;
  40. ll n ;
  41. while (true) {
  42. cin >> n ;
  43. if (!n) return 0 ;
  44. map <ll , ll> freq ;
  45. vector < ll > v = prime_factorization(n) ;
  46. for (auto& i : v) freq[i]++ ;
  47. for (auto& i : freq) cout << i.first << "^" << i.second << " " ;
  48. cout << "\n" ;
  49. }
  50. return 0;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
Add Comment
Please, Sign In to add comment