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++: //42
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  static bool cmp[1000000]={true,true};
  for(int i=2;i<1000000;i++){
    if(!cmp[i]){
      for(int j=2;j*i<1000000;j++){
	cmp[i*j]=true;
      }
    }
  }
  for(int n;cin>>n,n;){
    int k=0;
    for(int i=0;i<n;i++){
      int p,m;
      cin>>p>>m;
      k--;
      for(int j=max(0,p-m);j<=min(p+m,999999);j++){
	k+=!cmp[j];
      }
    }
    cout<<k<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983+100
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MP;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		int x;
		for(;pair_num--;){
			int p,m;
			cin>>p;
			cin>>m;
			
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			
			if(x<0){
				ans--;
			}else{
				x--;
				//ans=ans+x;
			}
		}
		cout<<ans<<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++: #include "bits/stdc++.h"
using namespace std;

vector<int>primes(1e8);
vector<int>sums(1e8 + 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 < 1e7; ++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>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 1000000
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	
	int prime_num[MAX_LIST];
	/*
	prime_num[0]=1;
	for(int i=1;i<MAX_LIST;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}*/
	
	/*
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(;pair_num--;){
			int p,m;
			int x;
			cin>>p>>m;
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			if(x<0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
		}
		cout<<ans<<endl;
	}*/
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

int main(void){
    int n;
    bool isprime[1000001];
    vector<int> primes;

    //sieve
    memset(isprime, true, sizeof(isprime));
    for(int i=2;i<=1000000;i++){
        if(isprime[i] == true){
            primes.push_back(i);
            for(int j=2;i*j<=1000000;j++){
                isprime[i*j] = false;
            }
        }
    }

    int prize,ret,p,m;
    while(cin >> n && n){
        ret = 0;
        prize = 0;
        for(int i=0;i<n;i++){
            cin >> p >> m;
            prize = upper_bound(primes.begin(), primes.end(),p+m) - 
                lower_bound(primes.begin(),primes.end(),p-m);
            
            if(prize == 0){
                ret -= 1;
            }else{
                ret += prize-1;
            }
        }
        if(ret <= 0){
            cout << 0 << endl;
        }else{
            cout << ret << endl;
        }
    }
}

C++: #include<iostream>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	/*
	int primelist[MAX_LIST];
	int j=0;
	for(int i=0;i<MAX_LIST;i++){
		if(prime[i]==1){
			primelist[j]=i;
			j++;
			if(j>60000){
				break;
			}
		}
	}*/
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MAX_LIST;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(;pair_num--;){
			int p,m;
			int x;
			cin>>p>>m;
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			if(x<0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}