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;

#define MP 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 > MP){
	large = MP;
      }     
      if(small < 0){
	small = 0;
      }
      int X = Prime[large] - Prime[small] + prime[small];
      if(X == 0){
	ans--;
      }else{
	ans += X-1;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

int main () {
	vector<int> furui(1000001, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i < 1000001; i++) {
		if (furui[i] == 0) {
			for (int j = 2; j <= 1000000/i; j++) {
				furui[i*j] = 1;
			}
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int counter = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			int counter2 = 0;
			for (int j = p-m; j <= p+m; j++) {
				if (j < 0) {
					j = 0;
				} else if (j > 1000000) {
					break;
				}
				if (furui[j] == 0) {
					counter2++;
				}
			}
			counter += (counter2-1);
		}
		cout << counter << endl;
	}
	return 0;
}

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

using namespace std;

int const MP = 999983;

bool is_prime[MP+100];
void Sieve() {
  
  fill(is_prime, is_prime+MP+100, true);
  is_prime[0] = is_prime[2] = false;
  
  for(int i=2; (long long)i*i <= MP; i++) {
    if(is_prime[i]) {
      for(int j=2*i; j<=MP; j+=i) {
	is_prime[j] = false;
      }
    }
  }
  
}

int main() {
  
  Sieve();
  
  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 += is_prime[i];
      }
      if(cnt == 0) cnt --;
      cnt --;
      X += cnt;
    }
    cout << X << endl;
  }
  
  return 0;
}

C++: #include <iostream>
using namespace std;
int a[1000000][2];
int main()
{
  int i,j,k,n,x,s;
  k = 0;
  for(i=2;i<1000000;i++){
    if(!a[i][0]){
      k++;
      for(j=2;i*j<1000000;j++){
	a[i*j][0] = 1;
      }
    }
    a[i][1] = k;
  }
  for(;;){
    cin >> n;
    if(n==0)break;
    s = 0;
    while(n--){
      cin >> i >> j;
      k = (i+j) >= 1000000 ? 999999 : i+j;
      x = a[k][1];
      k = (i-j-1) < 0 ? 0 : i-j-1;
      x -= a[k][1];
      if(x > 0) s += x-1;
      else s--;
    }
    cout << s << endl;
  }
  return 0;
}

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 c = sum[(ip+m+1)>MAX_P-1?MAX_P-1:(ip+m+1)] - sum[(ip-m)>0?(ip-m):0];
      if(c) res += c-1;
      else res--;
    }
    printf("%d\n", res);
  }
  return 0;
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
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;
}


#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 MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

const int maxn=1000010;
int f[maxn],prime[maxn];
 
int GenP() {
	int i,tot,p,tmp;
	tot=0;
	for (i=2;i<maxn;++i) f[i]=i;
	for (p=2;p<maxn;++p)
	{
		if (f[p]==p) prime[tot++]=p;
		for (i=0;i<tot && prime[i]<=f[p];++i)
		{
			tmp=prime[i]*p;
			if (tmp<maxn) 
				f[tmp]=prime[i];
			else
				break;
		}
	}
}
int a[1000010];
void mainmain(){
	GenP();
	int t=0;
	reep(i,1,1000010){
		a[i]=a[i-1];
		if(prime[t]==i){
			t++;
			a[i]++;
		}
	}
	int n;
	while(cin>>n,n){
		int ans=0;
		rep(i,n){
			int p,m;
			cin>>p>>m;
			int x=p-m,y=p+m;
			if(x<1) x=1;
			if(y>1000000) y=1000000;
			ans+=a[y]-a[x-1]-1;
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<iostream>
using namespace std;
bool isP[2000001];
int sumP[2000001];
int N=2000000;

void make_table();

int n,p,m,sum,X;
int main(){
  make_table();
  while(cin>>n&&n){
    sum=0;
    for(int i=0;i<n;i++){
      cin>>p>>m;
      X=sumP[p+m];
      if(p-m-1>=0)X-=sumP[p-m-1];
      if(X>0)sum+=(X-1);
      else sum--;
    }
    cout<<sum<<endl;
  }
  return 0;
}
void make_table(){
  for(int i=0;i<=N;i++){isP[i]=true;sumP[i]=0;}
  isP[0]=isP[1]=false;
  for(int i=2;i*i<=N;i++){
    if(isP[i]==false)continue;
    for(int j=i*i;j<=N;j+=i)isP[j]=false;
  }

  for(int i=1000003;i<=N;i++)isP[i]=false;

  for(int i=1;i<=N;i++){
    sumP[i]=sumP[i-1]+isP[i];
  }
}