My code is below. It works fine until the gap is very large. However, whenever I switch to a million, the program stops. for example, for intervals from 2 to 2,000,000, the sum of primes should be 142913828922 (on the back of the book), while my application shows 1179908154. Maybe someone can indicate where I made a mistake.
#include <iostream> #include <math.h> #define BELOW 2000000; using namespace std; bool isPrime(int num) { int i; for (i = 2; i <= sqrt(num); i++) { if (num % i == 0) { return 0; } else { ; } } } int main() { long sum = 0; for (int i = 2; i < BELOW i++) { if (isPrime(i)) { sum = sum + i; printf("sum: %ld\n", sum); } } cin.get(); return 0; }
source share