View Code of Problem 27

#include<iostream>
#include<string>
#include<vector>
#include<vector>
using namespace std;
bool st[100005];

int prime[100005];
int cnt = 0;
void getprime(int n) {
	for (int i = 2; i <= n; i++) {
		if (!st[i])prime[cnt++] = i;
		for (int j = 0; prime[j] <=n/i; j++) {
			st[prime[j] * i] = true;
			if (i%prime[j] == 0)break;
		}
	}
}

int main()
{
	int a, b;
	getprime(100000);
	st[0] = true;
	st[1] = true;
	while (cin >> a >> b) {
		if (a > b) {
			swap(a, b);
		}
		int num=0;
		for (int i = a+1; i < b; i++) {
			if (!st[i]) {
				num += i;
			}
		}
		cout << num << endl;
	}

	return 0;
}

Double click to view unformatted code.


Back to problem 27