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 <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983+100
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MP;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(int i=0;i<pair_num;i++){
			int p,m;
			cin>>p;
			cin>>m;
			int x=0;
			int a,b;
			if(p+m>MP){
				a=MP;
			}else{
				a=p+m;
			}
			if(p-m<0){
				b=0;
			}else{
				b=p-m;
			}
			x=prime_num[a]-prime_num[b]+prime[b];
			
			if(x==0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: //42
#include<iostream>

using namespace std;

int main(){
  static bool cmp[1000000]={true,true};
  for(int i=2;i<1000000;i++){
    if(!cmp[i]){
      for(int j=2;j*i<1000000;j++){
	cmp[i*j]=true;
      }
    }
  }
  for(int n;cin>>n,n;){
    int k=0;
    for(int i=0;i<n;i++){
      int p,m;
      cin>>p>>m;
      k--;
      for(int j=p-m;j<=p+m;j++){
	k+=0<=j&&j<1000000&&!cmp[j];
      }
    }
    cout<<k<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	vi isprime(999983+1,true);
	isprime[0]=isprime[1]=false;
	repi(i,2,isprime.size())
		for(int j=i+i;j<isprime.size();j+=i)
			isprime[j]=false;
	vi primes;
	rep(i,isprime.size())
		if(isprime[i])
			primes.push_back(i);
	
	for(int n;cin>>n,n;){
		int res=0;
		rep(i,n){
			int p,m; cin>>p>>m;
			int x=upper_bound(allof(primes),p+m)-lower_bound(allof(primes),p-m);
			res+=x-1;
		}
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983+100
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MP;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(;pair_num--;){
			int p,m;
			cin>>p;
			cin>>m;
			int x=0;
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			
			if(x<0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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;

bool is_prime(int n){
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return n!=1;
}

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,max(1,p-m),min(p+m,1000000)){
				if(is_prime(j)){
					c++; //o(j);
				}
			}
			//o(c);
			if(c==0) ans--;
			else ans+=c-1;
		}
		o(ans);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int S=1000001;

bool f[S];
int im[S];

int main(){

    fill_n(f,S,true);
    f[0]=f[1]=false;

    for(int i=2;i<S;i++){
        if(f[i])im[i]++;
        im[i]+=im[i-1];
        if(!f[i])continue;
        for(int j=2;i*j<S;j++)f[i*j]=false;
    }

    int n;
    while(cin>>n,n){
        int ans=0;
        while(n--){
            int p,m;
            cin>>p>>m;
            int v=im[min(S-1,p+m)]-im[max(0,p-m-1)];
            ans+=v-1;
        }

        cout<<max(0,ans)<<endl;
    }
}

C++: #include<iostream>
using namespace std;
#define N 1000000

bool li[N];

void calpri();
int calx(int p, int m);
int money(int x);

int main(){
	int n, p[N], m[N], x[N], summoney = 0;
	while(1){
	cin >>n;										//IÊÌüÍ
	if(n == 0) break;								//nª0Åbreak
	for(int i = 0; i < n; i++){						//nÂÌIÊÌîñ
		cin >>p[i] >>m[i];
		x[i] = calx(p[i], m[i]);
		summoney += money(x[i]);
		}
		cout <<summoney <<endl;
	}
	
	return 0;
}

void calpri(){										//GgXelXÌâ¿Éæèboolì
	int max = (N - 1), min = 2;						//iªf=true fÅÍÈ¢=false
	li[0] = false;
	li[1] = false;
	
	for(int i = 2; i <= max; i++){
		li[i] = 1;
	}
	
	for(int i = 2; i <= max; ){
		for(int j = (i + 1); j <= max; j++){
			if(j % i == 0){
				li[j] = false;
			}
		}
		do{
			i++;
		}while(!li[i]);
	}
}


int calx(int p, int m){								//zñliÌtrueÌ©çfJEg
	int nop = 0, max, min;
	if((p + m) > 1000000) max = 1000000;
	else max = p + m;
	min = p - m;
	for(int i = min; i <= max; ){
		if(li[i]){
			nop++;
		}
		do{
			i++;
		}while(!li[i]);
	}
	return nop;
}

int money(int x){							//ÜàvZ
	if(x == 0) return 0;
	else return(x - 1);
}

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

const int MP = 999983;
int sv[MP+1] = {}, ac[MP+1] = {};

int main() {
	sv[0] = sv[1] = 1;
	for (int i=2; i*i<=MP; ++i) {
		if ( ! sv[i]) {
			for (int j=i*i; j<=MP; j+=i) {
				sv[j] = 1;
			}
		}
	}
	for (int i=1; i<=MP; ++i) {
		ac[i] = ac[i-1] + 1 - sv[i];
	}
	int n, p, m;
	while (cin >> n, n) {
		int ret = 0, X;
		for (int i=0; i<n; ++i) {
			cin >> p >> m;
			ret += ac[min(MP, p+m)] - ac[max(0, p-m-1)] - 1;
		}
		cout << ret << endl;
	}
	return 0;
}