fork(2) download
  1. # include <stdio.h>
  2. # include <math.h>
  3. # include <iostream>
  4. # include <map>
  5.  
  6. using namespace std;
  7. // A function to print all prime factors of a given number n
  8. map<int,int> m;
  9. void primeFactors(int n)
  10. {
  11. // Print the number of 2s that divide n
  12. while (n%2 == 0)
  13. {
  14. printf("%d ", 2);
  15. m[2] += 1;
  16. n = n/2;
  17. }
  18.  
  19. // n must be odd at this point. So we can skip one element (Note i = i +2)
  20. for (int i = 3; i <= sqrt(n); i = i+2)
  21. {
  22. // While i divides n, print i and divide n
  23. while (n%i == 0)
  24. {
  25. int k = i;
  26. printf("%d ", i);
  27. m[k] += 1;
  28. n = n/i;
  29. }
  30. }
  31.  
  32. // This condition is to handle the case whien n is a prime number
  33. // greater than 2
  34. if (n > 2)
  35. m[n] += 1;
  36. printf ("%d ", n);
  37.  
  38.  
  39. cout << endl;
  40. }
  41.  
  42. /* Driver program to test above function */
  43. int main()
  44. {
  45. int n = 72;
  46. primeFactors(n);
  47. map<int,int>::iterator it;
  48. int to = 1;
  49. for(it = m.begin(); it != m.end(); ++it){
  50. cout << it->first << " appeared " << it->second << " times "<< endl;
  51. to *= (it->second+1);
  52. }
  53. cout << to << " total facts" << endl;
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
2 2 2 3 3 1 
2 appeared 3 times 
3 appeared 2 times 
12 total facts