Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const (
	MAX  = 1000000
	SQRT = 1000
)

func main() {
	comp := make([]int, MAX+2)
	sieve := func() {
		for i := 3; i < SQRT; i += 2 {
			if comp[i] == 0 {
				for j := i * i; j < MAX; j += i {
					comp[j] = 1
				}
			}
		}
	}
	sieve()
	tbl := make([]int, MAX+2)
	tbl[2] = 1
	k := 1
	for i := 3; i < MAX; i += 2 {
		if comp[i] == 0 {
			k++
		}
		tbl[i+1], tbl[i] = k, k
	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		ans := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			p, _ := strconv.Atoi(buf[0])
			m, _ := strconv.Atoi(buf[1])
			a, b := p-m, p+m
			if a < 2 {
				a = 2
			}
			if b > MAX {
				b = MAX
			}
			ans += tbl[b] - tbl[a-1] - 1
		}
		if ans < 0 {
			ans = 0
		}
		fmt.Println(ans)
	}
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;


int prime[MAX_PRIME+1];
bool isPrime[MAX_PRIME+1];
int p;

void erats(){
	// primeツδ環スツトツづーツ渉可甘コツ可サ
	fill(isPrime,isPrime+MAX_PRIME,true);
	p = 0;
	isPrime[0] = isPrime[1] = false;

	for(int i = 2; i <= MAX_PRIME; i++){
		if(isPrime[i]){
			prime[p++] = i;
		}
		else
			continue;

		// ツづ督づゥツつ「ツづ可つゥツつッツづゥ
		for(int j = 2*i; j <= MAX_PRIME; j+=i){
			isPrime[j] = false;
		}
		cout << flush;
	}
}


