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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)
bool prime[1000000];
int a[1000000];
void mainmain(){
    int n;
    rep(i,1000000){
        prime[i]=true;
    }
    prime[0]=prime[1]=false;
    for(int i=2;i*i<=1000000;i++){
        if(prime[i]){
            for(int j=i*i;j<1000000;j+=i){
                prime[j]=false;
            }
        }
    }
    rep(i,1000000){
        if(!i) continue;
        a[i]=a[i-1];
        if(prime[i]) a[i]++;
    }
    while(cin>>n){
        if(!n) return;
        ll ans=0;
        rep(i,n){
            int x,y;
            cin>>x>>y;
            int l=max(1,x-y);
            int r=min(1000000-1,x+y);
            int t=a[r]-a[l-1]-1;
            // cout<<x<<" "<<y<<" "<<t<<" "<<l<<" "<<r<<endl;
            ans+=t;
        }
        cout<<ans<<endl;
    }
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
      cout<<fixed<<setprecision(20);
    mainmain();
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int p[1000001];
int main(){
for(int i=0;i<1000001;i++)p[i]=1;
p[0]=p[1]=0;
for(int i=2;i<1000001;i++)
if(p[i])
for(int j=i+i;j<1000001;j+=i)
p[j]=0;
for(int i=1;i<1000001;i++)p[i]+=p[i-1];
int a;while(scanf("%d",&a),a){
long long r=0;
while(a--){
int b,c;
scanf("%d%d",&b,&c);
r+=p[min(b+c,1000000)]-p[max(0,b-c-1)]-1;
}printf("%lld\n",r);}}
}
}

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(1000001,1); 
void prime_number(ull n){
   //fill(all(pn),1);
   pn[0]=false;pn[1]=false;
   loop(i,2,n/2+1){
      if(!pn[i]) continue;
      for(ull j=2;i*j<=n;j++){
         pn[i*j]=false;
      }
   }
}

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

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

using namespace std;

int main()
{
  int n, m, p, s, t;
  bool a[1000000];
  fill(a, a+1000000, true);
  a[0] = a[1] = false;
  vector<int> vec;

  for(int i = 2; i < 1000000; i++){
    if(a[i]){
      vec.push_back(i);
      for(int j = i+i; j < 1000000; j += i){
	a[j] = false;
      }
    }
  }

  while(cin>>n && n){
    set<int> stt;
    while(n--){
      cin >> p >> m;
      s = p-m;
      if(s < 0){
	s = 0;
      }
      t = p+m;
      if(t > 999983){
	t = 999983;
      }
      
      for(int i = 0; i < vec.size() && vec[i] <= t; i+=1){
	if(s <= vec[i] && vec[i] <= t){
	  stt.insert(vec[i]);
	}
      }
    }

    cout << ((stt.size() > 1)? (stt.size()-1): 0) << endl;
  }
  
  return 0;
}