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>
using namespace std;

bool nonprime[1000001] = {};
int accum[1000001] = {};

int main(){
    nonprime[0] = nonprime[1] = true;
    for(int i = 2; i <= 1000000; i++){
        if(!nonprime[i]){
            for(int j = i+i; j <= 1000000; j+=i){
                nonprime[j] = true;
            }
        }
    }

    for(int i = 2; i <= 1000000; i++){
        if(nonprime[i-1]){
            accum[i] = accum[i-1];
        }else{
            accum[i] = accum[i-1] + 1;
        }
    }

    while(1){
        int n;
        cin >> n;
        if(n == 0)  break;

        int ans = 0;

        for(int i = 0; i < n; i++){
            int p, m;
            cin >> p >> m;

            int cnt = accum[min(p+m, 1000000)] - accum[max(0, p-m)];
            if(p+m <= 1000000 && !nonprime[p+m])    cnt++;

            if(cnt == 0)    ans--;
            else            ans += cnt-1;
        }
        
        cout << ans << 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;
		
		for(;pair_num--;){
			int p,m;
			int x;
			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++: 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,mn,mx;
	while(scanf("%d",&n),n){
		S=0;
		for(int j=0;j<n;j++){
			scanf("%d%d",&p,&m); mn=p-m; if(mn<2)mn=2; mx=p+m; if(mx>M-1)mx=M-1;
			c=0; for(int i=mn;i<=mx;i++)if(!P[i])c++;
			if(c>0)c--; else c=-1;
			S+=c;
		}
		printf("%d\n",S);
	}
	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;
		for(;pair_num--;){
			int p,m;
			cin>>p;
			cin>>m;
			int x=0;
			int a,b;
			if(p+m>MP){
				a=MP;
			}else{
				a=p+m;
			}
			if(p-m<0){
				b=0;
			}else{
				b=p-m;
			}
			x=prime_num[a]-prime_num[b];
			
			if(x==0){
				ans--;
			}else{
				//x--;
				ans=ans+x;
			}
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

using namespace std;

bool era[1000001];
int pnum[1000001];

int main(){
	era[0]=era[1]=true;
	for(int i=0;i<=1000;i++){
		if(!era[i])	for(int j=i*i;j<=1000000;j+=i)	era[j]=true;
	}
	for(int i=1;i<=1000000;i++)	pnum[i]=pnum[i-1]+!era[i];

	for(int n;scanf("%d",&n),n;){
		int prz=-n;
		while(n--){
			int p,m;	scanf("%d%d",&p,&m);
			prz+=pnum[min(p+m,1000000)]-pnum[max(0,p-m-1)];
		}
		printf("%d\n",prz);
	}

	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); 
void prime_number(ull n){
   pn[0]=false;pn[1]=false;
   loop(i,2,n/2+1){
      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(999983);
   while(cin >> n&&n){
      x=0;
      rep(i,n){
         cin>>p>>m;
         if(p-m<2) s=2;
         else s=p-m;
         if(p+m>999983) g=999984;
         else g=p+m+1;
         x+=(count(pn.begin()+s,pn.begin()+g,1)-1);
      }
      cout << x << endl;
   }
   return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX_P 999983
#define MAX 1000000
int Prime[MAX+1];
bool prime[MAX+1];

void make(){
  fill(prime,prime+MAX,true);
  prime[0] = prime[1] = false;

  for(int i = 2 ; i*i <= MAX ; i++){
    if(prime[i]){
      for(int j = i*i ; j <= MAX ; j += i){
	prime[j] = false;
      }
    }
  }

  int sum = 0;
  for(int i = 0 ; i <= MAX ; i++){
    if(prime[i]){
      sum++;
    }
    Prime[i] = sum;
  }
}

int main(){
  int n;

  make();

  while(cin >> n ,n){
    int p,m,ans = 0;
    for(int i = 0 ; i < n ; i++){
      cin >> p >> m;

      int large = p + m;
      int small = p - m;

      if(large > MAX_P){
	large = MAX_P;
      }     
      if(small < 0){
	small = 0;
      }

      if(!prime[large] && !prime[small] &&
	 Prime[large] == Prime[small]) continue;

      ans += Prime[large] - Prime[small];
      if(Prime[large] || Prime[small]) ans--;
    }
    cout << ans << endl;
  }

  return 0;
}