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<algorithm>
using namespace std;
int x[2000000];

void prime(int p) {
	x[2] = 1;
	for (int i = 3; i < p; i += 2) {
		for (int j = 3; j * j <= i; j += 2) {
			if (i%j == 0) { goto E; }
		}
		x[i] = 1;
	E:;
	}
}

int n, m, c, sum;

int main() {
	prime(1000000);
	while (true) {
		sum = 0;
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m >> c;
			for (int j = max(0, m - c); j <= min(1000000, m + c); j++) {
				sum += x[j];
			}
			sum -= 1;
		}
		cout << sum << 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 = 1000100;
int is_prime[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,1000002,PrimeMax){
    is_prime[i] = 0;
  }
  REP(i,1,N){
    is_prime[i] += is_prime[i-1];
  }
}
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: p + m;
      if(is_prime[s]- is_prime[f] == 0){
        ans -= 1;
      }
      else{
        ans -= (is_prime[s]-is_prime[f]);
      }
    }
    cout << ans-1 << endl;
  }
  return 0;
}

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

// この問題での最大の素数
const int MP = 999983;

// is_p[i] := i が素数のとき true
bool is_p[MP+1] = {false,false};
// cnt_p[i] := i 以下の素数の数
int cnt_p[MP+1];

// エラトステネスのふるい
void sieve(){
	for(int i = 2 ; i <= MP ; i++ )
		is_p[i] = true;
	for(int i = 2 ; i*i <= MP ; i++ ){
		if( is_p[i] ){
			for(int j = i+i ; j <= MP ; j += i ){
				is_p[j] = false;
			}
		}
	}
	// 任意の i 以下の素数の個数を求める(i <= MP)
	int cnt = 0;
	for(int i = 0 ; i <= MP ; i++ ){
		if( is_p[i] ){
			cnt++;
		}
		cnt_p[i] = cnt;
	}
}

// [a,b] の素数の個数を返す.
int get_x(int a, int b){
	a = max(1,a);
	b = min(b,MP);
	return cnt_p[b] - cnt_p[a-1];
}

int main(){
	sieve();
	int n;
	while( cin >> n , n ){
		int ans = 0;
		for(int i=0 ; i < n ; i++ ){
			int p, m;
			cin >> p >> m;
			// x := [p-m,p+m] の素数の個数
			int x = get_x(p-m,p+m);
			ans += x-1;
		}
		cout << ans << endl;
	}
}

C++: #include "bits/stdc++.h"
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>
using namespace std;
bool isp[1<<20];
int cnt[1<<20];
main()
{
	for(int i=2;i<=1e6;i++)isp[i]=true;
	for(int i=2;i<=1e6;i++)
	{
		if(isp[i])
		{
			for(int j=i+i;j<=1e6;j+=i)isp[j]=false;
		}
	}
	for(int i=1;i<1<<20;i++)cnt[i]=cnt[i-1]+isp[i];
	int N;
	while(cin>>N,N)
	{
		int sum=0;
		for(;N--;)
		{
			int p,m;cin>>p>>m;
			int L=p-m-1,R=p+m;
			if(L<0)L=0;
			if(R>=1<<20)R=(1<<20)-1;
			int X=cnt[R]-cnt[L];
			sum+=X-1;
		}
		cout<<sum<<endl;
	}
}

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

using namespace std;

#define MP (999983)

int memo[MP+2];
bool isPrime(int x) {
  if(x < 2) return 0;
  for(int i=2; i*i<=x; i++) {
    if(x % i == 0) return memo[x] = 0;
  }
  
  return memo[x] = 1;
}

int main() {
  
  memset(memo, -1, sizeof(memo));
  
  
  int N;
  while(cin >> N && N) {
    int X = 0;
    for(int i=0; i<N; i++) {
      int p, m; cin >> p >> m;
      int L = max(p-m, 0), R = min(p+m, MP);
      int cnt = 0;
      for(int i=L; i<=R; i++) cnt += isPrime(i);
      //if(cnt == 0) cnt --;
      cnt --;
      X += cnt;
    }
    cout << X << endl;
  }
  
  return 0;
}