fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. int main() {
  7. ll n;
  8. cin>>n;
  9. unordered_map<ll, ll> mp;
  10. while(n%2==0){
  11. mp[2]++;
  12. n=n/2;
  13. }
  14. for(ll i=3;i<=sqrt(n);i=i+2){
  15. while(n%i==0){
  16. mp[i]++;
  17. n=n/i;
  18. }
  19. }
  20. if(n>2){
  21. //this is for cases where n=34
  22. //so first 2*17
  23. //then sqrt(17)=4.12
  24. //so loop runs for i=3 and fails and it exits
  25. //so 17 remains as is, so we need to count it now separately
  26. mp[n]++;
  27. }
  28. for(auto it:mp){
  29. cout<<it.first<<"^"<<it.second;
  30. cout<<"\n";
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 5344KB
stdin
34
stdout
17^1
2^1