fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. bool isPrime(int n) {
  6. if (n < 2) return false;
  7. if (n == 2) return true;
  8. if (n % 2 == 0) return false;
  9. int sqrt_n = sqrt(n);
  10. for (int i = 3; i <= sqrt_n; i += 2) {
  11. if (n % i == 0) return false;
  12. }
  13. return true;
  14. }
  15.  
  16. int main() {
  17. std::vector<int> primes;
  18. int sum = 0;
  19. int limit = 10000000;
  20. for (int i = 5; i <= limit; i += 2) {
  21. if (isPrime(i) && i % 10 != 3) {
  22. primes.push_back(i);
  23. sum += i;
  24. }
  25. }
  26.  
  27. std::cout << "Sum of primes from 5 to 10 million (excluding those ending with 3): " << sum << std::endl;
  28. std::cout << "Number of primes: " << primes.size() << std::endl;
  29. return 0;
  30. }
Success #stdin #stdout 2.5s 5304KB
stdin
Standard input is empty
stdout
Sum of primes from 5 to 10 million (excluding those ending with 3): 1120268000
Number of primes: 498348