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 _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

bool prime[1000001]; //10^6
int t[1000001*2];

//素数列挙
void init_prime()
{
	memset(prime,1,sizeof(prime));
	prime[0] = prime[1] = false;
	for(int i = 2; i < sizeof(prime); i++) if(prime[i])
		for(int j = i * 2; j < sizeof(prime); j += i) prime[j] = false;
	t[0] = 0;
	for(int i = 0; i < sizeof(prime); i++) t[i+1] = t[i] + prime[i+1];
	for(int i = sizeof(prime); i < sizeof(t)/sizeof(int); i++) t[i+1] = t[i];
}

int main(){
	init_prime();
	int n;
	while(cin>>n,n){
		int p = 0;
		FOR(i,n){
			int x,y; cin>>x>>y;
			int cnt = t[x+y]-t[max(x-y-1,0)];
			p += cnt-1;
		}
		cout << p << endl;
	}

	return 0;
}

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

C++: #include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define ALL(c) c.begin(),c.end()
#define LOOP(i) while(i--)
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

vector<ll> enum_prime(ll N){
	deque<bool> is_prime(N+1,true);
	vector<ll> P;
	for(ll i=2;i<=N;i++){
		if(is_prime[i]){
			for(ll j=i*i;j<=N;j+=i){
				is_prime[j] = false;
			}
			P.emplace_back(i);
		}
	}
	return P;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	auto P=enum_prime(1000000);
	vector<ll> H(2000000,0),S(2000001,0);
	for(auto& x:P) H[x]=1;
	for(int i=0;i<2000000;i++){
		S[i+1]=S[i]+H[i];
	}

	while(true){
		ll N=in();
		if(!N) break;

		ll sum=0;
		REP(i,N){
			ll A=in(),B=in();
			ll L=max(A-B,1LL),R=A+B;
			sum += (S[R+1]-S[L])?S[R+1]-S[L]-1:-1;
		}
		out(sum);
	}
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vii;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007
 

 

int MP=999983;

vi is_prime(MP+1,1);
vi sum_prime(MP+1,0);


void set_prime(){
	is_prime[0]=is_prime[1]=0;
	for(int i=2; i<=MP; i++){
		if(is_prime[i]){
			for(int j=i*2; j<MP; j+=i){
				is_prime[j]=0;
			}
		}
		sum_prime[i]=sum_prime[i-1]+is_prime[i];
	}
}

int main(){
	set_prime();
	int n,p,m;
	int x;
	while(cin>>n && n>0){
		int ans=0;

		rep(i,n){
			cin>>p>>m;
			x=(p+m>MP?sum_prime[MP]:sum_prime[p+m])-(p-m-1>0?sum_prime[p-m-1]:0);
			if(x==0) ans--;
			else{
				ans+=x-1;
			}
		}
		cout<<ans<<endl;
	}
}

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=1000000+1;
bool isp[N];
vector<int> ps;
void init(){
    memset(isp,1,sizeof(isp));
    isp[0] = isp[1] = false;
    for(int i=2;i<N;i++){
        if(!isp[i])continue;
        ps.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(all(ps), p+m+1);
    auto l = lower_bound(all(ps), 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 <iostream>
#include <vector>
using namespace std;
vector<bool> prime(1000001,true);

void p_init(){
	prime[0] = prime[1] = false;
	for(int i=0;i<=1000;i++){
		if(prime[i])
			for(int j=i<<1;j<=1000000;j+=i){
				prime[j] = false;
			}
	}
}

int main(){
	p_init();
	int x,n,p,m;
	int ret;
	while(cin >> n && n){
		ret = 0;
		for(int i=0;i<n;i++){
			cin >> p >> m;
			int mi = p<m ? 0 : p-m;
			int mx = p+m>1000000?1000000:p+m;
			x = 0;
			for(int o=mi;o<=mx;o++){
				if(prime[o]){
					x++;
				}
			}
			if(x){
				ret += x-1;
			}else{
				ret--;
			}
		}
		cout << (ret<0?0:ret) << endl;
	}
}

C++: #include<iostream>
using namespace std;
bool a[1000001];
int main() {
	fill(a, a + 1000001, true);
	a[0] = false; a[1] = false;
	for (int i = 2; i <= 999983; i++) {
		if (a[i]) {
			int j = 2;
			while (i*j <= 1000000) {
				a[i*j] = false;
				j++;
			}
		}
	}
	int n;
	while (cin >> n&&n != 0) {
		long long sum = 0;
		for (int i = 0; i < n; i++) {
			int p, m, X = 0, k;
			cin >> p >> m;
			k = (p - m);
			if ((p - m) < 0) k = 2;
			for (int j = k; (j <= p + m && j <= 1000000);) {
				if (a[j]) X++;
				if (j % 2 == 0) j ++;
				else j+=2;
			}
			if (X == 0) sum -= 1;
			else sum += (X - 1);
		}
		if (sum < 0) cout << "0" << endl;
		else cout << sum << endl;
	}
}