View Code of Problem 27

#include<bits/stdc++.h>
using namespace std;
int main() {
    int a[65537];
    int n, m;
    a[0] = -1;
    a[1] = -1;
    a[2] = 2;
    for (int i = 2; i < 65537; ++i) {
        if (a[i] != -1) {
            a[i] = i;
        }
        for (int j = 2; j * i < 65537; ++j) {
            a[j * i] = -1;
        }
    }
    while (cin >> n >> m) {
        if (n>m){
            swap(n,m);
        }
        int sum = 0;
        for (int i = n+1; i < m; ++i) {
            if (a[i] != -1) {
                sum = sum + a[i];
            }
        }
        cout << sum << endl;
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 27