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 <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

vector<int> eratosthenes(int n) {
  vector<int> v(n);
  for (int i=2; i<n; i++) v[i] = i;
  for (int i=2; i*i<n; i++)
    if (v[i])
      for (int j=i*i; j<n; j+=i)
        v[j] = 0;
  return v;
}

int main() {
  vector<int> prime = eratosthenes(1000000);
  vector<int> psum(2000000);
  int sum = 0;
  REP(i,1000000) {
    if (prime[i]) ++sum;
    psum[i] = sum;
  }
  int n;
  while(cin >> n, n) {
    int res = 0;
    REP(i,n) {
      int p, m;
      cin >> p >> m;
      
      int ue = min(999983, p+m);
      int shita = max(0, p-m-1); 
      int X = psum[ue] - psum[shita];
      // cout << ue << " " << psum[ue] << endl;
      // cout << shita << " " << psum[shita] << endl;
      if (X)
        res += X-1;
      else
        --res;
    }
    cout << res << 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 = 999984;
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 <iostream>
#include <memory>
#include <memory.h>
#include <cmath>
#include <fstream>
#include <cmath>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>
#define USE_MATH_DEFINES
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

bool prime[2000000];

int main(){
	fill(prime, prime + 2000000, true);
	prime[0] = prime[1] = false;
	for(int i = 2; i * i < 2000000; i++){
		if(prime[i]){
			for(int j = 2 * i; j < 2000000; j += i) prime[j] = false;
		}
	}

	int n, x, p, m;
	while(cin >> n && n){
		x = 0;
		rep(i, n){
			cin >> p >> m;
			x--;
			rep2(j, p - m, p + m + 1){
				if(j <= 999983 && prime[j]) x++;
			}
		}
		cout << x << endl;
	}
	return 0;
}

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

void eratos ( int n, bool prime[]){
    for ( int i = 0; i <= n; i++ ) prime[i] = false;
    for ( int i = 3; i <= n; i += 2 ) prime[i] = true;
    prime[2] = true;
    int limit = (int)sqrt((double)n) + 1;
    for ( int i = 3; i <= limit; i += 2 ){
	if ( !prime[i] ) continue;
	for ( int j = i + i; j <= n; j += i ) prime[j] = false;
    }
}

int main(void){
    bool prime[MAX + 1];
    eratos( MAX, prime );

    int T[MAX+1];
    for ( int i = 0; i <= MAX; i++ ) T[i] = 0;
    for ( int i = 1; i <= MAX; i++ ){
	T[i] = T[i-1];
	if ( prime[i] ) T[i]++;
    }

    int n, p, m, x, ans;
    while( cin >> n && n ){
	ans = 0;
	for ( int i = 0; i < n; i++ ){
	    cin >> p >> m;
	    x = 0;
	    int left = max(0, p-m);
	    int right = min(p+m, MAX);

	    x += T[right] - T[left];
	    if ( prime[left] ) x++;

	    /*
	    for ( int j = max(0, p-m); j <= min(p+m,MAX); j++ ){
		if ( !prime[j] ) continue;
		if ( p-m <= j && j <= p+m ) x++;
		}
	    */
	    if ( x == 0 )  ans--;
	    else ans += x-1;

	}
	cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define inf (1<<29)
#define MAX 10000004
using namespace std;

vector<int> prime;
bool isntprime[MAX+1]; 
void init()
{	
  isntprime[0] = isntprime[1] = true;
  int j;	
  for(int i=2;i<= MAX;i++)if(!isntprime[i])for(prime.push_back(i),j=2*i;j<=MAX;j+=i)isntprime[j] = true;
}

int main()
{
  init();
  int n;
  while(cin >> n,n)
    {
      int p,m;
      int ans = n/101;
      rep(i,n)
	{
	  cin >> p >> m;
	  int X = 0;
	  REP(j,max(0,p-m),min(999983,p+m)+1)
	    {
	      if(!isntprime[j])X++;
	    }
	  if(X == 0)ans--;
	  else      ans += X-1;
	}
      cout << ans << endl;
    }
  return 0;
}