int main(){

	// ツ素ツ青板づーツ仰づ淞づゥ
	erats();

	int n;
	while(cin>>n&&n!=0){
		int sum=0;
		for(int i = 0; i < n; i++){
			int pp,mm;
			cin>>pp>>mm;
			int cnt=0;
			for(int j = 0; j < p; j++){
				if(prime[j]>=pp-mm&&prime[j]<=pp+mm){
					cnt++;
				}
				else if(prime[j]>pp+mm)
					break;
			}
			sum+=max(0,cnt-1);
			if(cnt==0)
				sum--;
		}
		cout<<max(0,sum)<<endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cmath>
using namespace std;


#define MAX 1000000
//素数配列
int h_sosu[MAX] = {0};
//素数flag
bool h_flag_sosu[MAX] = {false};

//----- 列挙関数 -----//
void f_sosu();


int main(void)
{
	//素数を列挙
	f_sosu();

	//入力変数
	int n = 0;

	while (true)
	{
		cin >> n;
		if (n == 0)	{	return 0;	}

		//答え変数(王が1払ってくれる)
		int ans = 0;
		//当たりくじ
		int atari = 0;
		//組
		int kumi = 0;

		//入力
		for (int i = 0; i < n; i++)
		{
			cin >> atari >> kumi;
			//ループ変数
			int j = atari - kumi;
			//終了
			int end = atari + kumi;
			//1は政府が支払う
			bool flag_in = false;

			//負数はいらない
			if (j < 0)	{	j = 0;	}
			//値が大きすぎるなら
			if (end >= 1000000)	{end = 999999;	}
			while (j <= end)
			{
				//flagがtrueなら足す
				if (h_flag_sosu[j] == true)
				{
					ans++;
					flag_in = true;
				}
				j++;
			}
			//1回でも入れば
			//if (flag_in == true)
			//{
				ans--;
			//}
		}

		cout << ans << endl;
	}

	return 0;
}



//*****----- 素数列挙(エラトステネスのふるい) -----*****//

void f_sosu()
{
	//2 , 3は加えておく
	register int count = 2;
	h_sosu[0] = 2 , h_sosu[1] = 3;
	h_flag_sosu[2] = true;
	h_flag_sosu[3] = true;
	//終了の値
	register int end = 0;
	//素数flag
	register bool flag_sosu = false;

	for (int i = 5; i < MAX; i += 2)
	{
		//flag初期化
		flag_sosu = true;
		//平方根までで良い
		end = sqrt((double)i);
		//カウント変数まで回す
		for (int j = 0; j < count; j++)
		{
			if (i % h_sosu[j] == 0)
			{
				flag_sosu = false;
				break;
			}
			if (h_sosu[j] > end)	{	break;	}
		}
		//素数なら
		if (flag_sosu == true)
		{
			h_sosu[count++] = i;
			h_flag_sosu[i] = true;
		}
	}
}

C++: using namespace std;
int M=1000001,n,p,m,c,S,i,j;
bool P[1000001];
int main(){
	for(i=2;i<M;i++)if(!P[i])for(j=i+i;j<M;j+=i)P[j]=1;
	P[0]=P[1]=1;
	while(cin>>n,n){
		for(S=0;n--;){
			cin>>p>>m;
			c=0; for(i=p-m>0?p-m:0;i<=p+m&&i<M;)if(!P[i++])c++;
			S+=c>0?c-1:-1;
		}
		cout<<S<<endl;
	}
	return 0;
}

C++: using namespace std;
int M=1000001;
bool P[1000001];
int main(){
	for(int i=2;i<1002;i++)if(!P[i])for(int j=i+i;j<M;j+=i)P[j]=1;
	P[0]=P[1]=1;
	int n,p,m,c,S;
	while(cin>>n,n){
		S=0;
		for(int j=0;j<n;j++){
			cin>>p>>m;
			c=0; for(int i=p-m>1?p-m:2;i<=p+m&&i<M;i++)if(!P[i])c++;
			S+=c>0?c-1:-1;
		}
		cout<<S<<endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;


int prime[MAX_PRIME+1];
bool isPrime[MAX_PRIME+1];
int p;

void erats(){
	// primeツδ環スツトツづーツ渉可甘コツ可サ
	fill(isPrime,isPrime+MAX_PRIME,true);
	p = 0;
	isPrime[0] = isPrime[1] = false;

	for(int i = 2; i <= MAX_PRIME; i++){
		if(isPrime[i]){
			prime[p++] = i;
		}
		else
			continue;

		// ツづ督づゥツつ「ツづ可つゥツつッツづゥ
		for(int j = 2*i; j <= MAX_PRIME; j+=i){
			isPrime[j] = false;
		}
		cout << flush;
	}
}


int main(){

	// ツ素ツ青板づーツ仰づ淞づゥ
	erats();

	int n;
	while(cin>>n&&n!=0){
		int sum=0;
		for(int i = 0; i < n; i++){
			int pp,mm;
			cin>>pp>>mm;
			int cnt=0;

			int *lb = lower_bound(prime,prime+p,pp-mm);
			for(int *it = lb;; it++){
				if(*it<=pp+mm){
					cnt++;
				}
				else if(*it>pp+mm||*it>=999983)
					break;
			}

			//for(int j = 0; j < p; j++){
			//	if(prime[j]>=pp-mm&&prime[j]<=pp+mm){
			//		cnt++;
			//	}
			//	else if(prime[j]>pp+mm)
			//		break;
			//}
			sum+=max(0,cnt-1);
			if(cnt==0)
				sum--;
		}
		cout<<max(0,sum)<<endl;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

vector<int>primes(1e6+1);
vector<int>sums(1e6 + 1);
void hurui(const int amax) {
	static bool flag = false;
	if (flag)return;
	vector<int>sos;
	sos = vector<int>(amax + 1, true);
	sos[0] = false; sos[1] = false;
	for (int i = 2; i <= amax; ++i) {
		if (sos[i]) {
			for (int j = 2 * i; j <= amax; j += i)sos[j] = false;
		}
	}
	for (int i = 0; i <= amax; ++i) {
		if (sos[i]) {
			primes[i] = true;
		}
	}
	flag = true;
}


int main() {
	hurui(999983);
	for (int i = 0; i <1e6; ++i) {
		sums[i + 1] = sums[i] + primes[i + 1];
	}
	while (1) {
		int N; cin >> N;
		if (!N)break;
		long long int prize = 0;
		while (N--) {
			long long int p, m; cin >> p >> m;
			int amax = min(999983ll, p + m);
			int amin = max(0ll, p - m-1);
			const int aprize = sums[amax] - sums[amin];
			prize += aprize - 1;
		}
		cout << max(0ll,prize )<< endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
bool a[1000001];
int main() {
	fill(a, a + 1000001, true);
	a[0] = false; a[1] = false;
	for (int i = 2; i <= 999983; i++) {
		if (a[i]) {
			int j = 2;
			while (i*j <= 1000000) {
				a[i*j] = false;
				j++;
			}
		}
	}
	int n;
	while (cin >> n&&n != 0) {
		long long sum = 0;
		for (int i = 0; i < n; i++) {
			int p, m, X = 0, k;
			cin >> p >> m;
			k = (p - m);
			if ((p - m) < 0) k = 0;
			for (int j = k; (j <= p + m && j <= 1000000);j++ ) {
				if (a[j]) X++;
			}
			if (X == 0) sum -= 1;
			else sum += (X - 1);
		}
		if (sum < 0) cout << "0" << endl;
		else cout << sum << endl;
	}
}