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<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++: #include <iostream>
#include <cmath>
#define MAX 1000000
//78498
using namespace std;

bool prime[MAX+1];

int count(int p, int m){
  int tmp=0;
  for(int i=p-m;i<=p+m;i++){
    if(prime[i]) tmp++;
  }
  if(tmp==0){
    return -1;
  }else{
    return tmp-1;
  }
}

void eratos(){
  int n=MAX;
  for(int i=0;i<=n;i++) prime[i]=false;
  for(int i=3;i<=n;i+=2) prime[i]=true;
  prime[2]=true;
  int limit=(int)sqrt((double)n)+1;
  for(int i=3;i<=limit;i+=2){
    if(!prime[i]) continue;
    for(int j=i+i;j<=n;j+=i)
      prime[j]=false;
  }
}

main(){
  int n;
  eratos();
  while(1){
    cin >> n;
    if(n==0) break;
    int ans=0;
    for(int i=0;i<n;i++){
      int p, m;
      cin >> p >> m;
      ans+=count(p, m);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

const int SIZE = 1000001;
int T[SIZE];
bool P[SIZE];

int solve( int p, int m ) {
    int left = max( 0, p - m );
    int right = min( SIZE - 1, p + m );
    int t = T[right] - T[left];
    if ( left == right ) return P[left] ? 0 : -1;
    return t == 0 ? -1 : t - 1;
}

int main() {
    for ( int i = 0; i < SIZE; ++ i ) {
        T[i] = 0;
        P[i] = false;
    }
    for ( int i = 3; i < SIZE; i += 2 ) {
        P[i] = true;
    }
    P[2] = true;
    for ( int i = 3; i < SIZE; i += 2 ) {
        if ( ! P[i] ) continue;
        for ( int j = i + i; j < SIZE; j += i ) {
            P[j] = false;
        }
    }
    for ( int i = 1; i < SIZE; ++ i ) {
        T[i] = T[i-1];
        if ( P[i] ) T[i] ++;
    }
    
    int n;
    while ( cin >> n && n ) {
        int res = 0;
        for ( int i = 0; i < n; ++ i ) {
            int p, m;
            cin >> p >> m;
            res += solve( p, m );
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX 1000000
bool is_prime[MAX+1];
vector<int> prime;
void init() {
    memset(is_prime, 1, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= MAX; i++) 
        if (is_prime[i]) {
            prime.push_back(i);
            for (int j = i + i; j <= MAX; j += i) 
                is_prime[j] = false;
        }
}

int cnt(int p, int m) {
    typedef vector<int>::iterator vi;
    vi u = lower_bound(prime.begin(), prime.end(), p+m+1);
    vi l = lower_bound(prime.begin(), prime.end(), p-m);
    return u - l;
}

int main() {
    int n, p, m;
    init();
    while (cin >> n, n) {
        int sum = 0;
        while (n--) {
            cin >> p >> m;
            int prize = cnt(p, m) - 1;
            sum += prize;
        }
        cout << sum << endl;
    }
    return 0;
}

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

const int MAX = 1000000;
const int MAX_P = 999983;
int prseq[MAX] = {};
int count[MAX + 1] = {};

void prime(){
	prseq[0] = 1;
	
	for(int i = 2; i <= MAX; ++i){
		int k = i * 2;
		while(k <= MAX_P){
			prseq[k - 1] = 1;
			k += i;
		}
	}
	for(int i = MAX_P + 1; i <= MAX; ++i){
		prseq[i - 1] = 1;
	}
	
	for(int i = 1; i <= MAX; ++i){
		count[i] = count[i - 1] + (prseq[i - 1] ? 0 : 1);
	}
}

int main(){
	int n, p, m;
	
	prime();
	
	while(cin >> n, n){
		int sum = 0;
		for(int i = 0; i < n; ++i){
			cin >> p >> m;
			int x = count[p + m > MAX ? MAX : p + m] - count[p - m < 1 ? 0 : p - m - 1];
			sum += x - 1;
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

#define MP 999983

int n;
int p, m;

bool is_prime[MP + 100];

int main(){
	fill(is_prime, is_prime + MP + 100, 1);
	is_prime[0] = 0;
	is_prime[1] = 0;
	for (int i = 2; i <= MP; i++){
		if (is_prime[i]){
			for (int j = i * 2; j <= MP; j += i){
				
				is_prime[j] = 0;
			}
		}
	}
	while (cin >> n&&n){
		int ans = 0;
		REP(i, n){
			cin >> p >> m;
			for (int i = max(p - m, 2); i <= min(p + m, MP); i++){
				if (is_prime[i]){
					ans++;
				}
			}
		}
		cout << ans - n << endl;
	}
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;
int pr[1000003];
int main() {
 int i,j,n,p,m,f,e,g;
 for (i=2;i<1000000;i++) if (pr[i]==0) { pr[i]=2; for (j=i*2;j<1000000;j+=i) pr[j]=1;}
 for (i=2,j=0;i<1000000;i++) { j+=pr[i]>1; pr[i]=j; }
 while(cin >> n) {
	 if (n==0) break;
	 g=0;
	 for (;n>0;n--) {
      cin >> p >> m;
	  f=p-m<0 ? 0 : p-m;
	  e=p+m>999999 ? 999999 : p+m;
	  j=pr[e]-pr[f-1];
	  if (j==0) g--;  else g+=j-1;
	 }
	 cout << g << endl;
 }
 return 0;
 }