fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. map<int,int> factorFactor(int n)
  7. {
  8. map<int,int> M;
  9. for(int m = 2; m <= n; ++m)
  10. {
  11. int d = m;
  12. for(int i = 2; i*i <= d; ++i)
  13. {
  14. if (d%i) continue;
  15. while(d%i == 0)
  16. {
  17. M[i]++;
  18. d /= i;
  19. }
  20. }
  21. if (d > 1) M[d]++;
  22. }
  23. return M;
  24. }
  25.  
  26. int main(int argc, char * argv[])
  27. {
  28. auto m = factorFactor(10);
  29. for(auto x: m)
  30. {
  31. cout << x.first << "^" << x.second << " ";
  32. }
  33. cout << "\n\n\n";
  34.  
  35. m = factorFactor(100);
  36. for(auto x: m)
  37. {
  38. cout << x.first << "^" << x.second << " ";
  39. }
  40. cout << "\n";
  41.  
  42. }
  43.  
Success #stdin #stdout 0.01s 5544KB
stdin
Standard input is empty
stdout
2^8  3^4  5^2  7^1  


2^97  3^48  5^24  7^16  11^9  13^7  17^5  19^5  23^4  29^3  31^3  37^2  41^2  43^2  47^2  53^1  59^1  61^1  67^1  71^1  73^1  79^1  83^1  89^1  97^1