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];
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,1,N){
    sum[i] += sum[i-1] + is_prime[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;
      if(p - m-1 < 0){
        s = 0;
      }
      else{
        s = p -m-1;
      }
      f = (p+m > PrimeMax) ? PrimeMax-1: p + m;
      ans += (sum[f]-sum[s])-1;
    }
    if(ans)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

vector<int> prime;
bool is_prime[1000010];
int min_factor[1000010];
int sieve(int n) {
  fill(is_prime + 2, is_prime + 1000010, true);
  min_factor[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime.emplace_back(i);
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
        if (min_factor[j] == 0) min_factor[j] = i;
      }
    }
  }
  return prime.size();
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  sieve(1000000);
  int cum[1000010];
  for (int i = 1; i <= 1000000; i++) {
    cum[i] = is_prime[i] + cum[i - 1];
  }
  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int p, m;
      cin >> p >> m;
      ans += cum[min(1000000, p + m)] - cum[max(1, p - m) - 1] - 1;
    }
    cout << ans << endl;
  }

  return 0;
}

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

//#define MAX 10000
#define MAX 999985
//素数配列
int h_sosu[MAX] = {0};

//----- 列挙関数 -----//
void f_sosu();


int main(void)
{
	//素数を列挙
	f_sosu();

	//入力変数
	int n = 0;

	while (true)
	{
		cin >> n;
		if (n == 0)	{	return 0;	}

		//答え変数(王が1払ってくれる)
		int ans = 0;
		//当たりくじ
		int atari = 0;
		//組
		int kumi = 0;

		//入力
		for (int i = 0; i < n; i++)
		{
			cin >> atari >> kumi;
			//ループ変数
			int j = atari - kumi;
			//終了
			int end = atari + kumi;
			//1は政府が支払う
			bool flag_in = false;

			int count = 0;
			//負数はいらない
			if (j < 0)	{	j = 0;	}
			//値が大きすぎるなら
			if (end >= MAX)	{end = MAX;	}
			while (h_sosu[count] < j)
			{
				count++;
			}
			while (h_sosu[count] <= end)
			{
				count++;
				ans++;
			}
			ans--;
		}

		cout << ans << endl;
	}

	return 0;
}



//*****----- 素数列挙(エラトステネスのふるい) -----*****//

void f_sosu()
{
	//2 , 3は加えておく
	register int count = 2;
	h_sosu[0] = 2 , h_sosu[1] = 3;
	//終了の値
	register int end = 0;
	//素数flag
	register bool flag_sosu = false;

	for (int i = 5; i < MAX; i += 2)
	{
		//flag初期化
		flag_sosu = true;
		//平方根までで良い
		end = sqrt((double)i);
		//カウント変数まで回す
		for (int j = 0; j < count; j++)
		{
			if (i % h_sosu[j] == 0)
			{
				flag_sosu = false;
				break;
			}
			if (h_sosu[j] > end)	{	break;	}
		}
		//素数なら
		if (flag_sosu == true)
		{
			h_sosu[count++] = i;
		}
	}
}

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(ll i=(a);i<ll(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,p,m;
   prime_number(1000000);
   while(cin >> n){
      if(n==0)break;
      int x=-n;
      rep(i,n){
         cin>>p>>m;
         loop(j,p-m,p+m+1){
            if(j<0||j>1000000) continue;
            x+=(int)pn[j];
         }
      }
      cout << x << endl;
   }
   return 0;
}