View Code of Problem 27

#include <bits/stdc++.h> 
using namespace std;

int prime(int x){
    if (x == 0 || x == 1)
        return 0;
    if (x == 2)
        return 1;
    for (int j = 2; j * j <= x; j++) {
        if (x % j == 0) {
            return 0;
        }
    }
    return 1;
}

int main(){
    int a, b;
    long long sum = 0;
    while (cin >> a >> b) {
        sum = 0;
        if (a > b) { // 正确的判断条件应该是a>b
            swap(a, b);
        } 
        for (int i = a + 1; i < b; i++) {
            if (prime(i) == 1) {
                sum += i;
            }
        }
        cout << sum << endl;
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 27