View Code of Problem 27

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int x) {
    if (x <= 1) return false; // 1不是质数也不是合数
    if (x == 2) return true;
    for (int i = 2; i <= sqrt(x); ++i) {
        if (x % i == 0) return false; // 如果能被整除,则不是质数
    }
    return true;
}

int main() {
    int a, b;
    while (cin >> a >> b) {
        long long sum = 0;
        for (int i = a + 1; i < b; ++i) {
            if (isPrime(i)) {
                sum += i;
            }
        }
        cout << sum << endl;
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 27