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<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
#define MAX 1000100
  bool num[MAX]={1,1,0};
  for(int i=3;i<MAX;i+=2){
    if(num[i])continue;
    for(int j=i*3;j<MAX;j+=i*2){
      num[j]=1;
    }
  }
  num[1000003]=1;

  int n;
  while(cin>>n,n){
    int sum=0;
    rep(i,n){
      int a,b;
      int co=0;
      cin>>a>>b;
      if(a-b==2)co++;
      for(int j=a-b+(a-b+1)%2;j<a+b+1;j+=2){
	if(j<2)continue;
	if(j>1000000)break;
	if(a-b==2)co++;
	if(j>2&&!num[j])co++;
      }
      //cout<<co<<endl;
      if(co)sum+=co-1;
    }
    cout<<sum<<endl;
  }

}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int N;
int BIT[6000000];

void add(int a, int w){
	
	// a?????????????´????w????¶???? O(log N)
	
	for(int x = a; x <= N; x += x & -x){
		BIT[x] += w;
	}
}

int sum(int a){
	
	// 1??????a?????§???????´????????¨?????????? O(log N)
	
	int ret = 0;
	for(int x = a; x > 0; x -= x & -x){
		ret += BIT[x];
	}
	return ret;
}

int main(){
	
	N = 1000000;
	
	bool a[1000000];
	
	for(int i = 0; i < N; i++){
		a[i] = true;
	}
	
	a[0] = false;
	a[1] = false;
	
	for(int i = 2; i < N; i++){
		if(a[i]){
			for(int j = i * 2; j < N; j += i){
				a[j] = false;
			}
		}
	}
	
	for(int i = 0; i < 6000000; i++){
		BIT[i] = 0;
	}
	
	for(int i = 0; i < N; i++){
		if(a[i]){
			add(i, 1);
		}
	}
	
	while(true){
		
		int n;
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		int ans = 0;
		
		for(int i = 0; i < n; i++){
			int p, m, hit_num;
			cin >> p >> m;
			if(p - m <= 2){
				hit_num = sum(p + m);
			}else{
				hit_num = sum(p + m) - sum(p - m - 1);
			}
			
			ans = ans + hit_num - 1;
			
		}
		
		if(ans < 0){
			ans = 0;
		}
		
		cout << ans << endl;
		
	}
	
	return 0;
}

C++: //include
//------------------------------------------
#include <bits/stdc++.h> // only for GCC

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

//container util
//------------------------------------------
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

//repetition
//------------------------------------------
#define loop(i,a,b) for(ull i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define riter(i,c) for(auto i=(c).rbegin(); i!=(c).rend(); ++i)

//constant
//--------------------------------------------
const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

// output vector
// ------------------------------------------
namespace std {
    template<typename T>ostream&operator<<(ostream&os, vector<T>const&v){
        iter(it,v) os<<*it<<(it+1==v.end()?"":",");
        return os;
    }
    template<typename T, typename U>ostream&operator<<(ostream&os,const pair<T,U>&p){
        return os << "[" << p.dirst << "," << p.second << "]";
    }
}

//clear memory
// ------------------------------------------
#define clr(a,i) memset((a), (i) ,sizeof(a))

int lcm(int a,int b){
   return a*b/__gcd(a,b);
}
 
vb pn(999984,1);
vi pnn(999984,0); 
void prime_number(ull n){
   pn[0]=false;pn[1]=false;
   loop(i,2,n){
      pnn[i]=pnn[i-1]+(int)pn[i];
      if(!pn[i]) continue;
      for(ull j=i*i;j<=n;j+=i){
         pn[j]=false;
      }
   }
}

int main(){
   ull n,s,g;
   ll p,m;
   int x;
   prime_number(999984);
   while(cin >> n&&n){
      x=0;
      rep(i,n){
         cin>>p>>m;
         if(p-m<2) s=1;
         else s=p-m-1;
         if(p+m>999983) g=999983;
         else g=p+m;
         x+=pnn[g]-pnn[s]-1;
      }
      cout << x << endl;
   }
   return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pii;

int prime[1000000];
bool is_prime[1000001];

void sieve(int n){
	int p=0;
	for(int i=0;i<=n;i++) is_prime[i]=true;
	is_prime[0]=is_prime[1]=false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++]=i;
			for(int j=2*i;j<=n;j+=i) is_prime[j]=false;
		}
	}
}

int d[1000001];

int main(){
	int n,p,m;
	sieve(1000000);
	rep(i,1,1000001){
		d[i]=d[i-1];
		if(is_prime[i]) d[i]++;
	}
	while(1){
		int ans=0;
		cin>>n;
		if(n==0) break;
		rep(i,0,n){
			cin>>p>>m;
			int c=0;
			c=d[min(p+m,1000000)]-d[max(1,p-m-1)];
			//o(c);
			if(c==0) ans--;
			else ans+=c-1;
		}
		o(ans);
	}
}