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
//------------------------------------------
#include <bits/stdc++.h> // only for GCC

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

//container util
//------------------------------------------
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

//repetition
//------------------------------------------
#define loop(i,a,b) for(ull i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define riter(i,c) for(auto i=(c).rbegin(); i!=(c).rend(); ++i)

//constant
//--------------------------------------------
const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

// output vector
// ------------------------------------------
namespace std {
    template<typename T>ostream&operator<<(ostream&os, vector<T>const&v){
        iter(it,v) os<<*it<<(it+1==v.end()?"":",");
        return os;
    }
    template<typename T, typename U>ostream&operator<<(ostream&os,const pair<T,U>&p){
        return os << "[" << p.dirst << "," << p.second << "]";
    }
}

//clear memory
// ------------------------------------------
#define clr(a,i) memset((a), (i) ,sizeof(a))

int lcm(int a,int b){
   return a*b/__gcd(a,b);
}
 
vb pn(1000001,1); 
void prime_number(ull n){
   //fill(all(pn),1);
   pn[0]=false;pn[1]=false;
   loop(i,2,n/2+1){
      if(!pn[i]) continue;
      for(ull j=2;i*j<=n;j++){
         pn[i*j]=false;
      }
   }
}

int main(){
   ull n,s,g;
   ll p,m;
   prime_number(1000000);
   while(cin >> n){
      if(n==0)break;
      int x=-n;
      rep(i,n){
         cin>>p>>m;
         if(p-m<0) s=0;
         else s=p-m;
         if(p+m>999983) g=999984;
         else g=p+m+1;
         /*loop(j,s,g){
            x+=(int)pn[j];
         }*/
         x+=count(pn.begin()+s,pn.begin()+g,1);
      }
      cout << x << endl;
   }
   return 0;
}

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

const int PRIME_SIZE = 1000000;

int main()
{
	bool prime[ PRIME_SIZE ] = { 0 };
	vector< int > pList;
	prime[ 0 ] = prime[ 1 ] = true;
	for( int i = 2; i < PRIME_SIZE; ++i ){
		if( prime[ i ] ) continue;
		pList.push_back( i );
		for( int j = i + i; j < PRIME_SIZE; j += i ){
			prime[ j ] = true;
		}
	}

	int n;
	while( cin >> n && n ){
		int pay = -n;
		int* hit = new int[ PRIME_SIZE ]();
		for( int i = 0; i < n; ++i ){
			int p, m;
			cin >> p >> m;
			int low = max( 0, p - m );
			int high = min( PRIME_SIZE - 1, p + m );
			int count = 0;
			for( int j = low; j <= high; ++j ){
				++hit[ j ];
			}
		}
		for( int i = 0; i < pList.size(); ++i ){
			pay += hit[ pList[ i ] ];
		}

		cout << ( pay < 0 ? 0 : pay ) << endl;

		delete[] hit;
	}
	return 0;
}

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

using namespace std;

const int MAX = 999984;
vector<int> v;
bool prime[MAX];

void sieve(){
    fill(prime, prime+MAX, true);
    prime[0] = prime[1] = false;

    for(int i=0; i < MAX; i++){
        if(!prime[i]) continue;
        v.push_back(i);
        for(int j=i+i; j < MAX; j += i){
            prime[j] = false;
        }
    }
}

int main(){
    sieve();
    int n;
    while(cin >> n, n){
        int ans = 0;
        for(int i=0; i< n; i++){
            int a, b;
            cin >> a >> b;
            int p = lower_bound(v.begin(), v.end(), a+b+1) - lower_bound(v.begin(), v.end(), a-b);
            ans += p;
        }
        ans -= n;
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

std::vector<int> primes;
bool isPrime[1000000 + 1];

void initPrimes(){
    std::fill(isPrime, isPrime+1000000+1, true);

    isPrime[0] = false;
    isPrime[1] = false;
    for(int i=4;i<=1000000;i+=2){isPrime[i] = false;}
    
    primes.push_back(2);

    for(int i=3;i<=1000000;i+=2){
        if(isPrime[i]){
            primes.push_back(i);
            for(int j=2*i;j<=1000000;j+=i){
                isPrime[j] = false;
            }
        }
    }
}

int main(){
    initPrimes();

    int N;
    while(std::cin >> N, N){
        int res = 0;
        REP(i, N){
            int P, M;
            std::cin >> P >> M;
            res += std::upper_bound(primes.begin(), primes.end(), P+M)
                - std::lower_bound(primes.begin(), primes.end(), P-M) - 1;
        }

        std::cout << res << std::endl;
    }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge{int to,from,cost;};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;i--)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define repnd(i,m,n) for(int i=(n)-1;i>=(m);i--)
#define rep0(i,n) for(i=0;i<(n);++i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define IL for(;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647/3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,-1,1,-1,0};
const int dy[]={0,0,1,-1,1,-1,-1,1,0};

bool pr[1000000];
int np[1000000];

int main() {
	rep(i,1000000)pr[i]=true;
	pr[0]=pr[1]=false;
	rep(i,1000){
		if(pr[i]){
			int j;
			for(j=i*i;j<1000000;j+=i){
				pr[j]=false;
			}
		}
	}
	rep(i,999999)np[i+1]=np[i]+(pr[i+1]?1:0);
	int n,p,m,res;
	IL{
		cin>>n;
		if(n==0)break;
		res=-n;
		while(n--){
			cin>>p>>m;
			int a=max(0,p-m-1);
			int b=min(999999,p+m);
			res+=np[b]-np[a];
		}
		cout<<res<<endl;
	}
	return 0;
}