fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. bool isPrime(int n) {
  6.  
  7. //if number less than 2 return false.
  8. if (n < 2) return false;
  9.  
  10. //if number the number is 2, return true.
  11. if (n == 2) return true;
  12.  
  13. //if the number is divisible by 2 return false.
  14. if (n % 2 == 0) return false;
  15.  
  16. //calculating the square root of the number.
  17. int sqrt_n = sqrt(n);
  18.  
  19. for (int i = 3; i <= sqrt_n; i += 2) {
  20. if (n % i == 0) return false;
  21. }
  22.  
  23. return true;
  24. }
  25.  
  26. int main() {
  27. std::vector<int> primes;
  28.  
  29. // variable to store sum of prime numbers.
  30. int sum = 0;
  31.  
  32. // last variable to iterate and check for the prime number.
  33. int limit = 10000000;
  34.  
  35. for (int i = 5; i <= limit; i += 2) {
  36. if (isPrime(i) && i % 10 != 3) {
  37. primes.push_back(i);
  38. sum += i;
  39. }
  40. }
  41. std::cout << "Sum of primes from 5 to 10 million (excluding those ending with 3): " << sum << std::endl;
  42. std::cout << "Number of primes: " << primes.size() << std::endl;
  43. return 0;
  44. }
Success #stdin #stdout 3.19s 5280KB
stdin
Standard input is empty
stdout
Sum of primes from 5 to 10 million (excluding those ending with 3): 1120268000
Number of primes: 498348