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 <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*2];
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 < N ; i++){
    if(is_prime[i]){
      for(int j = i*2; j < N; j+=i){
        is_prime[j] = 0;
      }
    }
  }
  REP(i,0,N){
    sum[i+1] += sum[i] + is_prime[i];
  }
  REP(i,N,2*N){
    sum[i+1] = sum[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;
      s = max(0LL,p-m-1);
      f = p+m;
      ans += (sum[f]-sum[s])-1;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int prime[999985];
int main()
{
  fill_n(prime, 999985, 1);
  prime[1] = 0;
  for(int i = 2; i * i <= 999985; i++) {
    if(prime[i]) {
      for(int j = i + i; j <= 999985; j += i) {
        prime[j] = 0;
      }
    }
  }
  for(int i = 1; i <= 999985; i++) {
    prime[i] += prime[i - 1];
  }

  int n;
  while(scanf("%d", &n), n) {
    long long ret = 0LL;
    for(int i = 0; i < n; i++) {
      int p, m;
      scanf("%d %d", &p, &m);
      int low = max(1, p - m);
      int high = min(999984, p + m);
      ret += prime[high] - prime[low - 1] - 1;
    }
    printf("%d\n", ret);
  }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool p[1000000];
int prime[80000];
int top;
int F(int p,int m){
	int left,right,midl,midr;
	left=0,right=top;
	while(true){
		if(left>=right-1)break;
		midl=(left+right)/2;
		if(prime[midl]<p-m)left=midl;
		else right=midl;
	}
	if(prime[midl]>=p-m)midl--;
	if(p-m<=2)midl=-1;
	left=0,right=top;
	while(true){
		if(left>=right-1)break;
		midr=(left+right)/2;
		if(prime[midr]==p+m)break;
		if(prime[midr]<p+m)left=midr;
		else right=midr;
	}
	//cout<<"%"<<prime[midl]<<" "<<prime[midr]<<endl;
	return midr-midl;
}
int main(){
	for(int i=0;i<1000000;i++)p[i]=true;
	for(int i=2;i<1000;i++)if(p[i])for(int j=i*i;j<1000000;j+=i)p[j]=false;
	top=0;
	for(int i=2;i<1000000;i++)if(p[i]){prime[top]=i;top++;}
	int n,p,m,ans;
	while(true){
	cin>>n;
	if(n==0)break;
	ans=0;
	for(int u=0;u<n;u++){
		cin>>p>>m;
		ans+=F(p,m)-1;
		//cout<<"#"<<F(p,m)<<endl;
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983+100
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MP;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(;pair_num--;){
			int p,m;
			cin>>p;
			cin>>m;
			int x=0;
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			/*
			if(x<0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
			*/
		}
		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 1000004
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;
}

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

#define MAX 1000000

using namespace std;

int eratos[MAX];
int primes[MAX];
int max_primes;

void MakePrimes(){
	int n = 0;
	eratos[0] = 1;
	eratos[1] = 1;
	eratos[2] = 0;
	for(int i = 2; i < MAX; ++i){
		if( eratos[i] == 0 ){
			for(int j = 2; i * j < MAX; ++j){
				eratos[i*j] = 1;
			}
			primes[n++] = i;
		}
	}
	primes[n++] = 1000003;
	max_primes = n;
}
int bin_search_prime(int n){
	int r = max_primes - 1, l = 0, m;

	while( r > l + 1 ){
		m = ( r + l ) / 2; 
		if( primes[m] > n )
			r = m;
		else if( primes[m] < n )
			l = m;
		else
			return m;
	}
	return l;
}

int main()
{
	MakePrimes();
	while(true){
		int n;
		int ans = 0;
		
		scanf("%d", &n);
		if( n == 0 ) break;

		for(int i = 0 ; i < n ; ++i){
			int X = 0;
			int p,m;
			scanf("%d%d", &p,&m);

			int l = bin_search_prime( std::max(0,p - m) );
			int r = bin_search_prime( std::min(MAX-1,p + m) );
			
			if( primes[l] < p-m ) ++l;
			X = (r - l) + 1;

			ans += X - 1;
		}

		printf("%d\n", ans);
	}
	return 0;
}