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<cstdio>

#define MAX 1000000

int eratos[MAX];

void MakePrimes(){
	eratos[0] = 1;
	eratos[1] = 1;
	eratos[2] = 0;
	for(int i = 2; i < MAX; ++i){
		if( eratos[i] == 0 ){
			for(int j = 2; i * j < MAX; ++j){
				eratos[i*j] = 1;
			}
		}
	}
}
int CountPrime(int a, int b){
	int ret = 0;
	a = std::max(0,a);
	b = std::min(MAX-1,b);
	for(int i = a; i <= b; ++i){
		if( eratos[i] == 0 )
			++ret;
	}
	return ret;
}

int main()
{
	MakePrimes();
	while(true){
		int n;
		int ans = 0;
		
		scanf("%d", &n);
		if( n == 0 ) break;

		for(int i = 0 ; i < n ; ++i){
			int X = 0;
			int p,m;
			scanf("%d%d", &p,&m);

			X = CountPrime( p - m, p + m );
			ans += X - 1;
		}

		printf("%d\n", ans);
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<double> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int sosu[1000001]={1,1,0};
int main(){
	for(int i=2;i*i<=1000001;i++)if(sosu[i]==0)
	for(int j=i*2;j<1000001;j+=i)sosu[j]=true;
	rep(i,1000001)sosu[i]^=1;
	rep(i,1000000)sosu[i+1]+=sosu[i];
	
	int n;
	while(cin>>n,n){
		int out=0;
		rep(i,n){
			int a,b;
			cin>>a>>b;
			int s=max(0,a-b-1),t=min(a+b,1000000);
			int co=sosu[t]-sosu[s];
			if(co==0)out--;
			else out+=co-1;
		}
		cout<<out<<endl;
	}
}

C++: #include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

const int SIZE = 1000001;
int T[SIZE];
bool P[SIZE];

int solve( int p, int m ) {
    int left = max( 1, p - m );
    int right = min( SIZE - 1, p + m );
    int t = T[right] - T[left-1];
    return t == 0 ? -1 : t - 1;
}

int main() {
    for ( int i = 0; i < SIZE; ++ i ) {
        T[i] = 0;
        P[i] = false;
    }
    for ( int i = 3; i < SIZE; i += 2 ) {
        P[i] = true;
    }
    P[2] = true;
    for ( int i = 3; i < SIZE; i += 2 ) {
        if ( ! P[i] ) continue;
        for ( int j = i + i; j < SIZE; j += i ) {
            P[j] = false;
        }
    }
    for ( int i = 1; i < SIZE; ++ i ) {
        T[i] = T[i-1];
        if ( P[i] ) T[i] ++;
    }
    
    int n;
    while ( cin >> n && n ) {
        int res = 0;
        for ( int i = 0; i < n; ++ i ) {
            int p, m;
            cin >> p >> m;
            res += solve( p, m );
        }
        cout << res << 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;

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++: //42
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  static bool cmp[1000000]={true,true};
  static int ps[1000000]={};
  for(int i=2;i<1000000;i++){
    ps[i]=ps[i-1];
    if(!cmp[i]){
      ps[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+=ps[min(999999,p+m)]-ps[max(p-m-1,0)]-1;
    }
    cout<<k<<endl;
  }
  return 0;
}

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

int count[1000001];
int pos[1000001];
int p[80000],size;

bool isPrime(int x){
	for(int i=0;i<size && p[i]<=sqrt(x);i++){
		if(x % p[i] == 0) return false;
	}
	return true;
}

void mkPrime(void){
	memset(pos,-1,sizeof(pos));
	p[0] = 2;
	pos[0] = pos[1] = pos[2] = 0;
	size = 1;

	for(int i=3;i<=999983;i++){
		if(isPrime(i)){
			p[size] = i;
			for(int j=i;pos[j]==-1;j--) pos[j] = size;
			size++;
		}
	}

	for(int i=999984;i<=1000000;i++){
		pos[i] = size;
	}
}

int calX(int from,int to){
	int res = 0;
	for(int i=pos[from];i < size && p[i]<=to;i++,res++);
	return res;
}

int main(void){
	int n;

	mkPrime();

	while(scanf("%d",&n) && n){
		memset(count,-1,sizeof(count));
		while(n--){
			int p,m;
			scanf("%d%d",&p,&m);
			if(count[p] == -1) count[p] = 0;
			count[p] += calX(p-m<0?0:p-m, p+m>1000000?1000000:p+m);
		}

		int ans = 0;
		for(int i=0;i<=1000000;i++){
			if(count[i] > 0) ans += count[i] - 1;
			else if(count[i] == 0) ans--;
		}

		printf("%d\n",ans);
	}

	return 0;
}

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

bool li[N + 1];

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

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

void calpri(){										//GgXelXÌâ¿Éæèboolì
	int max = N, 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 * i) <= max; ){
		for(int j = (i + 1); j <= max; j++){
			if(j % i == 0 && li[j] != false){
				li[j] = false;
			}
			while(!li[max]){
				max--;
			}
		}
		do{
			i++;
			if(i > max) break;
		}while(!li[i]);
	}
}

int calx(int p, int m){								//zñliÌtrueÌ©çfJEg
	int nop = 0, max, min;
	if((p + m) > (N + 1)) max = N;
	else max = p + m;
	if(p - m < 0) min = 0;
	else 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 -1;
	else return(x - 1);
}