View Code of Problem 27

#include <iostream>
#include <valarray>

using namespace std;

/**
 * kkmd66 四刷
 * @return
 */

int main() {

    int a, b;
    while (cin >> a >> b) {
        //判断大小,否则无法通过
        if (a > b)
            swap(a, b);

        //找素数,并求和
        long long sum = 0;
        for (int i = a + 1; i < b; ++i) {
            //判断是否为素数
            bool flag = true;
            for (int j = 2; j <= sqrt(i); ++j) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            //是素数,累加
            if (flag && i > 1)
                sum += i;
        }
        //输出
        cout << sum << endl;
    }

    return 0;
}

Double click to view unformatted code.


Back to problem 27