fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. typedef unsigned long long ulong;
  5.  
  6. void print_prime_factors(ulong n)
  7. {
  8. std::cout << n << ": ";
  9. if (!n) { std::cout << "0 is not a composite number.\n"; return; }
  10.  
  11. ulong sqrt_n = sqrt(n);
  12. for (ulong i=2; i<=sqrt_n && n!=1; ++i)
  13. {
  14. if (n%i==0) { std::cout << i << " "; }
  15. while (n%i==0) { n /= i; }
  16. }
  17. if (n != 1) { std::cout << n; } //now n is a prime, must output it!
  18.  
  19. std::cout << "\n";
  20. }
  21.  
  22. int main()
  23. {
  24. ulong n;
  25. while (std::cin >> n) { print_prime_factors(n); }
  26. }
Success #stdin #stdout 0s 2856KB
stdin
600851475143
1
2
3
4
5
6
7
8
64
74
161
0
stdout
600851475143: 71 839 1471 6857 
1: 
2: 2
3: 3
4: 2 
5: 5
6: 2 3
7: 7
8: 2 
64: 2 
74: 2 37
161: 7 23
0: 0 is not a composite number.