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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
using namespace std;
typedef long long ll;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<(DUMP(),__VA_ARGS__).str()<<" ["<<__LINE__<<"]"<<endl)
struct DUMP:ostringstream{template<class T>DUMP &operator,(const T&t){if(this->tellp())*this<<", ";*this<<t;return *this;}};
#else
#define dump(...)
#endif

const int N=1000111;
bool isp[N];
vector<int> prime;
void init(){
    memset(isp, 1, sizeof(isp));
    isp[0] = isp[1] = false;
    for(int i=2;i<N;i++){
        if(!isp[i])continue;
        prime.push_back(i);
        for(int j=i+i;j<N;j+=i){
            isp[j] = false;
        }
    }
}

int cnt(int p, int m) {
    auto u = lower_bound(prime.begin(), prime.end(), p+m+1);
    auto l = lower_bound(prime.begin(), prime.end(), p-m);
    return u - l;
}

int n, p, m;

int main() {
    init();
    while(cin>>n && n) {
        int ans = 0;
        rep(x,n){
            cin >> p >> m;
            ans += cnt(p, m)-1;
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pii;

int prime[1000000];
bool is_prime[1000001];

void sieve(int n){
	int p=0;
	for(int i=0;i<=n;i++) is_prime[i]=true;
	is_prime[0]=is_prime[1]=false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++]=i;
			for(int j=2*i;j<=n;j+=i) is_prime[j]=false;
		}
	}
}

int main(){
	int n,p,m;
	sieve(1000000);
	while(1){
		int ans=0;
		cin>>n;
		if(n==0) break;
		rep(i,0,n){
			cin>>p>>m;
			int c=0;
			rer(j,p-m,p+m){
				if(is_prime[j]) c++;
			}
			//o(c);
			if(c==0) ans--;
			else ans+=c-1;
		}
		o(ans);
	}
}

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

using namespace std;

int main () {
	vector<int> furui(999984, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i < 999984; i++) {
		if (furui[i] == 0) {
			for (int j = i; j <= 999983/i; j++) {
				furui[i*j] = 1;
			}
		}
	}
	int n;
	int a, b, p, m;
	int counter;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		counter = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			a = p-m;
			if (a < 2) {
				a = 3;
				counter++;
			}
			b = p+m;
			if (b > 999983) {
				b = 999983;
			}
			for (int j = a; j <= b; j++) {
				if (j % 2 != 0 && furui[j] == 0) {
					counter++;
				}
			}
			counter--;
		}
		cout << counter << endl;
	}
	return 0;
}

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 < 0){
        s = 0;
      }
      else{
        s = p -m;
      }
      f = (p+m > PrimeMax) ? PrimeMax: p + m;
      if(s == f){
        if(!is_prime[s])
          ans -= 1;
      }
      else if(sum[s]- sum[f] == 0){
        ans -= 1;
      }
      else{
        ans += (sum[f]-sum[s])-1;
      }
    }
    if(ans)
      cout << ans << endl;
    else
      cout << 0 << 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 all(n) (n).begin(),(n).end()
#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;
      int ans = 0;
      rep(i,n)
	{
	  cin >> p >> m;
	  int bottom = max(0,p-m);
	  int top = min(999983,p+m);
	  int X = upper_bound(all(prime),top) - lower_bound(all(prime),bottom);
	  if(X == 0)ans--;
	  else      ans += X-1;
	}
      cout << ans << endl;
    }
  return 0;
}

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


#define MAX_N 1000000
int prime[MAX_N];//i番目の素数
bool is_prime[MAX_N+1];
int sieve(int n){
	int p=0;
	memset(is_prime,true,sizeof(is_prime));
	is_prime[0]=is_prime[1]=false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++]=i;
			for(int j=2*i;j<=n;j+=i)is_prime[j]=false;
		}
	}
	return p;
}


int main(){
	int num=sieve(MAX_N),n;
	while(cin>>n&&n){
		int ans=0,x;
		int p,m;
		for(int i=0;i<n;i++){
			cin>>p>>m;
			x=upper_bound(prime,prime+num,p+m)-lower_bound(prime,prime+num,p-m);
			if(x==0)ans--;
			else ans+=x-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}