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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

const int PrimeMax = 1000000;
int is_prime[PrimeMax],sum[PrimeMax];
void Eratosthenes(int N){
  for(int i = 0; i < N; i++){
    is_prime[i] = 1;
  }
  is_prime[0] = 0;
  is_prime[1] = 0;
  for(int i = 2; i*i < N ; i++){
    if(is_prime[i]){
      for(int j = 0; i * (j + 2) < N; j++){
        is_prime[i *(j + 2)] = 0;
      }
    }
  }
  REP(i,1,N){
    sum[i] += sum[i-1] + is_prime[i];
  }
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  Eratosthenes(PrimeMax);
  while(cin>>n && n){
    ll p,m,ans = 0;
    rep(i,n){
      ll f,s;
      cin >> p>>m;
      if(p - m < 0){
        s = 0;
      }
      else{
        s = p -m;
      }
      f = (p+m > PrimeMax) ? PrimeMax-1: p + m;
      if(s == f){
        if(!is_prime[s])
          ans -= 1;
      }
      else if(sum[s]- sum[f] == 0){
        ans -= 1;
      }
      else{
        ans += (sum[f]-sum[s])-1;
      }
    }
    if(ans)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

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

const long N=1000000;

int main(){
	bool prime[N+1];
	
	for(long i=0; i<=N; ++i) prime[i]=true;
	prime[0]=prime[1]=false;
	for(long i=2; i<=N; ++i){
		if(prime[i]) for(long j=2; i*j<=N; ++j) prime[i*j]=false;
	}
	
	while(1){
		int n;
		scanf(" %d", &n);
		if(n==0) break;
		
		long ans=0;
		for(int i=0; i<n; ++i){
			long p, m;
			scanf(" %ld %ld", &p, &m);	
			
			long x=0;
			for(long j=p-m; j<=p+m; ++j){
				if(j<2) continue;
				if(j>N) break;
				
				if(prime[j]) x++;
			}
			
			ans+=x-1;
		}	
		
		printf("%ld\n", ans);
	}
}

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

bool li[1000001];

void calpri();
int calx(int p, int m);
int money(int x);

int main(){
	int n, p[1000001], m[1000001], x[1000001], summoney = 0;
	while(1){
	cin >>n;										//IÊÌüÍ
	if(n == 0) break;								//nª0Åbreak
	for(int i = 0; i < n; i++){						//nÂÌIÊÌîñ
		cin >>p[i] >>m[i];
		x[i] = calx(p[i], m[i]);
		summoney += money(x[i]);
		}
		cout <<summoney <<endl;
	}
	
	return 0;
}

void calpri(){										//GgXelXÌâ¿Éæèboolì
	int max = 1000001, min = 2;						//iªf=true fÅÍÈ¢=false
	li[0] = false;
	li[1] = false;
	
	for(int i = 2; i <= max; i++){
		li[i] = 1;
	}
	
	for(int i = 2; i <= max; ){
		for(int j = (i + 1); j <= max; j++){
			if(j % i == 0){
				li[j] = false;
			}
		}
		do{
			i++;
		}while(!li[i]);
	}
}


int calx(int p, int m){								//zñliÌtrueÌ©çfJEg
	int nop = 0, max, min;
	if((p + m) > 1000000) max = 1000000;
	else max = p + m;
	min = p - m;
	for(int i = min; i <= max; ){
		if(li[i]){
			nop++;
		}
		do{
			i++;
		}while(!li[i]);
	}
	return nop;
}

int money(int x){							//ÜàvZ
	if(x == 0) return 0;
	else return(x - 1);
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MP 999983

bool is_prime[MP+1];

int s_prime[MP+1];

int count_prime(int l, int r) {
    if(l<=0) l=1;
    if(r>MP) r=MP;
    return s_prime[r] - s_prime[l-1];
}

int main() {
    fill(&is_prime[2], &is_prime[MP+1], true);
    for(int p=2; p*p<=MP; p++) {
        if(is_prime[p]) {
            for(int i=p*p; i<=MP; i+=p) {
                is_prime[i] = false;
            }
        }
    }
    for(int i=1; i<=MP; i++) s_prime[i] = s_prime[i-1] + (is_prime[i] ? 1 : 0);
    
    int n;
    while(cin>>n, n) {
        int res = 0;
        for(int i=0; i<n; i++) {
            int p, m;
            cin >> p >> m;
            res += count_prime(p-m, p+m) - 1;
        }
        cout << res << endl;
    }
    
    return 0;
}

C++: #include<iostream>
#define N 1000000
using namespace std;

bool li[N + 1];

void calpri();
int calx(int p, int m);
int money(int x);

int main(){
	calpri();
	cout <<"ok" <<endl;
	int n, p, m, x, summoney;
	while(1){
	cin >>n;										//IÊÌüÍ
	if(n == 0) break;
	summoney = 0;									//nª0Åbreak
	for(int i = 0; i < n; i++){						//nÂÌIÊÌîñ
		cin >>p >>m;
		x = calx(p, m);
		summoney += money(x);
		}
		cout <<summoney <<endl;
	}
	return 0;
}

void calpri(){										//GgXelXÌâ¿Éæèboolì
	int max = N, min = 2;						//iªf=true fÅÍÈ¢=false
	li[0] = false;
	li[1] = false;
	
	for(int i = 2; i <= max; i++){
		li[i] = 1;
	}
	
	for(int i = 2; (i * i) <= max; ){
		for(int j = (i + 1); j <= max; j++){
			if(j % i == 0 && li[j] != false){
				li[j] = false;
			}
			while(!li[max]){
				max--;
			}
		}
		do{
			i++;
			if(i > max) break;
		}while(!li[i]);
	}
}

int calx(int p, int m){								//zñliÌtrueÌ©çfJEg
	int nop = 0, max, min;
	if((p + m) > (N + 1)) max = N;
	else max = p + m;
	if(p - m < 0) min = 0;
	else min = p - m;
	for(int i = min; i <= max; ){
		if(li[i]){
			nop++;
		}
		do{
			i++;
			if(i > N) break;
		}while(!li[i]);
	}
	return nop;
}

int money(int x){									//ÜàvZ
	if(x == 0) return -1;
	else return(x - 1);
}