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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30
#define MAX_P 1000000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;


int p[MAX_P], n, sum[MAX_P];

int main(){
  p[0] = p[1] = 1;
  REP(i, 2, MAX_P){
    sum[i] = sum[i-1];
    if(!p[i]){
      sum[i]++;
      for(int j = i+i; j < MAX_P; j += i) p[j] = 1;
    }
  }
  //  rep(i, 100) printf("%d ", sum[i]); puts("");

  while(scanf("%d", &n) && n){
    int ip, m, res = 0;
    rep(i, n){
      scanf("%d%d", &ip, &m);
      //      int c = 0;
      //      REP(j, (ip-m)>0?(ip-m):0, (ip+m+1)>MAX_P?MAX_P:(ip+m+1)) if(!p[j]) c++;
      int top = (ip+m)>MAX_P-1?MAX_P-1:(ip+m);
      int under = (ip-m-1)>0?(ip-m-1):0;
      int c = sum[top] - sum[under];
      if(c) res += c-1;
      else res--;
    }
    printf("%d\n", res);
  }
  return 0;
}

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

#define pb push_back
#define sz size()
#define clr clear()
#define M 999983

vector<bool> pt;
vector<int> pa;

void MakeTable(int n){
  pt.clr;
  pt.pb(false);pt.pb(false);pt.pb(true);
  
  while((int)pt.sz<=n+1){
    pt.pb(true);
    pt.pb(false);
  }
  
  int i = 3;
  while(i*i<=n){
    for(int j=3*i;j<=n;j+=2*i)pt[j]=false;
    do{i+=2;}while(!pt[i]);
  }
}

int main(){
  int n,p,m,pn[1000000];
  long long ans;

  MakeTable(M);
  int cnt = 0;
  for(int i=0;i<=M;i++){
    if(pt[i])cnt++;
    pn[i] = cnt;
  }

  for(;;){
    cin >> n;
    if(!n)break;

    ans = 0;
    for(int i=0;i<n;i++){
      cin >> p >> m;
      int l = max(0,p-m-1),r = min(M,p+m);

      if(pn[r]-pn[l]>0)ans += pn[r] - pn[l] - 1;
      else ans--;
    }
    cout << ans << endl;
  }
}

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 = 999983;
int is_prime[1000001],sum[1000001];
void Eratosthenes(){
  ll N = PrimeMax;
  for(int i = 2; 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 = i*2; j <= N; j+=i){
        is_prime[j] = 0;
      }
    }
  }
  ll c = 0;
  REP(i,0,N+1){
    if(is_prime[i]){
      ++c;
    }
    sum[i] = c;
  }
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  Eratosthenes();
  while(cin>>n && n){
    ll p,m,ans = 0;
    rep(i,n){
      ll f,s;
      cin >> p>>m;
      s = max(1LL,p-m);
      f = min(p+m,(ll)PrimeMax);
      ans += (sum[f]-sum[s-1])-1;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
#include <numeric>
#define rep(i,n) Rep(i,0,n)
#define Rep(i,k,n) for(int i=k ; i<n ; i++)
#define rep1(i,n) for(int i=1 ; i<=n ; i++)
#define Rep1(i,k,n) for(int i=k ; i<=n ; i++)
#define rrep(i,n) for(int i=n ; i>=0 ; i--)
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
//#define int ll
typedef long long ll;

const int md = 1000000007;
const int INF = 1<<30;
using namespace std;

//vi prime(1000000,0);
vector<bool> isp;

void makePrime(int n){
    isp.assign(n+1,true);
    int id = 0;
    for(int i=2 ; i <= n ; i++){
        if(!isp[i]) continue;

        for(int k = i*2 ; k <= n ; k += i){
            isp[k] = false;
        }
        /*
        prime[id] = i;
        id++;
        */
        
        //prime.pb(i);
    }
    //Sort(prime);
}

ll check(int a, int b){
    ll ans = 0;
    if(a-b <= 2) ans++;
    int s;
    if((a-b)%2 == 0) s = a-b+1;
    else s = a - b;

    for(int i = max(s,3); i <= min(a+b,999983) ; i += 2) {
        if(isp[i]) ans++;
    }
    return ans;
}
int main(){
    makePrime(999983);
    //cout << prime.size() << endl;
    //cout << binary_search(ALL(prime),2) << endl;
    int n; 
    while(true){
        cin >> n;
        if(n == 0) break;
        ll ans = 0;
        rep(i,n){
            int a,b; cin >> a >> b;
            //cout << check(a,b) << endl;
            int tmp = check(a,b);
            //cout << "check: " <<  tmp << endl;
            ans += check(a,b)-1;
            
        }
        cout << ans << endl;
    }
}

C++: // 2011/10/16 Tazoe

#include <iostream>
using namespace std;

void prime(bool prm[], int n)
{
	if(n>=1)
		prm[0] = false;
	if(n>=2)
		prm[1] = false;

	for(int i=2; i<n; i++)
		prm[i] = true;

	for(int i=2; i<n; i++)
		if(prm[i])
			for(int j=i+i; j<n; j+=i)
				prm[j] = false;
}

int main()
{
	bool prm[1000001];
	prime(prm, 1000001);

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

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

			int bgn;
			if(p-m<0)
				bgn = 0;
			else
				bgn = p-m;

			int end;
			if(p+m>1000000)
				end = 1000000;
			else
				end = p+m;

			for(int j=bgn; j<=end; j++)
				if(prm[j])
					sum++;

			sum--;

//			cout << sum << endl;

		}

		cout << sum << endl;
	}

	return 0;
}