fork(6) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void print_prime_factors(long long int n)
  6. {
  7. long long int i = 2;
  8.  
  9. // print out the initial value of n
  10.  
  11. cout << "n = " << n << endl;
  12.  
  13. // keep dividing n by factors from smallest ones,
  14. // until n becomes 1
  15.  
  16. while (n>1)
  17. {
  18. // is n divisible by i?
  19.  
  20. if (n%i == 0)
  21. {
  22. // if n divisible by i, divide it by i and print out i because
  23. // it is a new prime factor (we must divide to not repeat
  24. // the same factors but factor only what remains)
  25.  
  26. n = n/i;
  27.  
  28. cout << i << " new value of n = " << n << endl;
  29. }
  30. else
  31. i++; // otherwise, we need to try divide by something bigger
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. long long int n;
  38.  
  39. // read (long) integer n from input
  40.  
  41. cin >> n;
  42.  
  43. // compute and print out all prime factors
  44.  
  45. print_prime_factors(n);
  46.  
  47. // return 0 from main
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.02s 2684KB
stdin
 100
stdout
n = 100
2    new value of n = 50
2    new value of n = 25
5    new value of n = 5
5    new value of n = 1