fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. unsigned largest_prime_factor(unsigned n)
  5. {
  6. unsigned lpf = 0;
  7. unsigned stop = sqrt(n);
  8. for (unsigned i=2; i<=stop; ++i)
  9. if (n%i==0) {
  10. lpf = i;
  11. while (n%i==0) { n/= i; }
  12. stop = sqrt(n);
  13. }
  14. return n==1 ? lpf : n;
  15. }
  16.  
  17. int main()
  18. {
  19. std::cout << largest_prime_factor(2147483647) << "\n";
  20. std::cout << largest_prime_factor(4294967291) << "\n";
  21. std::cout << largest_prime_factor(4294967294) << "\n";
  22. std::cout << largest_prime_factor(65536) << "\n";
  23. std::cout << largest_prime_factor(2*2*2*7*13*19) << "\n";
  24. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
2147483647
4294967291
2147483647
2
19