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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const int N = 1000000;
const int RATE = 9973;

int p[N];
bool is_p[N];

void prime(){
  REP(i, N) p[i] = 1;
  REP(i, N) is_p[i] = false;
  p[0] = 0; p[1] = 0;
  for(int i = 2; i < sqrt(N) + 1; ++i){
    if(!p[i]) continue;
    is_p[i] = true;
    for(int j = i + i; j < N; j += i) p[j] = 0;
  }
  FOR(i, 1, N) p[i] += p[i - 1];
}

int main() {
  prime();
  int n;
  while(cin >>n && n){
    vector<P> v(n);
    map<int, int> m;
    REP(i, n){
      cin >>v[i].first >>v[i].second;
      m[v[i].first] = p[min(N - 1, v[i].first + v[i].second)] - p[max(0, v[i].first - v[i].second - 1)];
    }
    int ans = 0;
    for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it){
      if((*it).second == 0) --ans;
      else ans += ((*it).second - 1);
    }
    cout <<max(0, ans) <<endl;
  }
  return 0;
}

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

using namespace std;
int prime_num[1000001];

void prime(){  
  bool prime[1000001];
  prime_num[0]=0;
  prime_num[1]=0;
  prime_num[2]=1;

  for(int i=0;i<1000001;i++)
    prime[i]=true;
  for(int i=2;i<=1000001;i++)
    {
      if(prime[i])
	{
	  prime_num[i]=prime_num[i-1]+1;
	  for(int j=i+i;j<1000001;j+=i)
	    prime[j]=false;
	}
      else
	prime_num[i]=prime_num[i-1];
    }
}
int main()
{
  prime();
  int n;
  for(;cin>>n,n;)
    {
      int sum=0;
      for(int i=0;i<n;i++)
	{
	  int p,m;
	  cin>>p>>m;
	  int t1=p+m,t2=p-m-1;
	  if(t1>1000000)
	    t1=1000000;
	  if(t2<0)
	    t2=0;
	  if(prime_num[t1]-prime_num[t2]==0)
	    sum-=1;
	  else
	    sum+=prime_num[t1]-prime_num[t2]-1;
	  //	  cout<<prime_num[t1]<<"-"<<prime_num[t2]<<endl;
	  //	  cout<<"tmp-sum:"<<sum<<endl;
	}
      cout<<sum<<endl;
    }
  //  cout<<prime_num[1000000];
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool isPrime[1000000];
vector<int> primes;

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

	int n;
	while (scanf("%d", &n), n)
	{
		int ans = 0;
		while (n--)
		{
			int p, m;
			scanf("%d %d", &p, &m);
			int X = upper_bound(primes.begin(), primes.end(), p+m)
				- lower_bound(primes.begin(), primes.end(), p-m);
			if (X)
				ans += X - 1;
			else
				--ans;
		}
		printf("%d\n", ans);
	}

	return 0;
}

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

const int MAX = 999984;
bool isPrime[MAX];

void createIsPrime() {
    fill_n(isPrime, MAX, true);
    isPrime[0] = false;
    isPrime[1] = false;
    for (int i = 4; i < MAX; i += 2) {
        isPrime[i] = false;
    }
    for (int i = 3; i * i <= MAX; i += 2) {
        if (isPrime[i]) {
            for (int j = i * 2; j < MAX; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

int main() {
    createIsPrime();
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        int bill = 0;
        while (n--) {
            int p, m;
            cin >> p >> m;
            int prime = 0;
            for (int i = max(p - m, 2), end = min(p + m, MAX - 1); i <= end; i++) {
                if (isPrime[i]) {
                    prime++;
                }
            }
            if (prime > 0) {
                bill += prime - 1;
            } else {
                bill--;
            }
        }
        cout << bill << endl;
    }
}

C++: using namespace std;
int M=1000001;
bool P[1000001];
int main(){
	for(int i=2;i<M;i++)if(!P[i])for(int j=i+i;j<M;j+=i)P[j]=1;
	P[0]=P[1]=1;/*
	int n,p,m,c,S;
	while(cin>>n,n){
		for(S=0;n--;){
			cin>>p>>m;
			c=0; for(int i=p-m>1?p-m:2;i<=p+m&&i<M;i++)if(!P[i])c++;
			S+=c>0?c-1:-1;
		}
		cout<<S<<endl;
	}*/
	return 0;
}

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(999984,1); 
void prime_number(ull n){
   pn[0]=false;pn[1]=false;
   loop(i,2,n/2+1){
      if(!pn[i]) continue;
      for(ull j=i*i;j<=n;j+=i){
         pn[j]=false;
      }
   }
}

int main(){
   ull n,s,g;
   ll p,m;
   int x;
   prime_number(999983);
   while(cin >> n&&n){
      x=0;
      rep(i,n){
         cin>>p>>m;
         if(p-m<2) s=2;
         else s=p-m;
         if(p+m>999983) g=999984;
         else g=p+m+1;
         x+=(count(pn.begin()+s,pn.begin()+g,1)-1);
      }
      cout << x << endl;
   }
   return 0;
}