fork download
  1. #include <iostream>
  2.  
  3. int main() {
  4. int n;
  5. std::cout << "Nhap vao so nguyen n: ";
  6. std::cin >> n;
  7. long long T = 0;
  8. int D = 0;
  9. for (int i = 2; i <= n; i++) {
  10. bool isPrime = true;
  11. for (int j = 2; j * j <= i; j++) {
  12. if (i % j == 0) {
  13. isPrime = false;
  14. break;
  15. }
  16. }
  17. if (isPrime) {
  18. T += i;
  19. D++;
  20. }
  21. }
  22. std::cout << "Tong cac so nguyen to tu 1 den " << n << " la: " << T << std::endl;
  23. std::cout << "So luong so nguyen to tu 1 den " << n << " la: " << D << std::endl;
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Nhap vao so nguyen n: Tong cac so nguyen to tu 1 den 21905 la: 25237919
So luong so nguyen to tu 1 den 21905 la: 2456